Jump to content

[SOLVED] checkboxes and arrays


anser316

Recommended Posts

Hi I am having a couple of problems with my checkboxes.

The first is that the id number, which i have put as the value of the checkbox, does post properly, but the others post only in order of the list, not of what has been checked.

 

e.g.

ID  BID Dname stock level

1    1    a        3      3

2    2    b        4      4

3    3    c        5      5   

 

If i check 2 and 3

ID  BID Dname stock level

2    1    a        3      3

3    2    b        4      4

 

So the checkbox for the ID works but the other values are not taken from that ID.

 

I am sure one of the factors is on my second form with where i have put my if statements and while loops, and where i have opened and closed them.

 

$result =mysql_query("SELECT.....
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></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=checkbox name=ticked[] value='{$row['drug_id']}'>";
echo "</td></tr>"; 
}
echo "</table>";
echo "<input type =submit value= Submit>";
echo "<input type = reset>";
echo "</form>";

 

if (isset($_POST['ticked'])) {
for ($i=0; $i<count($_POST['ticked']); $i++) {
$result =mysql_query("UPDATE branch_items SET
status ='ORDERED'
WHERE drug_id=".$_POST['ticked'][$i]."
AND branch_id=".$_POST[branch_id][$i]."");
}

if (isset($_POST['ticked'])) {
for ($i=0; $i<count($_POST['ticked']); $i++) {
$result2 =mysql_query("SELECT status from branch_items
WHERE drug_id=".$_POST['ticked'][$i]."
AND branch_id=".$_POST[branch_id][$i]."");
}}
$row = mysql_fetch_array( $result2 );



    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>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 $row[status];
echo "</td></tr>"; 
    }
}

Link to comment
Share on other sites

my brain might be a little mushy but I'm having a hard time figuring out what you want help with specifically,

 

you're on the right track, providing examples and code, but a specific question makes things go much more smoothly,

 

not to mention helps train your brain to debug your own work

Link to comment
Share on other sites

Its cuz only checked boxes get sent.

u didnt check 1, so boxes 2 & 3 got sent

however all hidden fields get sent.

 

so add keys to the indexes.

 

$counter=0;
while($row = mysql_fetch_array( $result2 )) {
echo "<tr><td>";
echo "<input type='hidden' name=drug_id[$counter] value='$row[drug_id]'>";
.
.
echo "<input type=checkbox name=ticked[] value='$counter'>";
echo "</td></tr>"; 
$counter++;
}

 

now each array value in ticked returns an index value associated to the info

Link to comment
Share on other sites

ok ive tried that, and it makes sense, thanks, but when i put this in

 

if (isset($_POST['ticked'])) {

for ($i=0; $i<count($_POST['ticked']); $i++) {

echo "<br>drug id: ";

echo $_POST[drug_id][$i];

echo "    branch id: ";

echo $_POST[branch_id][$i];

echo "    exp: ";

echo $_POST[drug_name][$i];

}}

i still get the checkbox 1 not the one i selected.

 

i dont think im setting $i to equal the value in $counter or its not storing properly. can some1 please help me with this

Link to comment
Share on other sites

Forget all the hidden fields.

 

List the data with c/box an each row name='cbox[]' value='id'

 

On the next page, to list the selected items, requery the database

 

<?php
$items = join (',', $_POST['cbox']);
$sql = "SELECT * FROM tablename WHERE id IN ($items)";
  // query and list selected data
?>



Link to comment
Share on other sites

<input type="checkbox" name="cbox[bx1]">
<input type="checkbox" name="cbox[bx2]">
<input type="checkbox" name="cbox[bx3]">
<input type="checkbox" name="cbox[bx4]">
<input type="checkbox" name="cbox[bx5]">
<input type="checkbox" name="cbox[bx6]">

 

$_POST['cbox'] will be an array listing each checkbox that was selected.  If a box isn't selected, it won't be part of the array.

 

Link to comment
Share on other sites

ok ive tried that, and it makes sense, thanks, but when i put this in

 

if (isset($_POST['ticked'])) {

for ($i=0; $i<count($_POST['ticked']); $i++) {

echo "<br>drug id: ";

echo $_POST[drug_id][$i];

echo "    branch id: ";

echo $_POST[branch_id][$i];

echo "    exp: ";

echo $_POST[drug_name][$i];

}}

i still get the checkbox 1 not the one i selected.

 

i dont think im setting $i to equal the value in $counter or its not storing properly. can some1 please help me with this

 

cuz the keys for ticked are not sequential.

using foreach will solve yer problem.

if (isset($_POST['ticked'])) {
foreach ($_POST['ticked'] as $id) {
echo "<br>drug id: ";
echo $_POST[drug_id[$id]];
echo "    branch id: ";
echo $_POST[branch_id[$id]];
echo "    exp: ";
echo $_POST[drug_name[$id]];
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.