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!
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?
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
<?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;
}
?>
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.
<?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>";
?>
<?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;
}
}
?>
<?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.
<?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>";
}
?>
<?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;
}
}
?>