[RESOLVED] Not displaying selected user's information
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: [RESOLVED] Not displaying selected user's information

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    51

    resolved [RESOLVED] Not displaying selected user's information

    Hi,

    I need help with part of the following php

    PHP Code:
    <?php
    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 works fine and lists the first_name and last_name correctly in the list
    $Submit $_POST['submit'];
    if (isset(
    $_POST['submit'])) {
    // this is where I need help, it does not display the user 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 
    "<p><a href='oop4.php'>Return</a></p>";
    echo 
    "</form>";
    }
    ?>
    user.php

    PHP Code:
    <?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;
    }

    }

    ?>
    Any assistance will be aprreciated

    Thanks

  2. #2
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,740
    PHP Code:
    // this is where I need help, it does not display the user information     
    $user = new User(); 
    $user->getUserById($ID); 
    Where is $ID defined?
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks

    I've changed it to

    PHP Code:
    <?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='oop4.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' />";

    $Submit $_POST['submit'];
    if (isset(
    $_POST['submit'])) {
    $user = new User();
    $user->getUserById($user_id);
    echo 
    "<p>User known as: " .$user->getDisplayName(). "<p>";

    $friends $user->getFriends();
    echo 
    "<p>Friends with: ".implode(',',$friends). "</p>";

    $user->getUserById($user_id);
    echo 
    "<p>User Know as: " .$user->getDisplayName(). "</p>";

    echo 
    "<p><a href='test2.php'>Return</a></p>";
    echo 
    "</form>";
    }
    ?>
    It still does not display anything when I select the user

  4. #4
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    same problem -- where is $user_id defined?

    changing the name of the variable doesn't help if the variable still doesn't exist. Try replacing this with a known user ID so you can see if the method itself is actually working.

    you would be getting some useful feedback, I think, if your getUserById() method didn't automatically return "true" - if no user is found, you should at least return "false" so you can know what's going on.

    ---------------------

    also, when you say it "does not display anything," do you mean that it's not printing the user info, or that it literally stops printing anything? In the latter case, you're probably running into an error. Make sure to set error_reporting( -1 ); so you can see all your error messages during development.
    Last edited by traq; 04-08-2012 at 01:37 PM.

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    51
    ok thanks, I've defined user_id now:

    PHP Code:
    <?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='oop4.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' />";

    $Submit $_POST['submit'];
    if (isset(
    $_POST['submit'])) {
        
    //header("location:user_find.php"); // redirects to the user_find.php page
    $user = new User();
    $user_id $user->getUserById($user_id);
    echo 
    "<p>User known as: " .$user->getDisplayName(). "<p>";

    $friends $user->getFriends();
    echo 
    "<p>Friends with: ".implode(',',$friends). "</p>";

    $user->getUserById($user_id);
    echo 
    "<p>User Known as: " .$user->getDisplayName(). "</p>";

    echo 
    "<p><a href='test.php'>Return</a></p>";
    echo 
    "</form>";
    }
    ?>
    and now it displays the following, no matter which user I select from the drop down list

    User known as:

    Friends with:

    User Known as: Peter Pan

    Return

    user.php
    PHP Code:
    <?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;
    }

    }

    ?>
    Last edited by cybastud; 04-08-2012 at 04:40 PM.

  6. #6
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,354
    I still don't see where you've defined $user_id before this line:
    PHP Code:
    $user_id $user->getUserById($user_id); 
    ?

  7. #7
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    PHP Code:
    $user_id $user->getUserById($user_id); 
    The second $user_id on this line is undefined. You don't define it anywhere (in the code you've shown us) before this line. It's the same as passing NULL to the function. Did you turn on error reporting as I suggested? Are you getting an error message along the lines of "Notice: Undefined variable: $user_id in ..."?

    As for the first $user_id, the end result of this line of code is that it will be assigned the value of TRUE (since your getUserById() method always returns TRUE).

    HOWEVER:
    • This probably isn't the value you need (I'm assuming you need an integer).
    • It's too late anyway: you already ran your DB query.


    --------------------------
    You need to define variables before you use them. If $user_id is actually defined elsewhere in your code, before you try to use it, please show us the relevant lines. It would seem to me, however, that you simply haven't defined it yet.
    Last edited by traq; 04-08-2012 at 05:26 PM.

  8. #8
    Member
    Join Date
    Sep 2011
    Posts
    51
    Is that not defining the variable? Sorry I'm new to php and a bit confused...how do I define the variable, and do I need to in order for the function to work properly?

  9. #9
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    PHP Code:
    $user_id 1;
    $user->getUserById$user_id ); 
    If there are any users in your database, there's probably a user with user_id = 1. Let me know if this works.

    BUT FIRST, please add error_reporting( -1 ); as the first line of code after your <?php tag.

    ----------------------------
    IF this works, then your function works (if not very well). To make it dynamic, you need to decide how the code is supposed to know which user it's supposed to look for (it can't guess), and make $user_id = whateverUserIdNumberYouWantToFind.

    ----------------------------Edit:
    Quote Originally Posted by cybastud
    Is that not defining the variable?
    yes, but as I said:
    ...it's still not defined when you're trying to use it (assignment doesn't happen until after the assignment is complete: so the second time you write $user_id on that line, it still "doesn't exist yet").

    ...it assigns $user_id the value of TRUE, which isn't useful for what you're trying to do.
    Last edited by traq; 04-08-2012 at 05:38 PM.

  10. #10
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks and yes I turned on error reporting and it gave me the Notice: Undefined variable: $user_id error, i tried the above now and it works but I have 7 users in my db and which ever user I select it needs to display the corresponding data, right now because I did $user_id = 1; I only get the info for the user id 1, how do I change this so whichever user I select from the options list I get there info...here is the output I got for user_id 1

    User known as: Peter Pan

    Friends with: 3,5

    User Known as: Peter Pan

    and here is the output if I have user_id = 2

    User known as: King of Rock

    Friends with: 2,5

    User Known as: Peter Pan

  11. #11
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Quote Originally Posted by cybastud
    ...how do I change this so whichever user I select from the options list I get there info
    Quote Originally Posted by traq View Post
    To make it dynamic, you need to decide how the code is supposed to know which user it's supposed to look for (it can't guess), and make $user_id = whateverUserIdNumberYouWantToFind.

    [emphasis added]
    if you're trying to select a name from the form you just made, you're out of luck: you don't have any user_ids to choose from in that form.

    Either add them as the option values, or write a new method that looks for users by name (what your form calls title, for some reason) instead of by user_id.
    Last edited by traq; 04-08-2012 at 06:00 PM.

  12. #12
    Member
    Join Date
    Sep 2011
    Posts
    51
    Yes, how do I add them as option values?

  13. #13
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    1. when you build your form, select the ids from the database along with the names.

    2. write the ids as the value of each option in your select list.

    3. when your form is submitted, you can use $_POST['title'] (or, rename your select list to something more appropriate -like user_id maybe- and use that index) to access them.

  14. #14
    Member
    Join Date
    Sep 2011
    Posts
    51
    ok cool, I've changed my retrieveNames() function in the user.php to include the user_id as well then write it to the options list. I've also renamed the $_POST['title'] to $_POST['user_id'] but now I dont know how to use that index to access the user_id

    user.php
    PHP Code:
    <?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 user_id, 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['user_id']. ". ".$row['first_name'].$row['last_name'];
         }

         return 
    $names;
    }

    form

    PHP Code:
    <?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='oop4.php'>";
    echo 
    "<select name='user_id' type='text' />";
    foreach (
    $names as $name) {
        echo 
    '<option>' .$name'</option>';
    }
    // now it displays the user ids along with name and surname
    echo "</select>";
    echo 
    "<br /><br />";
    echo 
    "<input type='submit' name='submit' value='Submit' />";

    if (isset(
    $_POST['user_id'])) { // is this right?
    $user = new User();
    $user_id 1// what should I do here?
    $user_id $user->getUserById($user_id);
    echo 
    "<p>User known as: " .$user->getDisplayName(). "<p>";

    $friends $user->getFriends();
    echo 
    "<p>Friends with: ".implode(',',$friends). "</p>";

    $user->getUserById($user_id);
    echo 
    "<p>User Known as: " .$user->getDisplayName(). "</p>";

    echo 
    "<p><a href='test.php'>Return</a></p>";
    echo 
    "</form>";
    }
    ?>

  15. #15
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    this:
    PHP Code:
    $names[] = $row['user_id']. ". ".$row['first_name'].$row['last_name']; 
    writes the user_id into a string along with the user's name. This will make it difficult to use later on.

    Instead, just pass the user_id and name separately:
    PHP Code:
    $names[] = array( $row['user_id'], $row['first_name'].$row['last_name'] ); 
    Then, when you build your select box, use the user_id as the value of the option, leaving the name as the option text
    (note I also changed some of your HTML markup that was incorrect):
    PHP Code:
    $select_box '<select name="user_id">';
    foreach (
    $names as $name) {
        
    // remember, $name is now an array holding the user_id and name
        
    list( $uid,$uname ) = $name;
        
    $select_box .= '<option value="'$uid .'">'$uname .'</option>';
    }
    $select_box .= "</select>"
    print 
    $select_box
    when the form is submitted:
    PHP Code:
    if (isset($_POST['user_id']) && ctype_digit$_POST['user_id'] )) {
    // the ctype_digit() check makes sure the user_id is numeric:
    //   (I'm assuming all your user_ids are integers)
    //   if other characters are submitted, then something fishy is going on
    //   This also means that (being only numbers) it's safe for the database query
    $user = new User();
    $user_id $_POST['user_id'];
    // since we don't use the return value of your getUserById() method,
    //    there's no reason to assign it to a variable
    $user->getUserById($user_id); 
    // and so forth 
    Last edited by traq; 04-09-2012 at 03:54 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •