I have a form that loops and queries out values for different products. you can either delete these products or send them to the store front.
say if the user just wants to delete them. they check the delete button but then when they press the submit button. the browser looks through the send to store front script too. I want it check to see if any send to store front checkboxes have been check. if not then skip the store front syntax and go straight to the delete syntax. I am using $_POST to carry the values over. I tried using the if set but it still runs throuh the send to store front syntax as if nothing happened. on the first page I looped all the products out but I also marked each checkbox name with a number. something like this
<?php $count = 0; ?>
<?php loop ?>
<input type="checkbox" name="storefront<?php echo $count; $count++ ?>
<?php end loop?>
so each name will be sent in the post like this
storefront0
storefront1
storefront2
and so on...
below is my script that processes that information here is my script
<?php if (isset($_POST)){
require_once('Connections/ok.php');
mysql_select_db($database_ok,$ok);
$sf = "false";
$i = 0;
//echo $_POST['storeFront'.$i]; //it would echo out the value for storefront0
foreach ($_POST as $key => $val) //the values for $key is storefront,delete,and submit. all of them have numbers beside them as well. like delete0 delete1 delete2 delete3 , and same for storefront. it all depends how many products was in the form. if there was 25 products then the $key will go from delete0 to delete25. the $val is the productIDs
{
$filter = substr($key,0,6); //all this function does is deletes the numbers from the word. so if its delete12 or delete9. the substr will only pick up the word delete which is a 6 letter word
if ($filter != 'delete' and isset($_POST['storeFront'.$i])) //it checks to see if the $_POST is a delete. if it does then it wont let the value pass through because the storefront varible below only makes a list of items going to the storefront only not to be deleted
{
$storefront .= "'".$val."',";
$sf = 'true';// here is a varible called $sf (short for StoreFront) and its assigned the value 'true'. if you look a little further down the script you will see a if statement that checks to see rather $sf is true or not. if its true then it does the mysql_query and inserts all the values into it. if it doesnt then evidently there is no items that need to go to the store front. so skip all the syntax and check to see if there is any items that need to be deleted.
//Here is my problem no matter what. the varible $sf is assigned true so therefore the send to store front syntax is always executed. even if no values are in the $_POST storefront varible.
$i++
}
}
$storefront = substr($storefront,0,-10);//when you echo out a $_POST, your also going to get the submit button as well. all this substr() function does it delete the value submit from list.
if ($sf == "true")
{
$sql = "SELECT * FROM products WHERE productID IN ($storefront)";
$insert = mysql_query($sql,$ok);
$numRows = mysql_num_rows($insert);
$i = 0;
$sql = "SELECT productID FROM storefront WHERE productID IN ($storefront)";
$results = mysql_query($sql,$ok);
$check = mysql_num_rows($results);
$sql = "SELECT * FROM storefront";
$results = mysql_query($sql,$ok);
$sfRows = mysql_num_rows($results);
if ($check == 0)
{
while ($row = mysql_fetch_assoc($insert))
{
if ($sfRows != 4)
{
$sql = "INSERT INTO storeFront
(sfName,sfPic,sfPrice,productID)
VALUE
('{$row['productName']}','{$row['productPic']}',
'{$row['productPrice']}','{$row['ProductID']}')";
mysql_query($sql,$ok);
} else {echo 'There is more then 4
products in the Store Front!';}
$sql = 'SELECT * FROM storeFront';
$results = mysql_query($sql,$ok);
$sfRows = mysql_num_rows($results);
}
} else {echo 'Some products you tried to insert
are already in the Store Front!';}
}
$counted = count($_POST);
$i=0;
$sf = "false";
Im starting to begin to think that its because Im using a foreach loop instead of a while loop. Im guesing that a foreach loop will look through each $_POST regarless if its is set or not to see rather a value is in there or not. I dont know, im confused and going crazy.