0

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!

3
  • echo $data1[0]['VendorName']; Commented Sep 19, 2013 at 22:08
  • or simply 'view source' if this is output to a browser. Commented Sep 19, 2013 at 22:11
  • Use echo '<pre>', print_r($data1), '</pre>'; for a formatted output of the array. Commented Sep 19, 2013 at 22:13

3 Answers 3

5

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(...).

Sign up to request clarification or add additional context in comments.

Comments

3
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.

Comments

0

You forgot the index [0]:

echo $data1[0]['VendorName'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.