You are trying to use PHP to do the heavy lifting (the work), but you should be having PDO or mysqli do the heavy lifting:
For example using PHP.NET’s world database, I wrote this:
[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 subsituted.
*/
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]
The important line to remember line to remember is line 54. All you really need to do is use a while loop. You should really be using prepared statements instead of putting the variable directly into the query statement.
It would also make your life easier if you could have a special field in the database table for the divisions (for example : American League Central Division = alcentral ) that way all you would need is an if statement or do it using MySQL. Just a thought.
HTH John