Jump to content

help with a if and else statement


co.ador

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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);

 

 

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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;
    }
   

Link to comment
Share on other sites

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

Link to comment
Share on other sites

// $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...

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.