Page 1 of 1

PHP Noobie needs major help

Posted: Tue May 22, 2007 7:48 am
by skeedo
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I need to debug a fairly simple php script that shows order information from variables created in other PHP file that mocks an SQL database. Unfortunately my PHP is fairly limited I am just learning, I am hoping someone can point me in the right direction?

Currently trying to view an order with products.php I get 'invalid argument supplied' for line 43 which is  'foreach ($order_details['products'] as $pid => $info)'. All that I can deduce from this is I am getting the error because the variable is not an array, I think? Any help would be greatly appreciated.

products.php

Code: Select all

<?php

include('lib_util.php');
include('lib_products.php');

$product = new Products();

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

?>
<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>
lib_products.php

Code: Select all

<?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['description'];
    		$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 $cur_order;
	}
}

?>
lib_util.php

Code: Select all

<?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'=>2,
			'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>';
}


?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue May 22, 2007 9:24 am
by bdlang
In quickly glancing over the code, your Product::loadOrderDetails() method returns an empty variable $cur_order, that according to the documentation, should be an array with all the values returned from the various 'get' functions as defined in lib_util.php. Once you figure that out you need to deal with the fact that the various functions actually return multidimensional arrays, i.e. your foreach() won't work as coded anyway.