Jump to content

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


Phaelon

Recommended Posts

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??

Link to comment
Share on other sites

What is you table structure like? If you are storing the product type in a separate column then you can do select distinct, example

$result = $mysqli->query('SELECT DISTINCT product_type FROM products');
if($result) {

   echo '<select name="type">';
   while(list($type) = $mysqli->fetch_row()) {
       echo "<option>$type</option>";
   }
   echo '</select>';
}
Link to comment
Share on other sites

Oh, its ok.. I came up with a way:

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.