Page 1 of 1
Unable to display drop down list
Posted: Thu Apr 05, 2012 3:37 pm
by Imtiyaaz
Hi
I need to display a list in my form retrieved from my db but cant seem to get it to work, below is my code
Code: Select all
<?php
error_reporting(0); // turn error messages off
include('user.php');
echo "<form Method='POST' ACTION='test.php'>";
echo "<select name='title' type='text' />";
echo "<option>".$row['first_name']."</option>";
echo "<option>".$row['first_name']."</option>";
echo "</select>";
echo "<br /><br />";
echo "<input type='submit' name='submit' value='Submit' />";
echo "</form>";
?>
code for user.php
Code: Select all
<?php
class User{
public function getUserById($user_id){
$sql = 'select first_name, last_name, display_name from users where user_id = '.$user_id;
$results = mysql_query($sql);
$row = mysql_fetch_array($results);
$this->user_id = $user_id;
$this->fist_name = $row['first_name'];
$this->last_name = $row['last_name'];
$this->display_name = $row['display_name'];
return true;
}
public function getDisplayName(){
return $this->display_name;
}
public function getFriends(){
$sql = 'select friend_id from user_friends where user_id = '.$this->user_id;
$results = mysql_query($sql);
$friends = array();
while($row = mysql_fetch_array($results)){
$friends[] = $row['friend_id'];
}
return $friends;
}
}
?>
Any assistance will be appreciated
Thanks
Re: Unable to display drop down list
Posted: Thu Apr 05, 2012 5:53 pm
by Celauran
Turn error reporting back on and tell us what errors you're getting. You've defined a user class, but I don't see it instantiated anywhere. Where is $row['whatever'] supposed to be coming from?
Re: Unable to display drop down list
Posted: Thu Apr 05, 2012 6:03 pm
by social_experiment
There is no function being called in the first code you pasted; also $row['first_name'] can only be used if you have called mysql_fetch_array() which hasn't happened yet. Which field in the database will create your list? Assume that it is 'name'; you would select the column from the database; the example below places all values from 'name' into an array and returns the array for later use
Code: Select all
<?php
// this function goes into the user class.
function retrieveNames()
{
$sql = "SELECT `name` FROM `table`";
$qry = mysql_query($sql);
while ($row = mysql_fetch_array($qry)) {
// place all 'name' values selected in an array called
// $names
$names[] = $row['name'];
}
return $names;
}
?>
Code: Select all
<?php
error_reporting(0); // turn error messages off
include('user.php');
$myObj = new User(); // new instance of User
echo "<form Method='POST' ACTION='test.php'>";
echo "<select name='title' type='text' />";
// call the function to create the array of values
foreach ($this->retrieveNames() as $name) {
echo '<option>' . $name . '</option>';
}
echo "</select>";
echo "<br /><br />";
echo "<input type='submit' name='submit' value='Submit' />";
echo "</form>";
?>
Re: Unable to display drop down list
Posted: Fri Apr 06, 2012 1:20 am
by Imtiyaaz
Thanks for this, I will test it later. In the table 'users' there are 6 records, I need to display the first name and last name of these users in a list.
Re: Unable to display drop down list
Posted: Fri Apr 06, 2012 10:22 am
by Imtiyaaz
ok this is what I have now but it still not working:
Code: Select all
<?php
error_reporting(0); // turn error messages off
include('user.php');
echo "<form Method='POST' ACTION='test.php'>";
echo "<select name='title' type='text' />";
// call the function to create the array of values
foreach ($this->retrieveNames() as $name) {
echo '<option>' . $name . '</option>'; // here I need to list the 6 records in the database
}
echo "</select>";
echo "<br /><br />";
echo "<input type='submit' name='submit' value='Submit' />";
echo "</form>";
?>
user.php
Code: Select all
<?php
class User{
public function getUserById($user_id){
$sql = 'select first_name, last_name, display_name from users where user_id = '.$user_id;
$results = mysql_query($sql);
$row = mysql_fetch_array($results);
$this->user_id = $user_id;
$this->fist_name = $row['first_name'];
$this->last_name = $row['last_name'];
$this->display_name = $row['display_name'];
return true;
}
public function getDisplayName(){
return $this->display_name;
}
public function getFriends(){
$sql = 'select friend_id from user_friends where user_id = '.$this->user_id;
$results = mysql_query($sql);
$friends = array();
while($row = mysql_fetch_array($results)){
$friends[] = $row['friend_id'];
}
return $friends;
}
// this function goes into the user class.
function retrieveNames()
{
$sql = "select first_name, last_name from users"; //first_name and last_name is the fields of the database i need to list, there are 6 records I need to list
$qry = mysql_query($sql);
while ($row = mysql_fetch_array($qry)) {
// place all 'name' values selected in an array called
// $names
$names[] = $row['first_name'].$row['last_name'];
}
return $names;
}
}
?>
Re: Unable to display drop down list
Posted: Fri Apr 06, 2012 10:30 am
by Celauran
Imtiyaaz wrote:Code: Select all
<?php
error_reporting(0); // turn error messages off
Here's problem #1. You're trying to debug something that's not working; shouldn't error messages be ON? It's like trying to find a needle in a haystack. In the dark.
Imtiyaaz wrote:Code: Select all
foreach ($this->retrieveNames() as $name) {
What's up with $this-> ? You still haven't instantiated an object.
Code: Select all
$user = new User();
$names = $user->retrieveNames();
Re: Unable to display drop down list
Posted: Fri Apr 06, 2012 10:45 am
by Imtiyaaz
Sorry, I'm new to php and very confused..this is what I have now but it still not working
Code: Select all
<?php
//error_reporting(0); // turn error messages off
include('user.php');
$user = new User(); // new instance of User
$names = $user->retrieveNames();
echo "<form Method='POST' ACTION='test.php'>";
echo "<select name='title' type='text' />";
// call the function to create the array of values
foreach ($this->retrieveNames() as $name) {
echo '<option>' . $name . '</option>';
}
echo "</select>";
echo "<br /><br />";
echo "<input type='submit' name='submit' value='Submit' />";
echo "</form>";
?>
the reason why I got foreach ($this->retrieveNames() as $name) { is because it was suggested above, what is it suppose to be then?
Re: Unable to display drop down list
Posted: Fri Apr 06, 2012 10:46 am
by Celauran
Code: Select all
$user = new User(); // new instance of User
$names = $user->retrieveNames();
You've stored the names in the $names variable, so
Re: Unable to display drop down list
Posted: Fri Apr 06, 2012 10:56 am
by Imtiyaaz
Thanks, its working

Re: Unable to display drop down list
Posted: Fri Apr 06, 2012 11:38 am
by Imtiyaaz
A small addition to the exercise Im doing...
Code: Select all
<?php
//error_reporting(0); // turn error messages off
include('user.php');
$user = new User(); // new instance of User
$names = $user->retrieveNames();
echo "<form Method='POST' ACTION='test.php'>";
echo "<select name='title' type='text' />";
foreach ($names as $name) {
echo '<option>' .$name. '</option>';
}
echo "</select>";
echo "<br /><br />";
echo "<input type='submit' name='submit' value='Submit' />";
//the above is working perfectly now
$Submit = $_POST['submit'];
if (isset($_POST['submit'])) {
//this is what I got from my php book but its not displaying the selected user's information
$user = new User();
$user->getUserById($ID);
echo "<p>User known as: " .$user->getDisplayName(). "<p>";
$friends = $user->getFriends();
echo "<p>Friends with: ".implode(',',$friends). "</p>";
$user->getUserById($ID);
echo "<p>User Know as: " .$user->getDisplayName(). "</p>";
echo "</form>";
}
?>
user.php
Code: Select all
<?php
class User{
public function getUserById($user_id){
$sql = 'select first_name, last_name, display_name from users where user_id = '.$user_id;
$results = mysql_query($sql);
$row = mysql_fetch_array($results);
$this->user_id = $user_id;
$this->fist_name = $row['first_name'];
$this->last_name = $row['last_name'];
$this->display_name = $row['display_name'];
return true;
}
public function getDisplayName(){
return $this->display_name;
}
public function getFriends(){
$sql = 'select friend_id from user_friends where user_id = '.$this->user_id;
$results = mysql_query($sql);
$friends = array();
while($row = mysql_fetch_array($results)){
$friends[] = $row['friend_id'];
}
return $friends;
}
public function retrieveNames()
{
$sql = "select first_name, last_name from users"; //first_name and last_name is the fields of the database i need to list
$qry = mysql_query($sql);
while ($row = mysql_fetch_array($qry)) {
// place all 'name' values selected in an array called
// $names
$names[] = $row['first_name'].$row['last_name'];
}
return $names;
}
}
?>
and again thanks for all the help
Re: Unable to display drop down list
Posted: Fri Apr 06, 2012 12:58 pm
by Celauran
$ID doesn't appear to be defined anywhere.