Reputation: 51
I working on a program and coming up with some annoying issue. I am trying to display data from an array. I copied the format from another array I setup and it works perfect. The only difference is I am gathering a lot more data...
Calling the function:
$data1 = display_orders($_SESSION['user_id'], $limit, 'fName', 'lName', 'VendorName', 'DateRequested', 'Shipping', 'VendorNumber', 'VendorFax', 'VendorAddress', 'VendorCity', 'VendorState', 'VendorZip', 'EquipmentConsumable', 'GasType', 'GasLocation', 'UNMTag', 'EquipmentLocation', 'index', 'totalcost', 'Approved', 'Shipped');
The Function itself
<?php
function display_orders($user_id, $limit)
{
$data = array();
$user_id = (int)$user_id;
$limit = (int)$limit;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
// print_r($func_get_args);
if ($func_num_args > 1)
{
unset($func_get_args[0]);
unset($func_get_args[1]);
$fields = '`' . implode('`, `', $func_get_args) . '`';
for($x = 0; $x < $limit; $x++)
{
$data[] = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `users` , `vendor` WHERE $user_id = users.id AND $user_id = vendor.user_id ORDER BY vendor.DateRequested DESC"));
}
return $data;
}
}
?>
So now i try to echo the data out:
echo $data1['VendorName'];
I get no output.
If I do the following:
print_r ($data1);
I get output!
Array ( [0] => Array ( [fName] => admin [lName] => test [VendorName] => Newegg [DateRequested] => 2013-09-19 [Shipping] => Standard [VendorNumber] => NA [VendorFax] => NA [VendorAddress] => NA [VendorCity] => NA [VendorState] => NA [VendorZip] => 00000 [EquipmentConsumable] => Equipment [GasType] => [GasLocation] => [UNMTag] => 0 [EquipmentLocation] => Computer Lab [index] => 0 [totalcost] => 39.99 [Approved] => 0 [Shipped] => 0 ) )
But if i try to add a field name No data....
Any help and I would be grateful!
Upvotes: 0
Views: 874
Reputation: 1511
As you see, $data1
is Array([0] => Array(...))
, so you'll need to call
echo $data1[0]['VendorName'];
This is because you are assigning data as $data[] = mysql_fetch_assoc(...)
.
Upvotes: 5
Reputation: 1553
echo $data1[0]['VendorName'];
I suggest when using print_r you first echo <PRE>
as it will help you to see the true structure of the array easier. In this case, the location of VendorName is one layer deeper than you are echoing.
Upvotes: 3