Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Trouble populating a selectlist with values from a single record

Posted on 2013-12-02
4
243 Views
Last Modified: 2013-12-02
Hi,
I'm not sure if its obvious but I'm struggling with below. I'm querying the db to populate the select list looking to get both phone numbers from  a single record. My resulting selectlist only contains just one phone number instead of the leadPhone and leadOtherPhone for that record.

What am I missing and I'll go bang my head on a wall when its pointed out:
<select name="dialnumber" id="dialnumber">
<?php
$query = "SELECT leadPhone, leadOtherPhone FROM leads WHERE user_id = '$current_id' AND id = '$thelead'";
$result = mysql_query($query) or die(mysql_error());
		echo("<option value=\"\">Choose a Number</option>");
		while($row = mysql_fetch_row($result)) {
		echo("<option value=\"$row[0]\">$row[0]</option>");
		}
?>

Open in new window


So if the record contained a number in each column... I'm only getting the first one populating my selectlist.
0
Comment
Question by:tjyoung
  • 3
4 Comments
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39689687
You can't loop through using a while loop because there's only 1 record!!

Try this:

$row = mysql_fetch_row($result);
printf("<option value='%s'>%s</option>", $row[0], $row[0] );
printf("<option value='%s'>%s</option>", $row[1], $row[1] );

Open in new window

0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39689708
It may make your code easier to read if you fetch an object rather than a row. You can then refer to the columns by name...

$row = mysql_fetch_object($result);
printf("<option value='%s'>%s</option>", $row->leadPhone, $row->leadPhone );
printf("<option value='%s'>%s</option>", $row->leadOtherPhone, $row->leadOtherPhone );

Open in new window

FYI - The mysql library is deprecated so sooner rather than later you should be switching to either the PDO or mySQLi libraries.

Ray Paseur, an Expert here on EE, has written a great article about it:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 39689762
Shoot me... it would be a mercy killing.
Thanks for the swat upside the head Chris.
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39689783
:)

We all need it from time to time...
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question