Calling function multiple times doesn't work

Hello, I am trying to call this function multiple times but it works only once. How to make it work? I’m using PDO for connection.

Calling function:
[php]
$pull_map = "
SELECT field_id, type_id, user_id_in, owner_id , disp_row
FROM map_eu_1
ORDER BY field_id
ASC
";

$stmt = $link->query($pull_map);

addFieldRow($stmt, 1);
addFieldRow($stmt, 2);
[/php]

Function:
[php]
function addFieldRow($a, $b){
echo ‘

’;
while($row = $a->fetch(PDO::FETCH_ASSOC))
{
if ($row[“disp_row”] == $b) {
echo ‘<div id="’. “r” . $row[“disp_row”] .’" class="’. “type_” . $row[“type_id”] .’" data-value="’. $row[“field_id”] .’">
’;
}
}
echo ‘’;
}
[/php]

How it looks in HTML:

How it should look like:

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.

Thank you for the reply. Sorry but I have described it not as it should be.
I need all of them with have the same disp_row in one div just like it:

... if disp row = 2 (because there is 2 of them with disp_row = 2) div div field id 2 / div field id 3 / div /

if disp row = 3 (because there is 3 of them with disp_row = 3)
div
div field id 4 /
div field id 5 /
div field id 6 /
div /

if disp row = 4 (because there is 4 of them with disp_row = 4)
div
div field id 7 /
div field id 8/
div field id 9 /
div field id 10 /
div /

Apologies for misunderstanding. I hope to get it finally done.

Sponsor our Newsletter | Privacy Policy | Terms of Service