Sorry in advance but for some reason cannot get this to work:
i am using the following function to create a multidimensional array of names and unit types from my database:
function display_unit_type($call1){ // function to display all unit type
$call = $call1;
$query1 = "SELECT Name , Type FROM $call, units WHERE $call.Unit_ID = units.Unit_ID";
include('../connections/connection2.inc'); // database connection
$result = $db->query($query1);
$num_results = $result->num_rows;
echo 'num results = '.$num_results.'<br>';
$len = $num_results - 1;
$name_unit[$len];
if($num_results > 0) {
// we have at least one fruit, so show all fruit names
for ($i=0; $i<$num_results; $i++)
{
$row = $result->fetch_assoc();
$product_names_called = $row['Name'];
// echo $product_names_called.'<br>';
$product_unit_type = $row['Type'];
// echo $product_unit_type.'<br>';
$name_unit[$i] = array($product_names_called, $product_unit_type); // return a multidimensional array with results
} // to the for
} // to the if
echo $name_unit[0][0].'.<br>';
echo $name_unit[0][1].'.<br>';
echo $name_unit[1][0].'.<br>';
echo $name_unit[1][1].'.<br>';
echo $name_unit[2][0].'.<br>';
echo $name_unit[2][1].'.<br>';
echo $name_unit[3][0].'.<br>';
echo $name_unit[3][1].'.<br>';
echo $name_unit[4][0].'.<br>';
echo $name_unit[4][1].'.<br>';
$db->close();
unset($product_names_called);
unset($product_unit_type);
unset($result);
unset($num_results);
unset($query1);
unset($i);
unset($row);
unset($call);
unset($len);
return($name_unit);
}
This function works fine, checked it as a seperate entity, but cannot seem to retrieve the returned array from this function back in the main program:
....//main program
if( ($sgv_Formats[$k] != '') && ($sgv_Formats[$k] != 'tray') ){
// need to ge the unit type from the units table
$name_and_unit = display_unit_type($call2); // returns mulit array of names and units i.e. the call to the function above
for($a=0; $a<(count($name_and_unit)); $a++){
if($sgv_Formats[$k] == $name_and_unit[$a][0]){
$unit = $name_and_unit[$a][1];
}
}
...... // main program continues
So here in the main program i believe that the array returned from the function should now be stored in the $name_and_unit array, but this does not seem to be the case. If anyone has a pointer at what i am doing wrong would be very grateful!