doushao1948 2011-06-04 23:46
浏览 18
已采纳

我无法获得保留在$ _SESSION变量中的值

I am trying to get values out of an indexed array, but when I use a variable as the index (which is equal to an index in the array), it does not appear. I get an Undefined Index Error. Why is this happening? Does anyone know? Thanks!

I have the following code:

$bookarray = array();
                $books = mysqli_query($db, "SELECT * FROM books");
                while($row = mysqli_fetch_assoc($books)){
                    $bookarray[$row['bookID']] = array(
                        'title' => $row['title'],
                        'author' => $row['author'],
                        'price' => $row['price']
                    );
                }
                echo"<hr>";
                print_r($bookarray);
                echo"<hr>";
                echo $bookarray[5]['title']."<br />";


                foreach($_SESSION['cart'] as $cartedbook){
                    echo $cartedbook;
                    echo $bookarray[$cartedbook]['title'];
                    echo "<br />";
                }

This is the output:

Books


Array
(
[1] => Array
    (
        [title] => Java 2 for Pro Deelopers
        [author] => Michael Morgan
        [price] => 34.99
    )

[2] => Array
    (
        [title] => Installing XAMPP
        [author] => Thomas Down
        [price] => 24.99
    )

[3] => Array
    (
        [title] => Alice Through the Looking Glass
        [author] => Louis Carroll
        [price] => 72.35
    )

[4] => Array
    (
        [title] => Quantum Mechanics in 124 Hours
        [author] => Neils Bohr
        [price] => 24.99
    )

[5] => Array
    (
        [title] => PHP For Fun And Profit
        [author] => Thomas Shenk
        [price] => 49.99
    )

[28] => Array
    (
        [title] => Test
        [author] => Eric Gross
        [price] => 100.00
    )

)


PHP For Fun And Profit
5
Notice: Undefined index: 5 in C:\xampp\htdocs\FinalProject\cart.php on line 52


3
Notice: Undefined index: 3 in C:\xampp\htdocs\FinalProject\cart.php on line 52

Title     Author     Price

  • 写回答

2条回答 默认 最新

  • duan7772 2011-06-04 23:55
    关注

    If I look at the source of your question, it seems there is a newline character before the number (so, it would be " 5" instead of 5, which logically cannot be found), can you confirm this? What does a var_dump($cartedbook); yield?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型