SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    only 2 options should get echo

    hi all

    there are 2 products inserted in my cart table.
    1) product 1094
    2) product 1095

    i want to echo the "message" according to their existence in the cart table.

    but my code echos all 4 messages.


    PHP Code:
    <?
    $qry 
    "select * from cart_table";
    $result mysql_query($qry);
    while(
    $row=mysql_fetch_array($result))
            {
                                 
                if(
    $row['product_id'] == 1094)
                {
                echo 
    "1094 is already there";
                }
                
                if(
    $row['product_id'] != 1094)
                {
                echo 
    "1094 is not there";
                }
                                     
                if(
    $row['product_id'] == 1095)
                {
                echo 
    "1095 is already there";
                }
                
                if(
    $row['product_id'] != 1095)
                {
                echo 
    "1095 is not there";
                }
                                 
            }
    ?>
    out of 4 only 2 message should be displayed

    vineet

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Why should the "____ is not there" message be displayed? There is nothing in this code that checks the WHY. Once that's resolved (and the code updated correctly) it will only display 2/4 at any given time.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wonshikee View Post
    Why should the "____ is not there" message be displayed? There is nothing in this code that checks the WHY. Once that's resolved (and the code updated correctly) it will only display 2/4 at any given time.
    hi wonshikee

    when a product is added in the cart table then its product_id gets added

    so i am checking if the product_id=1094 doesnt exists then the message should say "1094 is not there"

    vineet

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Why not just select the fields you need from the outset?

    PHP Code:
    $tgt = array(10941095);
    $qry "select product_id from cart_table where product_id IN (".join(','$tgt).")";


     
    // echo $qry; // debug

    // spoofing your result set
    $rows[0]['product_id'] = 1000;
    $rows[1]['product_id'] = 1094;

    foreach( 
    $rows as $row ){
      if( 
    in_array($row['product_id'], $tgt ) ) {
        echo 
    $row['product_id'] . " is in the table";
      }else{
        echo 
    $row['product_id'] . " is NOT in the table"
      }


  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,122
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Edit:

    Seems several others have posted along these lines too, this just explains why your original code is flawed, with a bit of logical thinking


    Look at your logic and think it over again.

    First let's consider the fact that you can simply your IF statements to
    PHP Code:
                if($row['product_id'] == 1094)
                {
                echo 
    "1094 is already there";
                } else {
                echo 
    "1094 is not there";
                }
                                     
                if(
    $row['product_id'] == 1095)
                {
                echo 
    "1095 is already there";
                } else {
                echo 
    "1095 is not there";
                } 
    Now what if your cart_table contains the following product_ids: 1094 and 1095

    When you loop through your records, you can easily see why it prints out all 4 messages. First record will produce 1094 is there and 1095 is not there, Second record will produce 1094 is not there and 1095 is there.

  6. #6
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Why not just select the fields you need from the outset?

    PHP Code:
    $tgt = array(10941095);
    $qry "select product_id from cart_table where product_id IN (".join(','$tgt).")";


     
    // echo $qry; // debug

    // spoofing your result set
    $rows[0]['product_id'] = 1000;
    $rows[1]['product_id'] = 1094;

    foreach( 
    $rows as $row ){
      if( 
    in_array($row['product_id'], $tgt ) ) {
        echo 
    $row['product_id'] . " is in the table";
      }else{
        echo 
    $row['product_id'] . " is NOT in the table"
      }

    thanks cups for this solution.

    i learned about in_array() from you and used it in my another code also.
    its a good function

    vineet

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Good for you, just try and pass the knowledge on one day, and lets try and keep the PHP community awesome!

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Cups, your... example doesnt work?

    $tgt = array(1094, 1095);
    $qry = "select product_id from cart_table where product_id IN (".join(',', $tgt).")";

    // spoofing your result set
    $rows[0]['product_id'] = 1000; <-- this row would NOT be returned by the query you just gave.
    $rows[1]['product_id'] = 1094;

    If the table contained entries for 1094 and 1095, and you searched for 1000 and 1094, the code would only spit out "1094 is in the table".
    My understanding of the OP was that he wanted a response for all search items. (If this is incorrect, let me know lol)

    Consider implementing array_intersect and array_diff.
    PHP Code:
    //Assume table holds entries for 1094,1095.
    $srch = array(1000,1094);
    $qry "select product_id from cart_table where product_id IN (".join(','$srch).")";

    //fetch_assoc the results, stripping the values into $rows.
    //$res = $db->query($qry);
    //while($row = $res->fetch_assoc()) {
    // $rows[] = $row['product_id'];
    //}
    //Spoofing the result:
    $rows[0] = 1094;
    //Only one result, because 1000 isnt in the table.
    $in_table array_intersect($srch,$rows);
    $out_table array_diff($srch,$rows);
    if(
    count($in_table) > 0) {
     echo 
    implode(',',$in_table)." are in the table already.<br />";
    }
    if(
    count($out_table) > 0) {
     echo 
    implode(',',$out_table)." are not in the table already.<br />";

    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Ok, thanks, I remember dashing that off -- I should have created some better test cases for myself.

    I think I just wanted to question the whole logic of "selecting * from the db", and then got a bit carried away.

    Using array_intersect() looks better.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •