Jump to content

Problem with Looping and post


anser316

Recommended Posts

Hi on the second form, the loop is not working correctly

 

I get

 

DRUGID    ID      DRUG NAME  STOC    RE      SUPPLIER

5        2      sampledrug7      4        4      Unichem

7        2      sampledrug7      4        4      Unichem

 

The drug id is taken from the checkbox and ive used a foreach. The other data is from the 'hidden'text box posted. This doesnt seem to work, and furthermore its shows the data for the second drug_id

 

Form 1

$result2 =mysql_query("SELECT i.drug_id,i.branch_id, d.drug_name,i.total_stock,i.reorder_level, s.supplier_name, i.status
FROM drugs d, supplier s, branch_items i,branch b
WHERE d.supplier_id=s.supplier_id
AND d.drug_id=i.drug_id
AND b.branch_id=i.branch_id
AND i.total_stock<=i.reorder_level
ORDER BY total_stock");

echo "<BR><br>";
echo "<b>Minimum Stock Level Met - Reorder Needed</b><p>";
echo "<form action= conorder.php method=POST>";
echo "<table border='1'>";
echo "<tr> <th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th><th>STOCK</th><th>REORDER LEVEL</th><th>SUPPLIER</th></tr>";
while($row = mysql_fetch_array( $result2 )) {
echo "<tr><td>";
echo "<input type='hidden' name='drug_id' value='$row[drug_id]'>";
echo $row['drug_id'];
echo "</td><td>";
echo "<input type='hidden' name='branch_id' value='$row[branch_id]'>";
echo $row['branch_id'];
echo "</td><td>";
echo "<input type='hidden' name='drug_name' value='$row[drug_name]'>";
echo $row['drug_name'];
echo "</td><td>";
echo "<input type='hidden' name='total_stock' value='$row[total_stock]'>";
echo $row['total_stock'];
echo "</td><td>";
echo "<input type='hidden' name='reorder_level' value='$row[reorder_level]'>";
echo $row['reorder_level'];
echo "</td><td>";
echo "<input type='hidden' name='supplier_name' value='$row[supplier_name]'>";
echo $row['supplier_name'];
echo "</td><td>";
echo "<input type=checkbox name=ticked[] value='{$row['drug_id']}'>";
echo "</td></tr>"; 

Form 2

if (isset($_POST['ticked'])) {
echo "chosen<br>";
echo "<table border='1'>";
echo "<tr> <th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th><th>STOCK</th><th>REORDER LEVEL</th><th>SUPPLIER</th><th>Status</th></tr>";
foreach ($_POST['ticked']as $drug_id){

echo "<tr><td>"; 
echo $drug_id;
echo "</td><td>";
echo "$_POST[branch_id]";
echo "</td><td>";
echo $_POST[drug_name];
echo "</td><td>";
echo $_POST[total_stock];
echo "</td><td>";
echo $_POST[reorder_level];
echo "</td><td>";
echo $_POST[supplier_name];
//echo "</td><td>";
//echo $_POST[reorder_quantity];
//echo "</td><td>";

Link to comment
https://forums.phpfreaks.com/topic/98751-problem-with-looping-and-post/
Share on other sites

Well, of course it is. In page 1 you have a loop that is creating the fields for each record. But, you are ony creating an array for the "ticked" checkbiox field. For all the other fields you are simply creating a duplicate field - so the last itteration of the loop determines the values. You need to create arrays for all of those fields as well and then iterrate through all the fields using a counter:

 

<?php
$query = "SELECT i.drug_id,i.branch_id, d.drug_name,i.total_stock,i.reorder_level, s.supplier_name, i.status
          FROM drugs d, supplier s, branch_items i,branch b
          WHERE d.supplier_id=s.supplier_id
            AND d.drug_id=i.drug_id
            AND b.branch_id=i.branch_id
            AND i.total_stock<=i.reorder_level
          ORDER BY total_stock";

$result2 =mysql_query($query) or die (mysql_error());

echo "<BR><br>";
echo "<b>Minimum Stock Level Met - Reorder Needed</b><p>";
echo "<form action= conorder.php method=POST>";
echo "<table border='1'>";
echo "<tr> <th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th><th>STOCK</th><th>REORDER LEVEL</th><th>SUPPLIER</th></tr>";
while($row = mysql_fetch_array( $result2 )) {
    echo "<tr><td>";
    echo "<input type='hidden' name='drug_id' value='$row[drug_id]'>";
    echo $row['drug_id'];
    echo "</td><td>";
    echo "<input type='hidden' name='branch_id' value='$row[branch_id]'>";
    echo $row['branch_id'];
    echo "</td><td>";
    echo "<input type='hidden' name='drug_name' value='$row[drug_name]'>";
    echo $row['drug_name'];
    echo "</td><td>";
    echo "<input type='hidden' name='total_stock' value='$row[total_stock]'>";
    echo $row['total_stock'];
    echo "</td><td>";
    echo "<input type='hidden' name='reorder_level' value='$row[reorder_level]'>";
    echo $row['reorder_level'];
    echo "</td><td>";
    echo "<input type='hidden' name='supplier_name' value='$row[supplier_name]'>";
    echo $row['supplier_name'];
    echo "</td><td>";
    echo "<input type=checkbox name=ticked[] value='{$row['drug_id']}'>";
    echo "</td></tr>";
}
?>

 

<?php
if (isset($_POST['ticked'])) {
    echo "chosen<br>";
    echo "<table border='1'>";
    echo "<tr><th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th><th>STOCK</th><th>REORDER LEVEL</th><th>SUPPLIER</th><th>Status</th></tr>";
    for ($i=0; $i<count($_POST['ticked']); $i++) {//as $drug_id){

        echo "<tr><td>"; 
        echo $_POST['ticked'][$i];
        echo "</td><td>";
        echo $_POST[branch_id][$i];
        echo "</td><td>";
        echo $_POST[drug_name][$i];
        echo "</td><td>";
        echo $_POST[total_stock][$i];
        echo "</td><td>";
        echo $_POST[reorder_level][$i];
        echo "</td><td>";
        echo $_POST[supplier_name][$i];
        //echo "</td><td>";
        //echo $_POST[reorder_quantity][$i];
        //echo "</td><td>";
    }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.