简体   繁体   中英

Put 2 info from different cols in 1 dropdown menu

I want to put 2 information in 1 drop-down menu. I'm doing a school system. So for classes drop-down menu, I would like to put [classname (year)] in the drop-down-menu. The information were in a same table but different cols. I can only manage to display the class id / class name only.

 <tr>
  <td>Class</td>
  <td>
    <select name="classname" id="textbox">
     <option>---- Class Name   ------</option>
       <?php
         $class_name=mysql_query("SELECT * FROM class_tbl");
         while($row=mysql_fetch_array($class_name)){
       ?>
     <option value="<?php echo $row['class_name'];?>"> <?php echo $row['class_id'];?> </option>
       <?php 
         }
       ?>
    </select>
   </td>
 </tr>

You should concat the 2 information in that field.

Do like this:

<option value="<?php echo $row['class_id'];?>"> <?php echo $row['class_name']." (".$row['year'].")";?> </option>

Write your table columns in $row array in place of my code.

Let me know for further help.

I cannot see naything wrong, are you sure you are not just mispelling some syntax?

<tr>
  <td>Class</td>
    <td>
     <select name="classname" id="textbox">
      <option>---- Class Name   ------</option>
       <?php
         $class_name=mysql_query("SELECT * FROM class_tbl");
         while($row=mysql_fetch_array($class_name)){
       ?>
      <option value="<?php echo $row['class_id'];?>"><?php echo $row['class_name']."(".$row['year'].")"; ?>  </option>
        <?php 
          }
        ?>
     </select>
   </td>
 </tr>

As you see i swapped the id and description position.

In terms of displaying class name you need to swap positions of id and name.This should be the basic structure if you want to display name and get the selected id.

<option value="option_id">Option Name</option>

So for your problem it should be something like this.

 <tr>
  <td>Class</td>
  <td>
    <select name="classname" id="textbox">
     <option>---- Class Name   ------</option>
       <?php
         $class_name=mysql_query("SELECT * FROM class_tbl");
         while($row=mysql_fetch_array($class_name)){
       ?>
     <option value="<?php echo $row['class_id'];?>"> <?php echo $row['class_name'];?> </option>
       <?php 
         }
       ?>
    </select>
   </td>
 </tr>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM