Why don’t you use MySQL do the heavy lifting instead of trying to call a function multiple times?
Here’s a quick script I wrote to show you an example what I’m talking about.
[php]<?php
include ‘lib/includes/connect/connect.php’;
$db_options = [
/* important! use actual prepared statements (default: emulate prepared statements) /
PDO::ATTR_EMULATE_PREPARES => false
/ throw exceptions on errors (default: stay silent) /
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
/ fetch associative arrays (default: mixed arrays) /
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
];
$pdo = new PDO(‘mysql:host=’ . DATABASE_HOST . ‘;dbname=world;charset=utf8’, DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);
/
- Grab the Amount of Records ($row) by setting a LIMIT (in this case it’s 50).
*/
$query = ‘SELECT Name, CountryCode, District, Population FROM city ORDER BY CountryCode DESC LIMIT 50’;
$stmt = $pdo->query($query);
?>
Display From Database Table
div.container {
display: block;
width: 100%;
max-width: 400px;
height: auto;
background-color: orange;
padding: 10px;
margin: 10px auto;
}
p.cityRow {
font-family: Arial, Helvetica, sans-serif;
font-size: 1.0rem;
line-height: 1.5;
color: #2e2e2e;
}
p.cityRow span.tabStyle {
margin-left: 10px;
}
<?php
/*
* I used the World Database (from php.net) and the table city for an example.
* I even styled it a little bit. Use class when it is needed and there is really no need to
* increment it. Though I don't know exactly whatcha doing and I couldn't use your database,
* so I substituted.
*/
echo '
' . "\n";
while ($row = $stmt->fetch()) {
echo '<p class="cityRow">'
. '<span class="tabStyle">' . $row['Name'] . '</span>'
. '<span class="tabStyle">' . $row['CountryCode'] . '</span>'
. '<span class="tabStyle">' . $row['District'] . '</span>'
. '<span class="tabStyle">' . $row['Population'] . '</span>'
. '</p>' . "\n";
}
echo "</div>\n";
?>
</body>
[/php]
I over the years have found the PHP is pretty easy to manipulate HTML elements, I think that is the hardest thing that I had to wrap my mind around. I even read somewhere that is one of the reasons why the PHP was developed in the first place to be able to manipulate HTML where the pages are dynamically added by PHP.