Forum Moderators: coopster

Message Too Old, No Replies

Passing variables using foreach..

         

Darren

1:24 pm on Mar 31, 2003 (gmt 0)

10+ Year Member



Dear All,
I am new to this forum and new to PHP as you may see so any help would be appreciated.

On page 1 I create a list from a database of checkboxes, quantities, and items, and I want to pass several of the variables onto page 2 depending on whether or not the checkbox has been ticked. It looks like it should be so simple (and I'm sure it is!). If anyone could suggest anything I would appreciate it.
In the following code on page 2 it is the $value and $qty that don't seem to contain anything.

Many thanks

PAGE 1
======

<?php
session_start();
$connection = mysql_connect($host, $user,$password)
or die ("Couldn't connect to server.");
$db = mysql_select_db($database, $connection)
or die ("Couldn't select database.");
$result=mysql_query("SELECT item, itemid, qty FROM tbl where username = '$logname'");

?>

<table width='95%' border='0' cellspacing='0' cellpadding='0'>
<form action="addselected.php" method="post">

<?php
while ($row=mysql_fetch_array($result))
extract($row);
}
?>
<tr>
<td><INPUT TYPE='checkbox' NAME='itemid[]'</td>
<td><b><?php echo $qty?></b></td>
<td><?php echo $item?></td>
<td><input type=hidden name='qty[]' value='<?php echo $qty?>'></td>
</tr>
<?php
}
?>

<tr><td>
<input type='submit' value='Add selected'>
</form>
</td></tr>
</table>

PAGE 2
======

<?php
session_start();
$connection = mysql_connect($host, $user,$password)
or die ("Couldn't connect to server.");
$db = mysql_select_db($database, $connection)
or die ("Couldn't select database.");

foreach ($HTTP_POST_VARS as $key => $value)
{
echo "$key, <b>$value</b>, $qty<br>";
}

?>

jatar_k

7:20 pm on Mar 31, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Darren,

Does it work on the first page before it passes to page 2?

I was looking at the output loop and there may be some mismatched braces.

<?
while ($row=mysql_fetch_array($result))
extract($row);
}
?>
html output stuff
<?
} // what's this brace for? seems to be orphaned
?>

you may also want to look at this thread for similarities. It is about deleting not adding but the premise is very similar.
Deleting Multiple Items Using Checkboxes! [webmasterworld.com]

willybfriendly

11:30 pm on Mar 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is this what you were trying to do? Looked to me like you had a backwards curly bracket, and it was on the wrong line. I am at work, so could not check the output. This should extract the variables and assign them to the table as a part of the loop. Original code simply extracted the variables as I read it.

<?php
while ($row=mysql_fetch_array($result))
{ //moved to here from...
extract($row);
//...here

//add echo statements
echo "<tr>";
echo "<td><INPUT TYPE='checkbox' NAME='itemid[]'</td>";
echo "<td><b>$qty</b></td>";
echo "<td>$item</td>";
echo "<td><input type=hidden name='qty[]' value=\"$qty\"></td>";
echo "</tr>";
}
?>

<edit>long day, fixed obvious errors above :-)<edit>

Darren

8:08 am on Apr 1, 2003 (gmt 0)

10+ Year Member



Many thanks that has solved the problem!
I don't want to outstay my welcome but.......
As you can see the first page has a list of checkboxes in column 1 and then an associated qty in column 2. My next problem (I promise I won't keep doing this!) is that when somebody selects for example checkboxes 1 and 5 only two values get passed onto the next page however the associated qty has all 5 values passed onto the next page and I cannot tie up the two lists. ie checkbox 1 ties up with the value passed for qty 1 but checkbox 5 has the value of qty 2.

As I couldn't crack the above I moved onto this tactic.. Forgeting the qty value altogether and just looping through the checkboxes to run a select query on each pass to get the qty value again but I keep getting 'Parse error: parse error, unexpected $ in /home/wwwnutr/public_html/menu1addselected.php on line 22'
Any suggestions gratefully recieved!
Here is the simple code from page 2 where I try to run the query..

<?php
session_start();
$connection = mysql_connect($host, $user,$password)
or die ("Couldn't connect to server.");
$db = mysql_select_db($database, $connection)
or die ("Couldn't select database.");

foreach ($itemid as $key => $value)
{
$result=mysql_query("select qty from menus where itemid='$value' and menunumb='1');
}

?>

Darren

1:01 pm on Apr 1, 2003 (gmt 0)

10+ Year Member



jatar_k
I have looked at the link you mentioned in your posting about 'Deleting Multiple Items Using Checkboxes' and made 'some' progress. I think to finally crack this I need to apply that logic to a select query. The field that I am trying to access in the code below is qty but I cannot seem to access any value in that. I presume it is creating it as a variable type but I'm not sure how to index it to obtain the value...

foreach ($itemid as $item)
{
mysql_query("Select qty FROM menus WHERE itemid = '$item' and menunumb='1'");
}

Darren

3:46 pm on Apr 1, 2003 (gmt 0)

10+ Year Member



I've done it.
Its probably not the best code ever but it works. Thanks for your help.

foreach ($itemid as $itemida)
{
$result=mysql_query("Select qty FROM menus WHERE itemid = '$itemida' and menunumb='1'");
while ($row = mysql_fetch_row($result))
{
$qty=$row[0];
}

jatar_k

4:34 pm on Apr 1, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nice, I love it when people figure things out ;)

an alternate method could be similar to the one in the other thread. Build your query using IN ('val1','val2','val3') and then execute it after.

$which = 0;
foreach ($itemid as $itemida) {
if ($which!=0) $itemlist .= ",";
$itemlist .= "'" . $itemida . "'";
$which++;
}
$selq = "Select qty FROM menus WHERE itemid in (" . $itemlist . ")" and menunumb='1';
$result=mysql_query($selq);

while ($row = mysql_fetch_row($result))
{
$qty=$row[0];
}

I think that is right. Keeping db calls to a minimum is always a bonus. Queries can, usually, be made more complex and created dynamically to decrease the number of db calls and better exploit the power of mysql.