如何显示数组中的所有数据

时间:2018-04-19 12:42:19

标签: php

我在显示来自函数

的数组中的数据时遇到问题
    public function izpis_narocil($user)
{
    $sql = "SELECT * FROM nabava WHERE id_uporabnika=?";
    if( !$this->stmt = $this->mysqli->prepare($sql) )
        throw new Exception("MySQL Prepare statement failed: ".$this->mysqli->error);

    $this->stmt->bind_param("i", $user);
    $this->stmt->execute();

    $result = $this->stmt->get_result();

    if( $result->num_rows == 0)
    return "";

    return $result->fetch_assoc();
}

这是代码,其中函数从:

调用
<?php

session_start();
require_once(dirname(__FILE__)."/simpleusers/su.inc.php");
include($path."/menu.php");

$SimpleUsers = new SimpleUsers();

$userId = $_GET["userId"];
$user = $SimpleUsers->getSingleUser($userId);
if( !$user )
    die("The user could not be found...");

$izpisnarocil = $SimpleUsers -> izpis_narocil($user[userId]);

&GT;

此代码继续使用html代码显示内容。

我想显示主文件中的所有行,它们调用上面的函数。

寻求帮助......

3 个答案:

答案 0 :(得分:0)

要在表格中显示结果,请以这种方式更改您的PHP代码。

<?php
public function izpis_narocil($user)
{
    $sql = "SELECT * FROM nabava WHERE id_uporabnika=?";
    if( !$this->stmt = $this->mysqli->prepare($sql) )
        throw new Exception("MySQL Prepare statement failed: ".$this->mysqli->error);

    $this->stmt->bind_param("i", $user);
    $this->stmt->execute();

    $result = $this->stmt->get_result();

    if( $result->num_rows == 0)
    return "";

    $data = array();
    while($row = $result->fetch_assoc()) {
            $data[] = $row;
    }

    return $data
}
?>

调用izpis_narocil()的文件是

<?php
   $izpisnarocil = $SimpleUsers -> izpis_narocil($user[userId]);

   $data = "<table>";
   foreach($izpisnarocil as $key=>$value) {
            $data .= "<tr><td>".$value['id_nabava']."</td><td>".$value['datum_vnosa']."</td><td>".$value['vrsta']."</td></tr>";
         }
        $data .= "</table>";
        echo $data;
    ?>

并添加您要显示的其他字段。

答案 1 :(得分:0)

显示网格视图

<强>代码

 $sql = "SELECT column1, column2,column3 FROM myTableName";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        echo "<table><tr><th>column1</th><th>column2</th><th>column3</th></tr>";
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "<tr><td>" . $row["column1"]. "</td><td>" . $row["column2"]. " " . $row["column3"]. "</td></tr>";
        }
        echo "</table>";
    } else {
        echo "0 Results";
    }

<强>结果

+++++++++++++++++++++++++++++++
+ column1 + column2 + column3 +
+++++++++++ +++++++++ +++++++++
+ val1    +    val2 + + val3  +
+++++++++++ +++++++++ +++++++++

答案 2 :(得分:0)

从数据库获得结果后,请按照

<?php
    $izpisnarocil = $SimpleUsers -> izpis_narocil($user[userId]);

    echo '<table>';
    echo '<thead><tr><th>column1</th><th>column2</th><th>column3</th></tr></thead><tbody>'; 
    foreach($izpisnarocil as $row) {
        echo'<tr>'; 
        echo'<td>'. $row['FieldName1']."</td>";
        echo'<td>'. $row['FieldName2'].'</td>';
        echo'<td>'. $row['FieldName3'].'</td>';
        echo'<tr>';

     }
    echo '</tbody>'; 
    echo '</table>'; 
?>

我希望这有助于你

注意:将FieldName替换为您的值