注意:未定义的索引:

时间:2014-02-27 17:02:46

标签: javascript php html

我收到以下错误:

Notice: Undefined index: oid in C:\wamp\www\var\de\products.php on line 9

Warning: Invalid argument supplied for foreach() in C:\wamp\www\var\de\products.php on line 45

任何人都可以指导我如何解决这个问题:

<?php

include('all.php');
include('product.php');

$product = new Products();


$order_details = $product->loadOrderDetails(intval($_GET['oid']));

print $order_details;

?>
<html>
<head>
    <title>Debug Test</title>

</head>
<body>


    <br/>

    <table align="center" border="1" cellpadding="1" cellspacing="2" width="90%">
        <tr>
            <th>Order id</th>
            <th>Order Date</th>
            <th colspan="2">Order Total</th>
        </tr>
        <tr>
            <td><?=$order_details['order_id'];?></td>
            <td><?=$order_details['ordered_on'];?></td>
            <td colspan="2"><?=$order_details['order_total'];?></td>
        </tr>
        <tr>
            <td colspan="5">&nbsp;</td>
        </tr>
        <tr>
            <th>Name</th>
            <th>Description</th>
            <th>Price</th>
            <th>Qty</th>
        </tr>
<?php
    foreach ($order_details['products'] as $pid => $info) {
?>
        <tr>
            <td><?=$info['name'];?></td>
            <td><?=$info['description'];?></td>
            <td><?=$info['price'];?></td>
            <td><?=$info['qty'];?></td>
        </tr>
<?php
    }
?>
        <tr>
            <td colspan="5">&nbsp;</td>
        </tr>
        <tr>
            <th colspan="5">Customer Information</th>
        </tr>
        <tr>
            <td colspan="5">
                <?=$order_details['customer_info']['username'];?><br/>
                <?=$order_details['customer_info']['company_name'];?><br/>
                <?=$order_details['address']['street'];?><br/>
                <?=$order_details['address']['city'].', '.$order_details['address']['state'].' '.$order_details['address']['zip'];?><br/>
            </td>
        </tr>
    </table>

</body>
</html>

all.php

<?php

/**
 * Fake database access
 * @return array customer info
 */
function getCustomerInfo() {

    return array (
        1=>array(
            'customer_id' => 1,
            'username'=>'george',
            'address_id' => 3,
            'created_on'=>'2005-01-28',
            'company_name'=>'Foo Inc.',
        ),
        2=>array(
            'customer_id' => 2,
            'username'=>'sam',
            'address_id' => 2,
            'created_on'=>'2005-10-09',
            'company_name'=>'Foo Inc.',
        ),
        3=>array(
            'customer_id' => 3,
            'username'=>'harrison',
            'address_id' => 1,
            'created_on'=>'2005-07-21',
            'company_name'=>'Bar Inc.',
        )
    );
}

function getAddresses() {
    return array(
        1 => array(
            'address_id' => 1,
            'street' => '123 Main St.',
            'city' => 'Some City',
            'state' => 'PA',
            'zip' => '12345'),
        2 => array(
            'address_id' => 2,
            'street' => '345 Garden Dr.',
            'city' => 'Manhatten',
            'state' => 'NY',
            'zip' => '55555'),
        3 => array(
            'address_id' => 3,
            'street' => '876 Over There BLVD',
            'city' => 'Atlanta',
            'state' => 'GA',
            'zip' => '88899'));
}

/**
 * Fake database access
 * @return array customer info
 */
function getOrderInfo() {
    return array (
        1=>array(
            'order_id' => 1,
            'ordered_on'=>'2006-11-17',
            'ordered_by'=>1,
            'order_total' => '47.00',
            'products'=>array(
            array('product_id' => 5, 'qty' => 2),
            array('product_id' => 6, 'qty' => 1),
            array('product_id' => 7, 'qty' => 3))
        ),

        2=>array(
            'order_id' => 2,
            'ordered_on'=>'2006-10-17',
            'ordered_by'=>1,
            'order_total' => '4.00',
            'products'=>array(
            array('product_id' => 1, 'qty' => 3),
            array('product_id' => 2, 'qty' => 1),
            array('product_id' => 7, 'qty' => 2))
        ),

        3=>array(
            'order_id' => 3,
            'ordered_on'=>'2006-11-12',
            'ordered_by'=>3,
            'order_total' => '43.00',
            'products'=>array(
            array('product_id' => 5, 'qty' => 1),
            array('product_id' => 6, 'qty' => 2))
        )
    );
}


