Jump to content

Displaying data in to a menu form element


bullbreed

Recommended Posts

HI.

 

I had help from the forum the other day about inserting data from the database in to a menu form field

 

This was the code I was taught and it works.

However, If I have 2 articles in the database with the same category  the menu shows the category twice.

e.g

Category----------------Article

Animals----------------------Cats

Animals----------------------Dogs

 

Category----------------Article

Vegetables----------------------Carrotts

 

 

The Menu Form Field  Shows

Animals

Animals

Vegetables

 

 

<div class="infowrap">
<?php
      //Open the database
            mysql_connect("localhost","root","");
            mysql_select_db("********"); //Select the database
                                                
            $sql = "SELECT DISTINCT(article_section) FROM articles";
            $query = mysql_query($sql)or die;(mysql_error());
                  if(mysql_num_rows($query) > 0){
?>
<label>
      <select name="exist_cat" id="exist_cat">
            <option selected></option>
<?php
      while($r = mysql_fetch_array($query)){
      echo '<option value=" ' .$r['article_section'] . '">' . $r['article_section'] . '</option>';
      }
?>
</select>
<?php
}
?>
</label>
</div>

 

How do I restrict the output to show the category only once. Because if I have 50 articles in the animal section the menu form element will show Animals 50 times

 

Hi bullbreed,

I set up a quick db and created an articles table with 3 records, one of which I duplicated.

When I ran the script I only got one option for 'Animals', so I reckon the script is fine. I'd have a closer look at the database or better still post a dump of it here.

Fergal

 

HTML  result of script:

 

<div class="infowrap">

<label>

      <select name="exist_cat" id="exist_cat">

            <option selected></option>

<option value=" Animals">Animals</option><option value=" Vegtables">Vegtables</option></select>

</label>

</div>

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.