0

Hi all, it's been awhile...
my prob. I have a form to order drinks in a bar, as below.

<div class="col_left">
<h2> Order Your Drinks And Food</h2><br />
<form action="process.php" method="post" name="orders">
<table border="1" class="trapezi">
<tr>
<th>Drink</th><th>Qty</th>
</tr>
<tr>
 <td><input type="checkbox" name="drink[]" value="p/c">Carlsberg 50cl</td>
 <td><input type="text" name="drinkno[]" size="2"></td>
</tr>
<tr>
<td><input type="checkbox" name="drink[]" value="c/half">Carlsberg 25cl</td>
 <td><input type="text" name="drinkno[]" size="2"></td>
 </tr>
 <tr>
 <td><input type="checkbox" name="drink[]" value="b/k">Keo Bottle</td>
 <td><input type="text" name="drinkno[]" size="2"></td>
 </tr>
 <tr>
 <td><input type="checkbox" name="drink[]" value="b/c">Carlsberg Bottle</td>
 <td><input type="text" name="drinkno[]" size="2"></td>
 </tr>
 <tr>
 <td><input type="checkbox" name="drink[]" value="b/w">Water 25cl</td>
 <td><input type="text" name="drinkno[]" size="2"></td>
 </tr>
 <tr>
 <td colspan="2" align="center"><input type="submit" name="submit" value="Order"></td>
 </tr>
  </table>


</form> 
</div>

the php script is so,

<?php
date_default_timezone_set('Asia/Nicosia');
$db=new sqlite3('daily.sqlite');

$name=htmlspecialchars($_COOKIE['visitor']);
$name=ucfirst($name);
$drink=$_POST['drink'];
$drinknum=$_POST['drinkno'];

$ordertime=date('H:i');
$date=date('dm');
$dord=(implode($_POST['drink']));
$drno=(implode($_POST['drinknum']));


//$db->exec("INSERT INTO lists (name,seat,drink,food,ticket,date)
//           VALUES('$name','44','$dord','$ford','$ordertime','$date')");





echo "Drink order for ".$name."<br />";
foreach($drink as $dord) {
echo $dord."<br />";

}
echo $ordertime;
//etc;
?>

which works to a point, but what I need to do is work it so that when it is echo'd or added to the db the
drink and qty are a pair.
I tried array_combine, but if only one or two checkboxes are checked, it throws an error.
I tried array_pad to make the arrays the same length, but of course, thay only adds to the end of the array.
any help much appreciated.

3
Contributors
3
Replies
27
Views
3 Years
Discussion Span
Last Post by TonyG_cyprus
0

I think you coud try changing the array name attribute

<td><input type="checkbox" name="data[0][drink]" value="p/c">Carlsberg 50cl</td>
<td><input type="text" name="data[0][drinkno]" size="2"></td>

continue the index value for the next pair of input
and print

print_r($_POST['data']);

Edited by Budy_1

0

You can also try doing something like this

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

    var_dump($_POST);

  }

the above should give you two sets of array. Assuming that all items are checked and quantities were filled, the above codes will give us something like this

array(3) {
  ["drink"]=>
  array(5) {
    [0]=>string(3) "p/c"
    [1]=>string(6) "c/half"
    [2]=>string(3) "b/k"
    [3]=>string(3) "b/c"
    [4]=>string(3) "b/w"
  }
  ["drinkno"]=>
  array(5) {
    [0]=>string(1) "1"
    [1]=>string(1) "2"
    [2]=>string(1) "3"
    [3]=>string(1) "4"
    [4]=>string(1) "5"
  }
  ["submit"]=>string(5) "Order"
}

looking at the dumped array, we can see that the drink and drinkno are paired by index that's all we need to know.

We change the code above to test if we can get pair.

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

    echo $_POST['drink'][0] .'=>'. $_POST['drinkno][0] .'<br/>';

}

You can also sort the not empty entries

$order ='';
$order .=(!empty($_POST[drink'][0] && !empty($_POST['drinkkno'][0] ?'drink type :'. $_POST[drink'][0].' quantity : '.$_POST['drinkkno'][0] : false );
$order .= '<br/>';

## do the same for the remaining drinks

Edited by veedeoo: more info added

0

Thanks guys Problem solved by complete re-write and change of db set up.
Now works as intended

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.