tags:

views:

148

answers:

4

The code below is just a sample of the format I have

 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($idc && $ids=null) {
           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);
A: 

Well the code you've posted is sound, so it's probably a problem with the variables getting to that script in the first place. Are you sure those variables are defined in the GET string?

Dump out the contents of it to make sure:

print_r($_GET);
nickf
one is set in the GET string and another is in the POST sorry, I have tried putting one int he GET string and another in the POST string respectively but it won't work.
jona
Please take a look at the edited script above.
jona
A: 

Try this:

$ids= isset($_GET['ids'])?intval($_GET['ids']):null;

$idc= isset($_GET['idc'])?intval($_GET['idc']):null;


if (isset($ids)) { 
    //display something
} 
elseif(isset($idc)) {
    //display something different
}
else 
{ 
    //display nothing has passed 
}
mattbasta
that's doing kind of the same thing as posted, but worse. Firstly, `(int)` is faster than `intval`, and secondly you've broken the logic since `$ids = 0` will now pass when it didn't before.
nickf
@mattbasta I have tried the above but there is part of the script where I think some operators to add the idc variable. look at the edit script above.
jona
@nickf My understanding was that if `ids` or `idc` were present, it was a matter of processing them. If they weren't to be processed, they wouldn't be included in the query string. Also, `intval` produces more consistent results (particularly across PHP 4/5), and two calls to it won't be significant enough to slow down any application. Speed is a nonissue.
mattbasta
A: 

I tested your script above,it works as expected.Maybe you should check whether the link is correct or not.

SpawnCxy
I have set up some differences in links coming from page1.php different from the link coming from page2.php. The only difference is the ids and idc variable
jona
jona
jona
see the only differences is the idc and ids
jona
don't know why it always go to the else statement ignoring the values of the idc and ids.
jona
not sure but try check the rewrite rule in your server.
SpawnCxy
did you try to print the GET string in cart.php as nickf suggested?
SpawnCxy
I have print_r throught the whole code and the variables GET and POST has print threir values, don't know why the if and elseif are not getting the isset fucntion as it's argument look at the edited script above
jona
+2  A: 

Jona, if you'd ever bother to properly format your code, you'd see that the 'else' in question is WITHIN A FUNCTION, and you're defining $ids and $idc OUTSIDE THE FUNCTION. Remember, in PHP, global variables (except the super-globals, such as $_GET, $_POST, etc...) are not visible within functions unless you explicity define them as globals within the function.

Add global $idc, $idc; as the first line in the function definition and your if() will start working correctly.


Followup:

Your code is still hideously formatted, and very wonky. Take this:

isset($_GET['ids'])?$ids=$_GET['ids']:null;

You're using a trinary operator, but not assigning its results anywhere, and using the 'true' condition to do an assignment. This is an ugly hack. It should be written like this:

 $ids = isset($_GET['ids']) ? $_GET['ids'] : null;

This way $ids will be set to null if there is no $_GET['ids']. Which brings up the fact that you're assigning a null instead of some other default value. If there really was no $_GET['ids'], then this:

 $idx = $_GET['ids'];

would work identically, as PHP will automatically assign a null in situations where the right-hand-side doesn't exist. Of course, you still have to sanitize this value, since you're using it in an SQL query later on. Leaving it like this will just invite SQL injection attacks and all kinds of other abuses.

Beyond that, you're still creating $ids and $idc OUTSIDE of your ShowCart() function. As such, $ids and $idc within the function will be automatically be created with null values, since you've not declared them to be global. I think by now it's obvious you have no idea what this means, so try out this piece of code:

<?php

$var = 'Here I am!';

function showVar() {
    echo "Within showVar(), var is set to: $var\n";
}
function workingShowVar() {
    global $var;
    echo "Within workingShowVar(), var is equal to: $var\n";
}

showVar();
workingShowVar();

If you copy/paste this code, run it, you'll see the following output:

Within showVar(), var is set to:
Within workingShowVar(), var is set to: Here I am!

Notice that both functions are identical, except for the global declaration. If you can figure out the difference, then you'll realize you need to re-write your ShowCart() function as follows:

function ShowCart() {
     global $ids, $idc;
     // rest of your code here
}
Marc B
Thanks for the explanation. Instead of using the function global which is a another way I haven't try I assigned the $ids and $idc as the ShowCart() function arguments like ShowCart($ids, $idc) but I will use your method now that assigning them as the arguments of the ShowCart() function didn't quite worked. I will give some feedback later.
jona
jona
Well, what priority do you want things done at? You've got two variables, which means four possibilities:1. $ids is something, $idc is null2. $ids is null, $idc is something3. $idc and $ids are both null4. $idc and $ids are both something (maybe not the same something).Which cases do you want to handle?
Marc B