Populate html table with data rows. Chooses with select boxes

This code work for one selectbox and when find value he stop even if exist more column with same value.
I need to work for many select box, and to populate table with all rows with same column value (value.that the user chooses with select boxes)

<?php
require('includes/config.php');
require('layout/header.php'); 
function get_info($db, $predmet)
{
    $sql = $db->prepare("SELECT * FROM raspored WHERE predmet = :predmet");
    $sql->setFetchMode(PDO::FETCH_ASSOC);
    $sql->execute([':predmet' => $predmet]);
    if ($row = $sql->fetch()) {
        return $row;
    }
    return false;
}

?>


<table border="0" class="table table-hover table-striped">
    <tr COLSPAN=2 BGCOLOR="#6D8FFF">
        <th>ИД</th>
        <th>Предмет</th>
        <th>Професор</th>
		<th>Ден</th>
		<th>Час</th>
		<th>Просторија</th>
		<th>Тип</th>
    </tr>
	
    <?php
    if (isset($_POST['predmet1'])) {
        if ($row = get_info($db, $_POST['predmet1'])) {
			
            echo "<tr>" .
                "<td>" . $row["ID"] . "</td>" .
                "<td>" . $row["predmet"] . "</td>" .
                "<td>" . $row["profesor"] . "</td>" .
				"<td>" . $row["den"] . "</td>" .
				"<td>" . $row["chas"] . "</td>" .
				"<td>" . $row["prostorija"] . "</td>" .
				"<td>" . $row["tip"] . "</td>" .
                "</tr>";
			
        } else {
            echo "don't exist records for list on the table";
        }
    }
    ?>
	
</table>
</div>

<?php 
//футер
require('layout/footer.php'); 
?>

Your code will always produce only one row because there is no loop to extract all of the rows.

Try to modify the function like so:

function get_info($db, $predmet)
{
    $sql = $db->prepare("SELECT * FROM raspored WHERE predmet = :predmet");
    $sql->setFetchMode(PDO::FETCH_ASSOC);
    $sql->execute([':predmet' => $predmet]);
    $rows = [];
    while ($row = $sql->fetch()) {
        $rows[] = $row;
    }
    return $rows;
}

and table generating code like so:

<?php
    if (isset($_POST['predmet1'])) {
        $rows = get_info($db, $_POST['predmet1']);
        if (count($rows)){
            foreach ($rows as $row) {    			
                echo "<tr>" .
                    "<td>" . $row["ID"] . "</td>" .
                    "<td>" . $row["predmet"] . "</td>" .
                    "<td>" . $row["profesor"] . "</td>" .
				"<td>" . $row["den"] . "</td>" .
				"<td>" . $row["chas"] . "</td>" .
			"<td>" . $row["prostorija"] . "</td>" .
			"<td>" . $row["tip"] . "</td>" .
                    "</tr>";
            }			
        } else {
            echo "don't exist records for list on the table";
        }
    }
?>
1 Like

I’m guessing you ignored the snippet I posted in your other topic. PDO: how to populate html table with rows from database table? - #8 by spaceshiptrooper

Different table, but still the same concept. That whole approach with $sql->setFetchMode(PDO::FETCH_ASSOC); is very numbing. It is appropriate to define a fetch mode in the options when you create your PDO connection. The way you are trying looks to be an old school which has other better ways anyways besides that. You can use setAttribute to set the default fetch mode. That’s a better alternative. However I highly suggest you do it the right way by defining it in the PDO connection.

Another problem I see in your snippet, you are using isset($_POST['predmet'])) which is the same as isset($_POST['submit']) or isset($_POST). This is a rookie move which mostly is taught by websites like w3schools. These are old methods that are basically “hacks” if you will of checking for form submission. The proper way is using $_SERVER['REQUEST_METHOD'].

1 Like

For grabbing the result set of the query, instead of the loop you could use http://php.net/manual/en/pdostatement.fetchall.php to grab the entire result set in a single hit

1 Like

Thank you so much. I need just one more thing, When table is generated save in the session and everytime when user is log in, table to be on the page. And selectbox When table is generated save in the session and everytime when user is log in, table to be on the page, and selectbox also.

So put your $_POST['predmet1'] into the session when it is set.
And change the condition to check session as well:

// check POST and SESSION
if (isset($_POST['predmet1']) || isset($_SESSION['predmet1'])) {
    // get ID from POST or SESSION
    $predmet1 = isset($_POST['predmet1']) ? $_POST['predmet1'] : $_SESSION['predmet1'];
    // and store it into the session
    $_SESSION['predmet1'] = $predmet1; 
    // get rows
    $rows = get_info($db, $predmet1);
    // ... the rest of the code ...

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.