Page 1 of 1

multiple variables

Posted: Mon Jan 08, 2007 2:21 pm
by PHP_ColdFusion
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


ok i know that the question has probably been asked somewhere before but i jsut spent like an hour looking on forums and cant find the answer to it.

How do i set multiple values to a variable i.e. i am trying to set variables with check boxes and set the variables to the product id # in my sql database.

i can do that part fine.  now when i go to pll the info back out of my variable i cant figure out how to make it pull EACH value independantly.  

code shown below

Code: Select all

$edit = $_POST['edit'];
$page = $_POST['page'];
$select = $_POST['select'];
$onhand = $_POST['onhand'];
$value = $onhand + $_POST['value'];
$isitprint = $_POST['edit'];
$itis = 'Print';
mysql_select_db("inventory", $con);
ECHO '<script type="text/javascript"> function printpage() {frame.print()} </script>';
IF ($isitprint != $itis) {
ECHO '<head>';
ECHO '<meta http-equiv="refresh" content="5;url=';
ECHO '?page=';
ECHO $page;
ECHO '">';
ECHO '</head>';
}
SWITCH ($_POST['edit']) {

	// if edit => Print
 	CASE 'Print':
		
		$sel  = "SELECT * FROM products
             WHERE ProductID = '" . $select . "'";       		    
       		           		
    $result = mysql_query($sel,$con);
		
		IF (!mysql_query($sel,$con))
 		 {
 		  ECHO '<tr>';
		  ECHO '<td><CENTER>';
		  ECHO 'Error: ' . mysql_error();
		  ECHO '<br><a href="';
      ECHO '?page=';
      ECHO $page;
      ECHO '">Go Back</a>';
		  ECHO '</td>';
		  ECHO '</tr>';
		  DIE();
 		 } ELSEIF (mysql_query($sel,$con)) {
   		  while($row = mysql_fetch_array($result))
         {
         echo '<tr>';
         echo '<td style="border:none;">'.$row['ProductID'].'</td>';
         echo '<td style="border:none;">'.$row['category'].'</td>';
         echo '<td style="border:none;">'.$row['Supplier'].'</td>';
         echo '<td style="border:none;">'.$row['Serial'].'</td>';
         echo '<td style="border:none;">'.$row['Product'].'</td>';
         echo '<td style="border:none;">'.$row['Description'].'</td>';
         echo '<td style="border:none;">'.$row['LeadTime'].'</td>';
         echo '<td style="border:none;">$ '.$row['UnitPrice'].'</td>';
         echo '<td align=right style="border:none;"> '.$row['UnitsOnHand'].'</td>';
         echo '</tr>';
         }
 		 }
            BREAK;
 }
all the other variables work and go to something else


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jan 08, 2007 2:23 pm
by PHP_ColdFusion
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


oh yeah here is the first page that sends the info

Code: Select all

<form action="?page=productedit" method="post">
<?
while($row = mysql_fetch_array($result))
  {
 echo '<tr>';
 echo '<td style="border:none;"><INPUT TYPE=checkbox NAME="select[]" VALUE="'.$row['ProductID'].'"></td>';
 echo '<td style="border:none;">'.$row['ProductID'].'</td>';
 echo '<td style="border:none;">'.$row['category'].'</td>';
 echo '<td style="border:none;">'.$row['Supplier'].'</td>';
 echo '<td style="border:none;">'.$row['Serial'].'</td>';
 echo '<td style="border:none;">'.$row['Product'].'</td>';
 echo '<td style="border:none;">'.$row['Description'].'</td>';
 echo '<td style="border:none;">'.$row['LeadTime'].'</td>';
 echo '<td style="border:none;">$ '.$row['UnitPrice'].'</td>';
 echo '<td align=right style="border:none;"> '.$row['UnitsOnHand'].'</td>';
 echo '</tr>';
}
?>
</form>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jan 08, 2007 2:25 pm
by Burrito
the checkbox will send the values as an array.

you can loop over the values with a foreach() loop

Posted: Mon Jan 08, 2007 2:29 pm
by PHP_ColdFusion
so my while needs to turn into a foreach?

Posted: Mon Jan 08, 2007 2:32 pm
by Burrito
no on the backend you need to loop over your checkbox array like this:

Code: Select all

foreach($_POST['select'] as $val)
  echo $val;

Posted: Mon Jan 08, 2007 2:35 pm
by Kieran Huggins
Other script - when you collect the $_POST data. $_POST['select'] is an array.

Read: http://www.php.net/manual/en/language.types.array.php
and: http://www.php.net/manual/en/ref.array.php

Edit: Nevermind... Burrito is too fast for me ;)

Posted: Mon Jan 08, 2007 2:43 pm
by PHP_ColdFusion
ok that lets me echo the array

but thats not what i want to do.

i want to take each value in the array and pull the info that corrisponds to that value out of the sql table and place it in a row.

Posted: Mon Jan 08, 2007 2:50 pm
by Burrito
so put your SQL logic within the loop.

if you're tying to pull info where all of a single column match that value, you might look at implode() rather than looping over the array.

Posted: Mon Jan 08, 2007 3:09 pm
by PHP_ColdFusion
thanx guys you kick azz