未定义的索引数组不能引用我的其他PHP页面? [英] undefined index arrays cannot refer to my other php page?

查看:134
本文介绍了未定义的索引数组不能引用我的其他PHP页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php
define("TITLE", "Menu Item | Risque");

include('includes/header.php');

$menuItem = '';
$dish = '';

if (isset($_GET['item'])) {

    $menuItem = $_GET['item']; 

    $dish = $menuItems[$menuItem];
}

function suggestedTip ($price, $tip){

    $totalTip = $price * $tip;
}
?>

<center><img src = "hr.png">
<br>

<div id = "dish">


    <h1><?php $dish = array(); echo $dish["title"]; ?> <span class="price"><sup>$</sup><?php echo $dish["price"]; ?></span></h1>
    <p><?php echo $dish["blurb"]; ?></p>
    <br>
    <p><strong>Suggested beverage: <?php echo $dish["drink"]; ?></strong></p>
    <p><em>Suggested tip: <sup>$</sup><?php suggestedTip($dish["price"], 0.20); ?></em></p>

</div>

arrays.php codeS低于
不知怎的,数组中的值不会在我的网页显示

arrays.php codes are below somehow the values inside the array does not show up in my page

<?php

$navItems = array (

                array(
                    'slug'  => "index.php",
                    'title' => "Home"
                     ),

                array(
                    'slug'  => "menu.php",
                    'title' => "Menu"
                     ),

                array(
                    'slug'  => "contact.php",
                    'title' => "Contact"
                     ),

                );



$menuItems = array(

                "club-sandwich" => array(

                    "name"  => "Club Sandwich",
                    "price" => 11,
                    "blurb" => "Bacon ipsum dolor sit amet fatback landjaeger ullamco pariatur. Nostrud laboris et, duis drumstick eiusmod kevin ut aliquip. Filet mignon short ribs tenderloin short loin kielbasa non pork chop aliqua. Jerky shank tri-tip capicola, non andouille tenderloin cupidatat turducken meatball pork beef eu shoulder jowl.",
                    "drink" => "Club Soda"

                ),

                "dill-salmon" => array(

                    "name"  => "Lemon &amp; Dill Salmon",
                    "price" => 18,
                    "blurb" => "Pork belly tempor ground round qui exercitation, jowl leberkas sed voluptate excepteur jerky. Reprehenderit veniam cow, quis in ribeye andouille eu pastrami eiusmod exercitation dolor.",
                    "drink" => "Fancy Wine"

                ),

                "super-salad" => array(

                    "name"  => "The Super Salad<sup>&reg;</sup>",
                    "price" => 34,
                    "blurb" => "Gumbo beet greens corn soko endive gumbo gourd. Parsley shallot courgette tatsoi pea sprouts fava bean collard greens dandelion okra wakame tomato. Dandelion cucumber earthnut pea peanut soko zucchini.",
                    "drink" => "Jug o' Water"

                ),

                "mexican-barbacoa" => array(

                    "name"  => "Mexican Barbacoa",
                    "price" => 23,
                    "blurb" => "I love fruitcake danish caramels. Tart danish pastry liquorice chocolate cake fruitcake. Bear claw gingerbread muffin I love apple pie apple pie tiramisu brownie chocolate. Sweet roll cotton candy cupcake gingerbread gummies jelly-o. Muffin I love croissant I love jelly-o brownie jelly beans. Toffee I love pastry.",
                    "drink" => "Beer with a lime"

                ),

             );

?>

我是新来的PHP,但我有一个项目,是即将到期,但我不断收到未定义的误差标题价格导语

I am new to PHP but I have a project that is due soon but I keep getting undefined error for title, price and blurb.

我想AP preciate,如果任何人都可以提供帮助。

I would appreciate it if anyone can help.

推荐答案

您已经声明为变量的 $菜空数组。因此,有没有办法让类似的标题,价格,Blurb的空数组索引。如果你正试图从数据库中获取的值,那么DB结果存储在变量$菜,然后继续。同时经常检查,如果你使用回声的使用isset()功能之前,数组的索引存在。否则它会抛出通知错误最大错误模式和错误日志文件。请参阅下面的code。

You have declared empty array for the variable $dish. So there is no way to get indexes like title, price, blurb from the empty array. If you are trying to get values from database, then store the db result in the variable $dish and proceed. Also always check if the array index exist before you echo it using isset() function. Else it will throw notice error in error maximum mode and in error log file. Refer the code below.

<h1><?php if(isset($dish["title"])) echo $dish["title"]; ?> <span class="price"><sup>$</sup><?php if(isset($dish["price"])) echo $dish["price"]; ?></span></h1>
<p><?php echo if(isset($dish["blurb"])) $dish["blurb"]; ?></p>
<br>
<p><strong>Suggested beverage: <?php if(isset($dish["drink"])) echo $dish["drink"]; ?></strong></p>
<p><em>Suggested tip: <sup>$</sup><?php if(isset($dish["price"])) suggestedTip($dish["price"], 0.20); ?></em></p>

所以这code不会显示错误。

So this code will not display the error.

这篇关于未定义的索引数组不能引用我的其他PHP页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