Jump to content

[SOLVED] Help with option box please!


littlevisuals

Recommended Posts

Hi all

 

How do I create an option dropdown using data from mysql?

 

I have 2 fields (artist_firstname, artist_lastname) I need merging together in this box (as well as ordering alphabetically).  I've tried this code and it doesn't work, but then again im not sure if im even selecting the tables correctly  :confused:

 

My database ARTISTS

 

tables in ARTISTS are "ARTISTS, CATORGORIES & GALLERY"

 

here is the code

 

<label for="artist">Artists</label><SELECT NAME="artist">

<?php
mysql_connect("localhost","root","password");
mysql_select_db("artists");
   
$q = "SELECT artist_firstname, artist_lastname FROM `artists` ORDER BY name ASC";
$r = mysql_query($q);
while($column = mysql_fetch_assoc($r)){
$artist_lastname = $column['id'];
$artist_lastname = $column['artist_lastname'];
echo '<OPTION VALUE="'.$artist_firstname.'">'.$artist_lastname.'</option>';
      }
?>

</SELECT>

 

How would I insert the data (artist_firstname + artist_lastname) into one field?

 

Sorry if its something stupid i've just started learning php/mysql!  :)

Link to comment
Share on other sites

<?php

mysql_connect("localhost","root","password");
mysql_select_db("artists");

$query = "SELECT id, CONCAT(artist_firstname, ' ', artist_lastname) as name
          FROM `artists` ORDER BY artist_lastname ASC";
$result = mysql_query($query);

$artist_options = '';
while($row = mysql_fetch_assoc($r))
{
    $artist_options .= "<option value=\"{$row['id']}\">{$row['name']}</option>\n";
}
?>
<label for="artist">Artists</label>
<SELECT NAME="artist">
<?php echo $artist_options; ?>
</SELECT>

Link to comment
Share on other sites

Sorry adding $result to the fetch did it  ;D

 

yeah, sorry, it is a pet peeve of mine when people use variable names that offer absolutely no value to identifying what the variable is. It may seem more efficient when initially writing the code, but it creates problems when having to edit the code later or if someone else needs to edit the code. So, I will sometimes go a little too far when editing peoples code on forums.

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.