i am getting following errors :
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
Can any one guide me how to fix this :
<?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"> </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"> </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;
}
}
?>
The array in question is $_GET[]
... which means...
Are you calling your file with that variable? Something like http://example.com/something.php?oid=123?
Because that's what it's looking for - it looks like all the other errors are cascading consequences of this error.
The error is coming from this line and its because you are trying to access a variable that isn't set.
$order_details = $product->loadOrderDetails(intval($_GET['oid']));
You pass GET parameters through the URL like products.php?oid=123
and you will get this error if you don't add this to the URL. So I suggest you change your code to check to see if the variable is set before using it. Something like
if(!empty($_GET['oid'])) {
$order_details = $product->loadOrderDetails(intval($_GET['oid']));
}
Your second error is related to the first. Because $_GET['oid']
doesn't exist it means that $order_details
is presumably empty. So you also need to do a check further down in your code before you do your foreach
to check that $order_details isn't empty
You're trying the read field that wasn't set. Perphaps it happens when you call your script without oid
parameter, e.g. products.php?oid=12345
. So instead of believing that you have value set in $_GET['oid']
please check if it is set, e.g. with isset($_GET['oid'])
and basing on this you can make decision whether or not try to load products etc.