PDO: how to populate html table with rows from database table?

Thanks, that’s useful. I didn’t know how to change the default fetch mode. I’m new to pdo myself, I just finished converting all my scripts to it this past weekend. I wish I’d known this before.

Only i need is “while()” to print rows for all selectbox. How to make “while” to do this.

<?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'], $_POST['predmet2'], $_POST['predmet3'], $_POST['predmet4'])) {
        if ($row = get_info($db, $_POST['predmet1'], $_POST['predmet2'], $_POST['predmet3'], $_POST['predmet4'])) {
			
            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'); 
?>

that’s the same as writing return $sql->fetch().

second, your get_info() function only supports a single value, not multiple values (for which you would need to repeatedly execute the prepared statement).

$stmt = $db->prepare("SELECT * FROM raspored WHERE predmet = :predmet");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->bindParam('predmet', $predmet, PDO::PARAM_STR);

// enumerated variables are a sign that you should use arrays instead
// i.e. use <input name="predmet[]"> instead of <input name="predmet1">/<input name="predmet2">/etc.
foreach ($_POST['predmet'] as $predmet) {
    $stmt->execute();
    echo result_to_html($stmt); // needs to be written
}

I make changes but don’t work. There is not errors but don’t generate table.

function get_info($db, $predmet)
{
	$stmt = $db->prepare("SELECT * FROM raspored WHERE predmet = :predmet");
	$stmt->setFetchMode(PDO::FETCH_ASSOC);
	$stmt->bindParam('predmet', $predmet, PDO::PARAM_STR);
    
	foreach ($_POST['predmet'] as $predmet) {
		$stmt->execute();
	}
}

?>


<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['save'])) {
        if ($stmt = get_info($db, $_POST['predmet[1]'])) {
		
            echo "<tr>" .
                "<td>" . $stmt["ID"] . "</td>" .
                "<td>" . $stmt["predmet"] . "</td>" .
                "<td>" . $stmt["profesor"] . "</td>" .
				"<td>" . $stmt["den"] . "</td>" .
				"<td>" . $stmt["chas"] . "</td>" .
				"<td>" . $stmt["prostorija"] . "</td>" .
				"<td>" . $stmt["tip"] . "</td>" .
                "</tr>";
			
        }
		
    }
	
	$_SESSION['predmet']=$_POST['predmet'];
	 
	
    ?>

of course it doesn’t work. I omitted the more or less inappropriate get_info() function for a reason.

What else i need to change or add?

create the result_to_html() function, which fetches the result from the statement and puts it into an HTML table.

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