Unable to display drop down list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Imtiyaaz
Forum Newbie
Posts: 8
Joined: Thu Feb 02, 2012 5:52 am

Unable to display drop down list

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Unable to display drop down list

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Unable to display drop down list

Post 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>";
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Imtiyaaz
Forum Newbie
Posts: 8
Joined: Thu Feb 02, 2012 5:52 am

Re: Unable to display drop down list

Post 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.
Imtiyaaz
Forum Newbie
Posts: 8
Joined: Thu Feb 02, 2012 5:52 am

Re: Unable to display drop down list

Post 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;
}

}

?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Unable to display drop down list

Post 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();
Last edited by Celauran on Fri Apr 06, 2012 10:48 am, edited 1 time in total.
Imtiyaaz
Forum Newbie
Posts: 8
Joined: Thu Feb 02, 2012 5:52 am

Re: Unable to display drop down list

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Unable to display drop down list

Post 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

Code: Select all

foreach ($names as $name)
Imtiyaaz
Forum Newbie
Posts: 8
Joined: Thu Feb 02, 2012 5:52 am

Re: Unable to display drop down list

Post by Imtiyaaz »

Thanks, its working :-)
Imtiyaaz
Forum Newbie
Posts: 8
Joined: Thu Feb 02, 2012 5:52 am

Re: Unable to display drop down list

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Unable to display drop down list

Post by Celauran »

$ID doesn't appear to be defined anywhere.
Post Reply