www.webdeveloper.com
Results 1 to 7 of 7

Thread: FOREACH beginner

  1. #1
    Join Date
    Mar 2012
    Posts
    22

    FOREACH beginner

    Hi there.

    I have a MySQL query that checks a "Compatible" column for a number. For instance "1".

    In this table there are 4 rows: Type, Compatible, StockLevel and Cost.

    I need to display the Type field of every row in Compatible which matches the number 1.

    EG:

    TYPE COMPATIBLE
    [Honda] - [1] - 0 - 1
    [Ford] - [1] - 0 - 1


    Currently I have

    Code:
    $cartridge_query = mysql_query("SELECT * FROM cartridges WHERE Compatible LIKE '1'");
    $cartridge_result = mysql_fetch_assoc($cartridge_query);
    $cartridge_count = mysql_num_rows($cartridge_query);
    Code:
    echo "<select>";
    
    foreach ($cartridge_result["Compatible"] as '1') {
    echo "<option>".$cartridge_result["Type"]."</option>";
    
    }
    
    echo "</select>";
    I have a feeling I'm close but it's not working. Any ideas?

  2. #2
    Join Date
    Feb 2012
    Posts
    218
    You are wrong using foreach. It should be
    PHP Code:
    foreach($cartridge_result as $cr){
       if(
    $cr['Compatible']==1){
           echo 
    "<option>".$cr["Type"]."</option>";
       }


  3. #3
    Join Date
    Mar 2012
    Posts
    22
    Quote Originally Posted by hyperionXS View Post
    You are wrong using foreach. It should be
    PHP Code:
    foreach($cartridge_result as $cr){
       if(
    $cr['Compatible']==1){
           echo 
    "<option>".$cr["Type"]."</option>";
       }

    OOH I get it!

    I had to get my mind wrapped around a while script and it clearly influenced what I was trying with foreach. That makes sense though!


    OK I have tried this and I am using:

    PHP Code:
    $cartridge_query mysql_query("SELECT * FROM cartridges");
    $cartridge_result mysql_fetch_assoc($cartridge_query);
    $cartridge_count mysql_num_rows($cartridge_query); 
    PHP Code:
    echo "<select>";

        foreach(
    $cartridge_result as $cr){ 
             if(
    $cr['Compatible']==$_POST["printer"]){ 
             echo 
    "<option>".$cr["Type"]."</option>"
             } 
            } 

    echo 
    "</select>"
    Where am I going wrong? My brain hasn't switched on this morning :/
    Attached Images Attached Images
    • File Type: jpg 5.JPG (30.9 KB, 18 views)

  4. #4
    Join Date
    Mar 2012
    Posts
    22
    BTW $_POST["printer"] is 1/2/3/4/5 etc... just one value.

    In the option field I am not seeing any names, just the printer ID.
    Attached Images Attached Images
    • File Type: png 6.png (6.4 KB, 17 views)

  5. #5
    Join Date
    Feb 2012
    Posts
    218
    Code:
    $cartridge_query = mysql_query("SELECT * FROM cartridges");
    $cartridge_result = array();`
    while ($row = mysql_fetch_assoc($cartridge_query)) {
    	$cartridge_result[] = $row;
    }
    PHP Code:
    echo "<select>"

    foreach(
    $cartridge_result as $cr){ 
        if(
    $cr['Compatible']==$_POST["printer"]){ 
            echo 
    "<option>".$cr["Type"]."</option>"
        } 
    }

    echo 
    "</select>"

  6. #6
    Join Date
    Mar 2012
    Posts
    22
    Thats great, thank you. It works!

    I forgot one important element though which is that I was using the explode function, because in the table some cartridges are compatible with several printers.

    Take these for insance:

    HP xx BLACK
    HP xx CYAN
    HP xx YELLOW

    In the compatible field may be "1, 9" (Printer ID #1 and Printer ID #9). The array you wrote makes me think this isn't possible on top?

  7. #7
    Join Date
    Feb 2012
    Posts
    218
    I guess you can use explode and in_array

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles