Current location: Hot Scripts Forums » Programming Languages » PHP » Populating a dropdown using data from mysql table


Populating a dropdown using data from mysql table

Reply
  #1 (permalink)  
Old 06-05-09, 03:31 PM
DimensionX DimensionX is offline
Newbie Coder
 
Join Date: May 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Populating a dropdown using data from mysql table

Hi,

I'm trying to make a dropdown box pull it's contents from a table called client but the page isn't loading it just goes blank and without the dropdown it loads fine so I've obviously messed this up, it connects to the database ok but just doesn't like what i did with the drop down.

PHP Code:

<td>Referred by:</td><td><select name="referredBy" tabindex="11">

            <option value="">Please select a client or the value None</option>
            <option value="None">None</option>
            
            <?php
            
//Assign the query
            
$query "SELECT * FROM client ORDER BY clientId ASC";

            
//Execute the query
            
$result mysql_query$query );
            if(!
$result){
                die (
"Could not query the database: <br />"mysql_error());
            }
            
            while (
$result_row mysql_fetch_array($resultMYSQL_ASSOC)){
            {
                echo 
'<option value="' $result_row["clientId"] . '">'$result_row["clientId"] . ' ' .                   $result_row["firstName"] . ' ' $result_row["surName"]  . '</option>';
            }
            
            
?>
            </select></td>
any help would be greatly appreciated, I'm still really new at php so I'm sorry if my code isn't clearly laid out.

thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-06-09, 02:49 AM
DimensionX DimensionX is offline
Newbie Coder
 
Join Date: May 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
[Solution] but still have a quick question

I found the solution after going through many many sites and getting nothing then coming back and combing through this one and finally finding the solution lol, I had to adapt it a bit but it works:

PHP Code:

<table>

        <tr>
            <td>Referred by:</td><td><select name="referredBy" tabindex="11">
            <option value="">Please select a client or the value None</option>
            <option value="None">None</option>
            
            <?php
            
//Assign the query
            
$query="SELECT `clientId`, `firstName`, 'surName' FROM `client` ORDER BY `clientId` ASC";

            
//Execute the query
            
$result mysql_query$query );
            if(!
$result){
                die (
"Could not query the database: <br />"mysql_error());
            }
            
            while (
$data=mysql_fetch_assoc($result)){
                
                echo 
"<option value = \"{$data[clientId]}\" >{$data[clientId]} {$data[firstName]} {$data[surName]}</option>\n";
            } 
            
?>
            </select></td>
        </tr>
    </table>
Now I'm still not quite sure, I mean I'm glad it works but I'ld like to understand the code. why is there a slash here \"{$data[clientId]}\" ....or is it a combination fo the \ and " that allows a quote to be used without interrupting the code? I understand the rest just not that bit.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-12-09, 11:30 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Quote:
Originally Posted by DimensionX View Post
I found the solution after going through many many sites and getting nothing then coming back and combing through this one and finally finding the solution lol, I had to adapt it a bit but it works:

PHP Code:

<table>

        <tr>
            <td>Referred by:</td><td><select name="referredBy" tabindex="11">
            <option value="">Please select a client or the value None</option>
            <option value="None">None</option>
            
            <?php
            
//Assign the query
            
$query="SELECT `clientId`, `firstName`, 'surName' FROM `client` ORDER BY `clientId` ASC";

            
//Execute the query
            
$result mysql_query$query );
            if(!
$result){
                die (
"Could not query the database: <br />"mysql_error());
            }
            
            while (
$data=mysql_fetch_assoc($result)){
                
                echo 
"<option value = \"{$data[clientId]}\" >{$data[clientId]} {$data[firstName]} {$data[surName]}</option>\n";
            } 
            
?>
            </select></td>
        </tr>
    </table>
Now I'm still not quite sure, I mean I'm glad it works but I'ld like to understand the code. why is there a slash here \"{$data[clientId]}\" ....or is it a combination fo the \ and " that allows a quote to be used without interrupting the code? I understand the rest just not that bit.
Sorry I missed your post.

You are correct in the the slash \ escapes the ". if the echo statement starts with a single quote ' you would not have to escape the double quote " and vice versa.

The fun is when your trying to build multiple select boxes from a query... but if you search hard enough, you can find that too.

or,

http://www.hotscripts.com/forums/php...ic-titles.html
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help extracting data from html and writing it to mysql? method PHP 2 06-12-08 12:53 PM
Moving data to another mySQL table zoliky Database 3 10-04-06 06:53 PM
populating a dropdown list (mysql) HeX Productions Script Requests 2 09-30-06 05:57 AM
Inserting data to mysql table using php Tjobbe PHP 4 09-28-06 07:37 AM


All times are GMT -5. The time now is 09:53 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.