I’m currently building a basic search utility for my site. However I’m stuck on DataTables and inserting php.
At present, I have an array with simulated data (ie Title and Description), and at a later stage I’ll replace the simulated array for a database.
I have an html file, which has a simple DataTable. I would like to ask how can I insert the simulated array/database into the DataTable?
my spider.php file reads:-
<?php
for ($arrcount=0; $arrcount <= 50; $arrcount++) {
$result_title[$arrcount] = "Header Title:- ".$arrcount;
$result_description[$arrcount] = "Description Line:- ".$arrcount;
}
include($_SERVER['DOCUMENT_ROOT'].'/public/pubdsply.html');
?>
and my pubdsply.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Landing Page - Start Bootstrap Theme</title>
Bootstrap core CSS -->
<link href="layout/public/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!--Custom fonts for this template -->
<link href="layout/public/vendor/fontawesome-free/css/all.min.css" rel="stylesheet">
<link href="layout/public/vendor/simple-line-icons/css/simple-line-icons.css" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Lato:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css">
<!-- Custom styles for this template -->
<link href="layout/public/css/landing-page.min.css" rel="stylesheet">
Data Table-->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/style.min.css" rel="stylesheet" />
<link href="/layout/publictable/css/styles.css" rel="stylesheet" />
<script src="https://use.fontawesome.com/releases/v6.3.0/js/all.js" crossorigin="anonymous"></script>
</head>
<body>
<?php
if(session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
?>
<!-- Navigation -->
<nav class="navbar navbar-light bg-light static-top">
<div class="container">
<!--<a class="navbar-brand"><?= $_SESSION['settings_title'] ?></a>-->
<a class="navbar-brand" href="#">Start Bootstrap</a>
<a class="btn btn-primary btn-sm" href="regsignin.php">Sign In</a>
</div>
</nav>
<!------------------------------------------------------------------------------------------------>
<div id="layoutSidenav_content">
<main>
<div class="container-fluid px-4">
<h1 class="mt-4">Tables</h1>
<ol class="breadcrumb mb-4">
<li class="breadcrumb-item"><a href="index.html">Dashboard</a></li>
<li class="breadcrumb-item active">Tables</li>
</ol>
<div class="card mb-4">
<div class="card-body">
DataTables is a third party plugin that is used to generate the demo table below. For more information about DataTables, please visit the
<a target="_blank" href="https://datatables.net/">official DataTables documentation</a>.
</div>
</div>
<div class="card mb-4">
<div class="card-header">
<i class="fas fa-table me-1"></i>
DataTable Example
</div>
<div class="card-body">
<table id="datatablesSimple">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
<?
for ($arrcount=0; $arrcount <= 50; $arrcount++) {
echo "<tr>";
echo "<td>";
echo $result_title[$arrcount] . "<br>";
echo $result_description[$arrcount] . "<br>";
echo "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</main>
</div>
<!-- Bootstrap core JavaScript -->
<script src="layout/public/vendor/jquery/jquery.min.js"></script>
<script src="layout/public/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
DataTable -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script src="/layout/publictable/js/scripts.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/simple-datatables.min.js" crossorigin="anonymous"></script>
<!--<script src="/layout/publictable/js/datatables-simple-demo.js"></script>-->
</body>
</html>
The result I’m getting is that it’s echoing the php code, instead of displaying the array.
May I please ask how I can solve this?
Thank You.
I’ve tried copying the code from the pubdsply.html file to php file, and to a tpl file and got the same result, it just echo’s the php code (ie for loop).
2
Answers
If here are no syntax server errors then it is likely as mentioned that the short open tag is not enabled. Try switching it to default open php tag with the label php following the question mark.
To generate sample output the provided code required some tweaks to make it output correctly, namely the session that started in the body was moved to the root of the file on the very first line so I could make this work in the test environment link below. The rendered code can be seen below. Referenced file was included on top of the main script as a local segment of code.
The most important thing to note is a referenced array label must be declared (as an array) if not already
Use
or
for each applicable variable label that is that is used with square brackets.
I also commented out the short hand php and the tag and the output statement that will output a $_SESSION key to produce viable output.
See php rendered code
https://onlinephp.io/c/8aec5
And resulting output.
I make an example. I assume that you already connected to the dB using (msqli object oriented).
Of course there are many ways to do so. You can also add conditions in the query if you want