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?