I grabbed part of a cart array script from a book to try and use the code to display detail items for an order view administration page and I cannot seem to get the array to work well.
Goal: display items ordered on the administration side order view/update page.
I have a table with data where records details can be selected based on the order number. The foreach or while, depending on what it ends up being, should get the details of an activity by using the code it's passed and putput a formatted table row with the details recovered from the activities table.
Data(from MySQL table order_details; non-indexed):
+----------+-------+----------+--------+
| order_no | code | quantity | price |
+----------+-------+----------+--------+
| 271 | IH01 | 1 | 92.71 |
| 270 | PH02 | 1 | 350.00 |
| 270 | BIG01 | 1 | 65.00 |
+----------+-------+----------+--------+
There's no index because I think I should be able to get away with it since the query I'm using selects records for one order number and the activity codes are unique within that order number group.
PHP code:
function db_result_to_array($result)
{
$res_array = array();
for ($count=0; $row = @mysql_fetch_array($result); $count++)
$res_array[$count] = $row;
return $res_array;
}
function get_activity_details($activity_no)
{
// query database for all details for a particular activity
if (!$activity_no || $activity_no=="")
return false;
$conn = db_connect();
$query = "SELECT *
FROM activities
WHERE code='$activity_no'";
$result = @mysql_query($query);
if (!$result||mysql_num_rows($result)==0)
{
echo "Could not get Activity Details.<br>";
return false;
}
$result = @mysql_fetch_array($result);
return $result;
}
function display_order_dtl($order_no)
{
$query="SELECT code,quantity,price FROM order_details
WHERE order_no='$order_no'";
echo "<table width=\"100%\" cellspacing=\"0\">
<form action=\"show_cart.php\" method =\"post\">
<tr>
<th>Activity</th>
<th>Vendor</th>
<th class=\"count\">Price</th>
<th class=\"count\">Quantity</th>
<th class=\"count\">Tax</th>
<th class=\"count\">Total</th></tr>";
$result=mysql_query($query);
$rows=mysql_num_rows($result);
echo $rows;
$detail=mysql_fetch_array($result);
//$items=mysql_fetch_array($result);
//display each item as a table row
//while(list($order_no,$activity_no) = each($items))
while($items=mysql_fetch_array($result))
//foreach ($items as $order_no => $activity_no)
{
$activity=get_activity_details($items["code"]);
echo $activity["net"];
echo "<tr>";
echo "<td>";
//$adultchild=display_adult_radio_view(get_adult_child_radio($activity["code"]));
echo $activity["title"]
." - ".$adultchild."</td><td>"
.$activity["vendor_name"];
echo "</td><td>$".number_format($activity["net"], 2);
echo "</td><td>";
// if we allow changes, quantities are in text boxes
echo "<input type=\"text\" name=\"$activity_no\" value=\"$qty\" size=\"3\">";
echo "</td><td>$".number_format($activity["tax_amt"]*$qty,2)."</td>\n";
echo "</td><td>$"
.number_format($activity["net"]*$qty+$activity["tax_amt"]*$qty,2)
."</td></tr>\n";
echo "</tr>";
unset($items[sizeof($items)-1]);
}
echo "</form></table>";
}
There are a couple commented out lines of things I tried as well as some redundant or unused variables, so don't spank me too hard on that.
Current trouble:
This function gives the table headers and displays no data. I should have exactly two table rows output when selecting using order number 270 from the data above. I checked that $row=mysql_num_rows($result) does return the number two which tells me I do have two rows of data in the array.
I can also get the data by echoing either $detail[0]["code"]; and $deatil[1]["code"];, or any other part thereof using this method.
Depending on which messed up version of a foreach or while loop I use, I get none or 6 table rows with the wrong data. The current while loop correctly finds the first line for order 270, but the second is not displayed.
Current output:
2 //number or rows from echo $rows;
350.00 //testing that data was found by echoing $activity["net"] a field from the activities table
Table headings:
Activity Vendor Price Quantity Tax Total
Correct first record found using code PH02 from data above:
The Hawaii Experience - Paradise Helicopters $350.00 $0.00 $0.00
Can you see why the second record is not displayed?