2

Unfortunately I am struggling to print some array items in PHP and was hoping someone could assist me. Slightly embarassing :) I think I might be using arrays incorrectly? I am trying to build an application object from the database and once this has been done i am trying to iterate over it and print some basic details. I have also included the separate Application class.

<?php

include("application.php");

$applicationsForUser = array();

if(isset($_POST['submit'])){
    $username=$_POST["username"];
    $password=$_POST["password"];
    $userid = logUserIn($username, $password);
    if($userid > 0){
        getAppInformation($userid);
        foreach ($applicationsForUser as $app) {
            echo $app->$getUserid() . $app->$getName();
        }
    }
}

function getAppInformation($userid){
    $conn = new mysqli('localhost:3306', 'root', '', 'clientportal');
    if ($conn->connect_errno > 0) {
        die('Could not connect: ' . mysql_error());
    }else{
        //we have connected to the database
        $sql = "SELECT * FROM application WHERE userid = '$userid'";
        if(!$val = $conn->query($sql)){
            die('There was an error running the query [' . $db->error . ']');
        }else{
            $index = 0;
            while($row = $val->fetch_assoc()){
                $userid = $row['userid'];
                $name = $row['name'];
                $dateCreated = $row['date'];
                $invoice = $row['invoiceid'];
                $comment = $row['commentsid'];              
                $application = new Application($userid, $name, $dateCreated, $invoice, $comment);
                $applicationsForUser[$index] = $application;
                $index++;
            }
        }
    }
    $conn -> close();   
}




<?php
class Application {

    var $userid;
    var $name;
    var $dateCreated;
    var $invoice;
    var $comment;

    function Application($userid, $name, $dateCreated, $invoice, $comment) {
        $this ->userid = $userid;
        $this ->name = $name;
        $this ->dateCreated = $dateCreated;
        $this ->invoice = $invoice;
        $this ->comment = $comment;
    }

    function getUserid(){
        return $this ->userid;
    }

    function getName(){
        return $this ->name;
    }

    function getDateCreatd(){
        return $this ->dateCreated;
    }

    function getInvoice(){
        return $this ->invoice;
    }

    function getComment(){
        return $this ->comment;
    }
}
?>
2
  • Your question is not clear. In which part of your code you are facing problem? Commented Jun 14, 2014 at 9:33
  • you could assume from the title that the problem lies in the area where i am printing the array content out :D Commented Jun 14, 2014 at 9:34

2 Answers 2

1

Your problem is, that $applicationsForUser is supposed to be global. Therefore you need to use

function getAppInformation($userid){
    global $applicationsForUser;

Otherwise your foreach iterates over an empty array here:

    getAppInformation($userid);
    foreach ($applicationsForUser as $app) {
Sign up to request clarification or add additional context in comments.

Comments

0

Don't use globals:

You didn't initialize your array before trying to loop through it.

<?php

include("application.php");


if(isset($_POST['submit'])){
    $username=$_POST["username"];
    $password=$_POST["password"];
    $userid = logUserIn($username, $password);
    if($userid > 0){
        // get application information
        $applicationsForUser = getAppInformation($userid);
        foreach ($applicationsForUser as $app) {
            echo $app->$getUserid() . $app->$getName();
        }
    }
}

function getAppInformation($userid){
    $applicationsForUser = array();
    $conn = new mysqli('localhost:3306', 'root', '', 'clientportal');
    if ($conn->connect_errno > 0) {
        die('Could not connect: ' . mysql_error());
    }else{
        //we have connected to the database
        $sql = "SELECT * FROM application WHERE userid = '$userid'";
        if(!$val = $conn->query($sql)){
            die('There was an error running the query [' . $db->error . ']');
        }else{
            $index = 0;
            while($row = $val->fetch_assoc()){
                $userid = $row['userid'];
                $name = $row['name'];
                $dateCreated = $row['date'];
                $invoice = $row['invoiceid'];
                $comment = $row['commentsid'];              
                $application = new Application($userid, $name, $dateCreated, $invoice, $comment);
                $applicationsForUser[$index] = $application;
                $index++;
            }
        }
    }
    $conn -> close();
    return $applicationsForUser; 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.