php数组打印中的问题

php数组打印中的问题,php,arrays,Php,Arrays,不幸的是,我正在努力用PHP打印一些数组项,希望有人能帮助我。有点尴尬:)我想我可能使用数组不正确?我正试图从数据库中构建一个应用程序对象,一旦完成,我将尝试对其进行迭代并打印一些基本细节。我还包括了单独的应用程序类 <?php include("application.php"); $applicationsForUser = array(); if(isset($_POST['submit'])){ $username=$_POST["username"]; $p

不幸的是,我正在努力用PHP打印一些数组项,希望有人能帮助我。有点尴尬:)我想我可能使用数组不正确?我正试图从数据库中构建一个应用程序对象,一旦完成,我将尝试对其进行迭代并打印一些基本细节。我还包括了单独的应用程序类

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

您的问题是,$applicationsForUser应该是全局的。因此,您需要使用

function getAppInformation($userid){
    global $applicationsForUser;
否则,您的foreach将在此处迭代空数组:

    getAppInformation($userid);
    foreach ($applicationsForUser as $app) {
不要使用全局变量:

在尝试循环遍历数组之前,您没有初始化数组

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

你的问题不清楚。您在代码的哪一部分遇到问题?您可以从标题中假设问题出在我打印数组内容的区域:D