SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question grouping data in option list

    Situation (simplified):

    mysql database with following fields:
    - product: 10,11,12,13
    - type: shirt
    - size: s, m, l, xl

    Try to group above data by type into an option list.

    Code (simplified)

    define("SQL", "SELECT `product`, `type` , `size`, FROM `products` ORDER BY `type` ASC");

    $sql = mysql_query(SQL) or die(mysql_error());
    while ($result = mysql_fetch_assoc($sql)) {
    foreach($result as $result["type"]) {
    echo '<li>' . $result["product"] . '<br />';
    echo '<br /><select name="size">';
    echo '<br /><option value=" ' . $result["size"] . '">' . $result["size"] . '</option>\n';
    echo '</select>';
    echo '</li>';
    }
    }

    the foreach loop is not correct, I know since the output fron this code is not correct. Is there another statement I could use for this situation?

    Regards,
    Enno

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    looks like your SQL has a syntax error

    why don't you test the SQL statement directly in mysql, i.e. outside of php?

    you will learn that there is a "dangling comma" in front of the FROM keyword
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's right, that one appeared because i simplified the SQL statement. In the original code the there is no comma at that place just the FROM statement.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    well, i simplified my answer

    in the original answer, i explained the problem with your foreach loop





    just kidding


    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by r937 View Post
    well, i simplified my answer

    in the original answer, i explained the problem with your foreach loop


    You can't use the foreach to do what you want, because $result is a one dimensional array: it only contains all the fields of one row at a time.

    If you want to display the product name, and then a list of all available sizes, you'll have to write the code that confronts the product of the previous row with the one of the actual row, and display the product name every time a new product appears.

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    confronts compares
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •