This was done by my friend TaxDAY. I couldn't believe it so I spent about 2 hours breaking it down (if statements and such) step by step. At the end of the two hours, I was frustrated, got absolutely nowhere, and was ready to slit my rist over this. Now, with the suicidal talk out of the way, Good Luck and Thanks.
~sn0w
This post is not for the faint of heart. This is a disclaimer. You can and probably will go crazy trying to figure this problem out.
As some of you might know, I have a business in giving people online stores that are stocked with a full product line. I coded the entire app from scratch and it's working very well. Although, I recently came across a problem that defies the logic of PHP itself. I have a function that's not working for some strange reason and I have been trying to figure out what's up with it for about three hours now. In my store system, products can have options. Now, if you add a product that has options (not all products have options) to your cart without selecting options, the system is designed to give you a screen before checkout that makes you select options for each product that you neglected to select options for. This works fine. The real issue is when you hit submit on this screen. Here's where it starts to get wierd. The logic in the following function is supposed to return parts of an HTML form for each of the products in question. Instead, it outputs all of the products that have no options. I use a MYSQL db and since this is being worked on right now, I have all of the fields returned by the query for each product printed out so that you can see the issue. It seems like the three consecutive "if" statements just don't work.... and not only do they not work, but it seems the logic itself that the if statement follows is broken.
Here's the code:
function OptFilled($ordid){
//check and see if all of the product options have been filled out
global $sszdir,$siteid;
$query = 'SELECT order_info.Product_ID,order_info.prod_option1 as selopt1,order_info.prod_option2 as selopt2,order_info.prod_option3 as selopt3,products.ProdName,products.DispImage,products.prod_option1,products.prod_option2,products.prod_option3 from order_info,products where order_info.Product_ID = products.ID AND order_info.Completed="n" AND order_info.Order_ID='.$ordid;
$result = mysql_query($query) or die("Invalid query: $query >>> " . mysql_error());
for ($i=0;$i<mysql_num_rows($result);$i++){
$ordarr = mysql_fetch_assoc($result);
$arrkeys = array_keys($ordarr);
foreach ($arrkeys as $key) {if ($siteid == 345) echo '<b>'.$key.':</b> '.$ordarr[$key].'<br>';}
$dirty = 0;
//if ($siteid == 345) echo ($ordarr['Product_ID'].'<br>'.$ordarr['prod_option1'].' - '.$ordarr['prod_option2'].' - '.$ordarr['prod_option3'].'<br>'.$ordarr['selopt1'].' - '.$ordarr['selopt2'].' - '.$ordarr['selopt3']).'<br><br>';
if ($ordarr[prod_option1] != 0 && ($ordarr[selopt1] == null || $ordarr[selopt1] == 0)) $dirty = 1;
if ($ordarr[prod_option2] != 0 && ($ordarr[selopt2] == null || $ordarr[selopt2] == 0)) $dirty = 1;
if ($ordarr[prod_option3] != 0 && ($ordarr[selopt3] == null || $ordarr[selopt3] == 0)) $dirty = 1;
if ($dirty == 1){
$img = $sszdir.'prodimages/'.$ordarr['Product_ID'].'-'.$ordarr['DispImage'];
$img = imageType($img);
if (!file_exists($img)) {$img = $sszdir.'prodimages/'.$ordarr['Product_ID'].'-'.$ordarr['DispImage']; $img = imageType($img);}
$optform .= '<table width="100%" cellspacing="2" border="0"><tr><td align="center"><hr><img src="'.$img.'" border="0"><br><font size="3"><b>'.$ordarr['ProdName'].'</b></font></td></tr><tr><td align="center">'.showOptions($ordarr['Product_ID'],true).'</td></tr></table>';
}
}
if ($dirty == true) $hr = '<hr>';
return $optform.$hr;
}
Go to http://www.estorebiz.com/art and click on the Apparel department, then hit "Add to cart" for some of the lingerie that's displayed in that department. After you've added one or two items, click on the "checkout" link up top. Instead of going to checkout, it will display a screen with some echo's at the top and a list of the products you selected with the options for each product under the image. Select the options for one of the products and hit submit. What will then happen is not supposed to happen. What's supposed to happen is the program puts the info in the DB and then it checks to see if there are any products in your order left that you have not selected options for. Once it has confirmed that all of the options are filled, it takes you to checkout. What's really happening is that the program adds the info in the DB (hence the printouts at the top of the screen) but it takes you back to the same page and displays the products over again even though you have already entered the options for those products in the DB.
Believe me, I wouldn't have asked for help on a message board about this if I hadn't looked at it as many ways as I could.
Whoever can give me why the logic is not working is a genius.
Thanks,
~TaxDAY - Just a little more insane today than most days.
To further clarify what's being printed out at the top of the screen on the select options page: they represent the actual fields from the DB... selprod1, 2, and 3 store the actual option that you selected, and prod_option1, 2, and 3 store the ID of that option. It works like this because the option system is dynamic so that any option can be made for any product. There are three options that can exist for any product and if any of the if statements resolve $dirty as 1 or true, then that means the product does not have it's options selected. To break the if statement down into two parts, the first part (thing1) says (in laymans terms): if the product has an option, and the second part says: and if the option is not filled in (in other words equal to 0 or null) then set the variable $dirty = 1. If the program gets through all three options without dirty being set to 1, that means that the product already has options selected and it should not be shown on the product options screen as it is already handled. The real logical hiccup is in those if statements.
if ($ordarr[prod_option1] != 0 && ($ordarr[selopt1] == null || $ordarr[selopt1] == 0)) $dirty = 1;
I'll refer to the two parts of the if statement as thing1 and thing2
if (thing1 = true and thing2 = true) then $dirty = true, and the system should display the product.... but if either thing one or two are false, then dirty should equal false for that product. It just doesn't make any sense.