Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2014
    Posts
    74
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Creating a single variable for multiple MySQL returns of the same data

    Hey all,

    I'm creating a search page and on it people can search by brand. They can do this by toggling a SELECT OPTION

    <SELECT>

    <OPTION>BRAND A</OPTION>
    <OPTION>BRAND B</OPTION>
    <OPTION>BRAND C</OPTION>

    </SELECT>

    What I am wanting to do is make these OPTIONS dynamic and only include brands that I will actually be carrying.

    Is there a way to do a lookup on a MySQL table of products and for each different type of data, to generate a <OPTION></OPTION> option for it?

    Like.. a foreach different type of data. The thing I am having a problem with is that there are several different types of products of the same category, such as 'product 1' and 'product 4' both being T-Shirt's, I don't want to have two options as <OPTION>T-Shirt</OPTION><OPTION>T-Shirt</OPTION>.

    Can what I want to do even be done??

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,076
    Thanks
    23
    Thanked 592 Times in 591 Posts
    Since this is in the php section I assume your getting the correct results from your query. $result = "SELECT products FROM table WHERE category = 'T-Shirt'

    echo "T-Shirts : <select>";
    $i=0;
    while($row = mysqli_fetch_row($result))
    {
    echo "<option value = $row[$i]>";
    echo "$row[$i]";
    echo "</option>";
    $i++;
    }
    echo "</select>";

    Or something close to that.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    Phaelon (05-12-2014)

  • #3
    Regular Coder
    Join Date
    Oct 2004
    Posts
    356
    Thanks
    0
    Thanked 16 Times in 16 Posts
    What columns are in your database table? You might want SELECT DISTINCT. Something like
    Code:
    SELECT DISTINCT product_category FROM products
    should list all the product categories but only once.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,076
    Thanks
    23
    Thanked 592 Times in 591 Posts
    Maybe I didn't read that right "I don't want to have two options as T-Shirts" List them by brand name as 'Hanes T-Shirt' or their style like 'Owl T-Shirt' or 'Red T-shirt'
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #5
    New Coder
    Join Date
    Jan 2014
    Posts
    74
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Yeah you read it wrong sunfighter, but you gave me an idea on how it can be done..

    PHP Code:
    while ($row mysql_fetch_row($result)) {
        if (empty(
    $option'.$row['type'].')) {
            echo 
    '<OPTION>'.$row['type'].'</OPTION>';
            
    $option'.$row['type'].' 1;
        }

    Victory!

    Thanks again sunfighter.

  • #6
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,076
    Thanks
    23
    Thanked 592 Times in 591 Posts
    Er, welcome, even if I don't know what I did.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #7
    New Coder
    Join Date
    Jan 2014
    Posts
    74
    Thanks
    11
    Thanked 0 Times in 0 Posts
    You used a counter, which gave me the idea of using a dummy variable to stop a repeated <OPTION></OPTION> and achieve exactly what I was looking to do. Although I'm not using one, it some how gave me the idea to come up with what I did, probably because I set dummy variables to 1, and your counter reminded me of this................... If all that makes sense!!

    See, I'm gonna have more than 1 product type return, and I don't want <OPTION>PRODUCT TYPE A</OPTION><OPTION>PRODUCT TYPE A</OPTION><OPTION>PRODUCT TYPE A</OPTION><OPTION>PRODUCT TYPE A</OPTION>.

    See what I did above, it will stop that.
    Last edited by Phaelon; 05-12-2014 at 05:27 AM.

  • #8
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,076
    Thanks
    23
    Thanked 592 Times in 591 Posts
    Thanx understand
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Posting Permissions

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