co.ador Posted February 21, 2010 Share Posted February 21, 2010 how can I direct the code to display the if or the else? Well users are going to come from either page1.php or page2.php url coming from page1.php cart.php?ids=1 url coming from page2.php cart.php?idc=1 for instance if the url is coming from page1.php then cart is going to receive it like $ids= isset($_GET['ids'])?(int) $_GET['ids']:null; $idc= isset($_GET['idc'])?(int) $_GET['idc']:null; if ($ids) { display something } elseif($idc) { display something different } else { display nothing has passed } Those are the conditions I have right but it is not working properly since it will display the else message "display nothing has passed". Again it will display the else statement even if I come from page1.php or page2.php. I guess the conditions are not well set up or it is passing empty. How can I set up the conditions so it can display either $ids statement or the elseif $idc. Quote Link to comment https://forums.phpfreaks.com/topic/192785-help-with-a-if-and-else-statement/ Share on other sites More sharing options...
teamatomic Posted February 21, 2010 Share Posted February 21, 2010 You need to set a value in your conditional statement because it returns 1 for true or 0 for false. That is why you drop through. isset($_GET['ids'])?$ids=$_GET['ids']:null; $ids is now the value of $_GET['ids'] HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/192785-help-with-a-if-and-else-statement/#findComment-1015503 Share on other sites More sharing options...
co.ador Posted February 21, 2010 Author Share Posted February 21, 2010 Teamatomic instead of have this ====> $ids= isset($_GET['ids'])?(int) $_GET['ids']:null; $idc= isset($_GET['idc'])?(int) $_GET['idc']:null; have something like this on top? ===> isset($_GET['ids'])?$ids= $_GET['ids']:null; $idc= isset($_GET['idc'])?$idc= $_GET['idc']:null; Quote Link to comment https://forums.phpfreaks.com/topic/192785-help-with-a-if-and-else-statement/#findComment-1015505 Share on other sites More sharing options...
teamatomic Posted February 21, 2010 Share Posted February 21, 2010 if the ids line is not correct it stands to reason that the idc line also needs correcting. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/192785-help-with-a-if-and-else-statement/#findComment-1015510 Share on other sites More sharing options...
co.ador Posted February 21, 2010 Author Share Posted February 21, 2010 sorry this is what I have isset($_GET['ids'])?$ids=$_GET['ids']:null; isset($_POST['idc'])?$idc=$_POST['idc']:null; Quote Link to comment https://forums.phpfreaks.com/topic/192785-help-with-a-if-and-else-statement/#findComment-1015511 Share on other sites More sharing options...
teamatomic Posted February 21, 2010 Share Posted February 21, 2010 That should work OK. Now either $ids or $idc will have a value and the other will be empty. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/192785-help-with-a-if-and-else-statement/#findComment-1015515 Share on other sites More sharing options...
co.ador Posted February 21, 2010 Author Share Posted February 21, 2010 Pardon Teamatomic I don't know if having a function plus a while loop wrapping the if and else statement will stop the variables $idc and $ids passing their values to the if and else statement? Let me explain my self the following structure is what I have besides the else and if statements. isset($_GET['ids'])?$ids=$_GET['ids']:null; isset($_POST['idc'])?$idc=$_POST['idc']:null; function ShowCart() { $que = "SELECT FROM cart LEFT OUTER JOIN ... ON ... LEFT OUTER JOIN... ON... WHERE "; $result = mysql_query($que); while(){ if (isset($ids)) { display something for (){ if(){ } // end of if inside the for loop }// end of for loop }elseif(isset($idc)) { display something different }else{ display nothing has passed } }//end of while loop }//end of showcart(); function that's the formatting above I wonder why the if and elseif are not getting the isset() as the if and elseif argument. I have debug the through the whole code and the print_r of GET and POST has values through the whole code. print_r($_POST); print_r($_GET); Quote Link to comment https://forums.phpfreaks.com/topic/192785-help-with-a-if-and-else-statement/#findComment-1015669 Share on other sites More sharing options...
wildteen88 Posted February 21, 2010 Share Posted February 21, 2010 Functions have their own variable scrope. Which means any variables defined outside of them will not be accessible. When using variables with functions you should pass them as arguments. For example $ids= isset($_GET['ids'])?(int) $_GET['ids']:null; $idc= isset($_POST['idc'])?(int)$_POST['idc']:null; function ShowCart($ids, $idc) { // $ids and $idc are not numbers if(!is_numeric($ids) || !is_numeric($idc)) { // exit the function, return false return false; } // continue with function } Quote Link to comment https://forums.phpfreaks.com/topic/192785-help-with-a-if-and-else-statement/#findComment-1015677 Share on other sites More sharing options...
co.ador Posted February 21, 2010 Author Share Posted February 21, 2010 Thank you very much. I have a question why have you do a return false; inside the if statement what does that function is saying? That it returns or send back the arguments of the if statment false outside the if statement? if(!is_numeric($ids) || !is_numeric($idc)) { // exit the function, return false return false; } Quote Link to comment https://forums.phpfreaks.com/topic/192785-help-with-a-if-and-else-statement/#findComment-1015685 Share on other sites More sharing options...
wildteen88 Posted February 21, 2010 Share Posted February 21, 2010 What the above code does is check to see that the arguments passed to the function (in your case the variables $ids and $idc) hold a numeric value. If they both don't hold a numeric value then the function will return a false value. No code after the if will be processed however if they both hold a numeric value then the function will ignore the code in the if statement. To understand how return works please read the documentation here. You may also find the following useful. http://uk3.php.net/manual/en/language.functions.php Quote Link to comment https://forums.phpfreaks.com/topic/192785-help-with-a-if-and-else-statement/#findComment-1015689 Share on other sites More sharing options...
co.ador Posted February 21, 2010 Author Share Posted February 21, 2010 thank you for the explanation and then the references! wildteen88 Quote Link to comment https://forums.phpfreaks.com/topic/192785-help-with-a-if-and-else-statement/#findComment-1015692 Share on other sites More sharing options...
co.ador Posted February 21, 2010 Author Share Posted February 21, 2010 // $ids will extract the value from the ids if coming from page1.php and then $idc will do the same if coming from page2.php $ids=isset($_GET['ids'])?(int)$_GET['ids']:null; $idc=isset($_POST['idc'])?(int)$_POST['idc']:null; switch($_GET["action"]) { case "add_item": { AddItem($_GET["ids"], $_POST["idc"], $_GET["qty"]); ShowCart($ids, $idc); break; } case "update_item": { UpdateItem($_GET["ids"], $_POST["idc"], $_GET["qty"]); ShowCart(); break; } case "remove_item": { RemoveItem($_GET["ids"],$_POST["idc"], $_GET["id"]); ShowCart(); break; } default: { ShowCart(); } } function AddItem($itemId, $itemId, $qty){ $result = mysql_query("SELECT COUNT(*) FROM cart WHERE cookieId = '" . GetCartId() . "' AND id = $itemId"); $row = mysql_fetch_row($result); $numRows = $row[0]; if($numRows == 0) { // This item doesn't exist in the users cart, // we will add it with an insert query mysql_query("INSERT INTO cart(cookieId, id, qty) values('" . GetCartId() . "', $itemId, $qty)"); //printf ("Inserted records: %d\n", mysql_affected_rows()); } else { // This item already exists in the users cart, // we will update it instead mysql_query("UPDATE cart SET qty = $qty WHERE cookieId = '" . GetCartId() . "' AND id = $itemId"); } } function UpdateItem($itemId, $qty) { mysql_query("UPDATE cart SET qty = $qty WHERE cookieId = '" . GetCartId() . "' AND id = $itemId"); //printf ("Updated records: %d\n", mysql_affected_rows()); } function RemoveItem($itemId) { mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() . "' AND id = $itemId"); } I would like to slow down and go step by step to see what's really going on in the code if you guys permit it. Above 1- first the extraction of the variable values $ids and $idc 2- I would say a set up of the functions add item, update and remove. I know so far I tried to pass the values of ids, idc and qty to functions add item, update and remove. But I don't know if that's properly set up. Another thing if you can check the qty value is only added to the update function. The ids and idc values will be later used in the if and else statement i have posted in the first post. But for now I want to go step by step to see If I can find the root of the problem why is not displaying. @Wildteen88 when you first gave me the code it displayed at the beggining but then it didn't it disappear strangely don't know why I suspect that the query inside the the AddItem function is not retreiving the data from the cart table. That's what is happening don't know why. I think I have not set up well enough the arguments in the AddItem functions $itemId, $itemId, $qty. I don't know help... Quote Link to comment https://forums.phpfreaks.com/topic/192785-help-with-a-if-and-else-statement/#findComment-1015736 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.