/**
 * Fake database access
 * @return array customer info
 */
function getProducts() {
    return array (
        1=>array(
            'product_id' => 1,
            'name'=>'Product A',
            'description'=>'Fancy Product with options',
            'price' => '10.00',
        ),
        2=>array(
            'product_id' => 2,
            'name'=>'Product B',
            'description'=>'Fancy Product with options',
            'price' => '10.00',
        ),
        3=>array(
            'product_id' => 3,
            'name'=>'Product C',
            'description'=>'Fancy Product with options',
            'price' => '20.00',
        ),
        4=>array(
            'product_id' => 4,
            'name'=>'Product D',
            'description'=>'Fancy Product with options',
            'price' => '5.00',
        ),
        5=>array(
            'product_id' => 5,
            'name'=>'Product E',
            'description'=>'Fancy Product with options',
            'price' => '13.00',
        ),
        6=>array(
            'product_id' => 6,
            'name'=>'Product F',
            'description'=>'Fancy Product with options',
            'price' => '15.00',
        ),
        7=>array(
            'product_id' => 7,
            'name'=>'Product G',
            'description'=>'Fancy Product with options',
            'price' => '2.00',
        )
    );
}

function print_array($a) {
    echo '<pre>';
    print_r($a);
    echo '</pre>';
}


?>

Product.php

<?php

class Products {

    var $name;
    var $description;
    var $productId;

    function Products($id = 0, $infoArr = array()) {
        if ($id > 0 && count($infoArr)) {
            $this->name = $infoArr['name'];
            $this->description = $infoArr[''];
            $this->productId = $id;
        }
    }



    /**
     * @static
     */
    function loadAllProducts() {
        $arr = getProducts();
        $prods = array();
        foreach ($arr as $id=>$info) {
            $prods[$id] = new Products($id,$info);
        }

        return $prods;
    }

/**
 * function loadOrderDetails
 * 
 * This function should be giving us all the information about the order:
 * The customer's name and address, the products that were ordered (deescriptions too) and the order totals.
 * See products that php to see what is expected to be shown
 *
 * @param Integer   $order_id   the unique identifier for the order
 * @return Array    $cur_order  the details of the order
 * 
 */
    function loadOrderDetails($order_id) {
        $orders    = getOrderInfo();
        $products  = getProducts();
        $customer  = getCustomerInfo();
        $address   = getAddresses();


        return $order_id;
    }
}

?>

3 个答案:

答案 0 :(得分:2)

错误来自此行及其错误,因为您正在尝试访问未设置的变量。

$order_details = $product->loadOrderDetails(intval($_GET['oid']));

您通过products.php?oid=123之类的网址传递GET参数,如果您不向网址添加,则会出现此错误。因此,我建议您更改代码以在使用之前检查变量是否已设置。像

这样的东西
if(!empty($_GET['oid'])) {
    $order_details = $product->loadOrderDetails(intval($_GET['oid']));
}

您的第二个错误与第一个错误有关。由于$_GET['oid']不存在,这意味着$order_details可能是空的。因此,在执行foreach检查$ order_details不为空之前,您还需要在代码中进一步检查

答案 1 :(得分:1)

有问题的数组是$_GET[] ...这意味着......

您是否使用该变量调用文件?类似http://example.com/something.php?oid=123的内容?

因为这就是它正在寻找的东西 - 看起来所有其他错误都是这个错误的级联后果。

答案 2 :(得分:1)

您正在尝试未设置的读取字段。在没有oid参数的情况下调用脚本时会发生Perphaps,例如: products.php?oid=12345。因此,不要相信您已在$_GET['oid']设置了价值,请检查是否已设置,例如使用isset($_GET['oid'])并在此基础上,您可以决定是否尝试加载产品等。