0

I'm fairly new using PHP classes, I have done some PHP using the procedural method for a while but never fully done proper object oriented programming on php. Which brings me to my question:

I have one file called classes.php where I intend to define all my php classes. Inside that file I have created a class as follows:

require_once("connection.php");

class User
    {

        public function getUserInfo() {
            $userinfo = mysqli_query($con,"SELECT * FROM users WHERE username='". $_SESSION['username'] ."'");
            while($row = mysqli_fetch_assoc($userinfo)) {

                // USER DETAILS:
                $userid = $row['id'];
                $username = $row['username'];
                $userfirstname = $row['first_name'];
                $userlastname = $row['last_name'];
                $useremail = $row['email'];
                $useraddress1 = $row['address_line_1'];
                $useraddress2 = $row['address_line_2'];
                $userpostcode = $row['postcode'];
                $userphone = $row['phone'];
                $usermobile = $row['mobile'];
                $usercreated = $row['created'];
                $usermodified = $row['modified'];
                $useraccess = $row['access_level'];

            }
        }

    } // end of User

Now I want to use the values of the variables defined in that class in another page. But how can I access those values?

So far I'm trying this (unsuccessfully):

<?php
include "php_includes/classes.php";
$user = new User();
?>

<div class="somediv">
    <?php echo $user->getUserInfo($username) ?>
</div>

I have also tried:

<?php
    include "php_includes/classes.php";
    $user = new User();
    ?>

    <div class="somediv">
        <?php echo $user->$username ?>
    </div>

Is there any way to echo those variables in another page so I can use the user information in, for example, profile pages?

Thanks in advance.

L. Paz
  • 41
  • 5
  • You can use php session – LF00 May 11 '17 at 10:39
  • Possible duplicate of [Pass variables between two PHP pages without using a form or the URL of page](http://stackoverflow.com/questions/16778425/pass-variables-between-two-php-pages-without-using-a-form-or-the-url-of-page) – LF00 May 11 '17 at 10:40

3 Answers3

0

Before your function you need make PUBLIC VARIABLE like:

public $userid; 

Then next in code in function you need to use:

$this->userid = $row['id'];

EDIT

Your calss need to be:

class User
    {

        public $userid;
        public function getUserInfo() {
            $userinfo = mysqli_query($con,"SELECT * FROM users WHERE username='". $_SESSION['username'] ."'");
            while($row = mysqli_fetch_assoc($userinfo)) {

                // USER DETAILS:
                $this->$userid = $row['id'];


            }
        }

    } // end of User

And you also need include this class file in other php page when you want show data.

  • Hi Robert, I have formatted the class as you have suggested. But how do I call the variable values in the other page? I'm trying this with no success: `username ?>` – L. Paz May 11 '17 at 10:53
  • Do you get any error or ? I will edit my answer with full class look and you see its ok in your code. – Robert Glavaš May 11 '17 at 10:55
  • Just for test, try $this->$userid = $row['id']; change with $this->$userid = 'test'; and tall me if test print (with that you will sure that is problem with class or else your database not give to you any record and your variable was empty). – Robert Glavaš May 11 '17 at 10:58
  • No, still not printing on the page – L. Paz May 11 '17 at 11:05
  • You can check it : http://stackoverflow.com/questions/1981375/setting-public-class-variables – Robert Glavaš May 11 '17 at 11:11
  • Right, I have defined the variable OUTSIDE the while loop with the test value like: `public $userid = "test";` and it prints on the page. But when the value is being defined inside the while loop, it doesn't print on the page. So if I declare the empty variable outside the loop like `public $userid;` and then grab the value inside the WHILE loop like `$this->userid = $row['id'];` or even `$this->userid = 'test';` it doesn't work – L. Paz May 11 '17 at 11:16
  • Its problem with your SQL query because your result was empty and while loop not started by event of row. You can try outside the while loop test if you $_SESSION['username'] are good, try this OUTSIDE the wile loop $this->userid = $_SESSION['username']; if you don't get any result your $_SESSION['username'] is empty and not working. – Robert Glavaš May 11 '17 at 11:20
  • Hi Robert, I can't define it cause it keeps giving me a syntax error "unexpected T_VARIABLE. I'm defining it like this: `$this->username = $_SESSION["username"];` if I define it inside the loop it doesn't give a syntax error, but still doesn't print – L. Paz May 11 '17 at 11:36
  • Just try this, in sql query change $_SESSION["username"] with real user in your database, and you back $this->userid = $row['id']; inside the while loop and if you get result your session variable was not work. – Robert Glavaš May 11 '17 at 11:38
  • Its also problem with your database connection, do you have any other mysql query (witch you are 100% sure that your mysql connection work)? You can also start chat. – Robert Glavaš May 11 '17 at 12:09
  • Yes the connection works cause I'm pulling information from the database on the page without using the class, I can login and logout using that connection code and the $_SESSION['username'] variable is being used outside the class without problems. In regards to chat, it won't let me because I'm new to stackoverflow – L. Paz May 11 '17 at 12:12
  • Sure robert, i have compiled all the relevant files here, but changed all sensitive information. https://lux365.s3.amazonaws.com/downloads/php_class_issue.zip – L. Paz May 11 '17 at 12:25
  • You don't need public def. of variable if you use it in just one function or other, try remove $userinfo and $con from public definitions. – Robert Glavaš May 11 '17 at 12:43
  • Hi Robert, I have actually tried Muhammad's answer and it is now working using his approach. I do appreciate the time you have taken on trying to find a solution as well. Thank you very much for your help! – L. Paz May 11 '17 at 13:30
0

you have to declare the variable as PUBLIC

class User
{
       public function getUserInfo() {
       $userinfo = mysqli_query($con,"SELECT * FROM users WHERE username='". $_SESSION['username'] ."'");
       while($row = mysqli_fetch_assoc($userinfo)) {

                        // USER DETAILS:
                        public $userid = $row['id'];

        }
        }
} 

For more information check here

lalithkumar
  • 3,480
  • 4
  • 24
  • 40
0

you should return the value from function.

require_once("connection.php");

class User
    {

        public function getUserInfo() {
            $userinfo = mysqli_query($con,"SELECT * FROM users WHERE username='". $_SESSION['username'] ."'");
            $row = mysqli_fetch_assoc($userinfo);

            return row
        }

    } 

and now only have to show the detail of user

<?php
  include "php_includes/classes.php";
  $user = new User();
 ?>
 <div class="somediv">
    <?php $user_detail=$user->getUserInfo(); ?>
    <label>name:</label><br>
    <?php echo $user_detail["first_name"]; ?>
 </div>

Hence you can check whether row is null or not and whole array can be returned by this function of class any where this class included in file.

lalithkumar
  • 3,480
  • 4
  • 24
  • 40
  • Hi Muhammad, I have tried this but I get a few errors. I think the class is not recognising the value of `$con` my connection.php code is `` and it works fine cause everything else in the site works, including the login and logout procedures. – L. Paz May 11 '17 at 11:50
  • public function getUserInfo() { global $con; $userinfo = mysqli_query($con,"SELECT * FROM users WHERE username='". $_SESSION['username'] ."'"); $row = mysqli_fetch_assoc($userinfo); return row } this will resolve the issue becuse conection variable is out side the class so make global to acces in the class of function } – Muhammad Zubair May 11 '17 at 12:51
  • Muhammad, that has actually worked! Thanks for your answer – L. Paz May 11 '17 at 13:29
  • This is the final code I have used in the class: `` – L. Paz May 11 '17 at 13:35