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

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>';
}

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;
    }
}  

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.