0

I am trying to pass through an array in PHP, to use it on another page and display it in a table.

I have 3 pages:

database.php
functions.php
page.php

database.php contains my database connection. functions.php contains my function with the SQL query page.php - In this page I include the functions.php and database.php files.

Functions.php contains:

<?php
include_once('includes/database.php');


function getOpenOrders()
{
global $con;


$query = "SELECT id,companyname,firstname,lastname,orderid,invoiceamount from open_orders";

if($result = mysqli_query($con, $query))
{
    if(mysqli_num_rows($result) > 0)
    {
        while($row = mysqli_fetch_array($result))
        {
            return $row;
        }
    }

}
}
?>

While my page.php contains the following code:

<?php
session_start();

if (!isset($_SESSION['loggedin']))
{
    header('Location: index.html');
    exit;
}
include_once('includes/database.php');
include_once('includes/functions.php');

$openOrder = getOpenOrders();

?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Profile Page</title>
        <link href="style.css" rel="stylesheet" type="text/css">
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
    </head>
    <body class="loggedin">
    <?php include 'header.html';?>
        <div class="content">
            <h2>Facturen aanmaken</h2>
            <div>
                <table>
                <tr>
                <th>Bedrijfsnaam</th>
                <th>Voornaam</th>
                <th>Achternaam</th>
                <th>Ordernummer</th>
                <th>Factuurbedrag</th>
                </tr>
                <?php
                    echo '<tr><td>',$openOrder['companyname'],'</td><td>',$openOrder['firstname'],'</td><td>', $openOrder['lastname'],'</td><td>', $openOrder['orderid'],'</td><td>', $openOrder['invoiceamount']
                ?>
                </table>
            </div>
        </div>
    </body>
</html>

In this page I call the function, using: $openOrder = getOpenOrders();

The code itself seems to be working, but only returns 1 (the first) row as the result of the query.

I think I have to use the foreach function and still have to add the data to a [] (array). I have tried several examples, but it seems I can't fit it into my example.

What is the next step to add to return all rows in the result of the query?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Marco
  • 1
  • 4
    https://www.php.net/manual/en/mysqli-result.fetch-all.php exists, if you want to fetch all records in one go. Otherwise, you’d have to put each single row into an array _inside_ your loop, and return the whole array only _after_ the loop. – CBroe Mar 18 '21 at 11:33
  • And then in the code embedded in the HTML you'd need to loop through that array, to create a HTML table row for each record. – ADyson Mar 18 '21 at 11:35
  • 1
    `I have tried several examples`...show what you tried then, and we can help you to fix it. Otherwise you'll just get pointed at the endless examples of this standard functionality already available online. If you couldn't make it work for you, then we need to see your attempt to understand what you did wrong, and then help you understand why it was wrong. – ADyson Mar 18 '21 at 11:36

0 Answers0