Answer the question
In order to leave comments, you need to log in
How to output data from pdo function?
Good evening. I started to study pdo and I'm interested in the following question:
I created a function.php file and I want to prescribe functions in it, and then display it on request.
I also have an index.php file, where the actual layout is located. I do not want to write anything extra from php into this layout, but I need to display a list of all students. Tell me how to do it right?
The following code is written in function.php
function get_student_list() {
$stmt = $pdo->query('SELECT studentFROM kafedra');
$stmt->execute();
foreach ($stmt as $row)
{
echo $row['name'] . "\n";
}
}
<div class="checkbox">
<input type="checkbox" id="c1" name="cc" />
<label for="c1">
<span class="filter_name">Петров</span>
</label>
</div>
Answer the question
In order to leave comments, you need to log in
I will not load you with frameworks, mvc, etc. - there are more experienced specialists here ...
But in short it is not entirely true ...
After execute, you need to do the following:
If you want to iterate, then it's done like this:
while ($row=$stmt->fetch()) {
$students[$row['id']]=$row;
}
<div class="checkbox">
<?php foreach ($students as $id => $student): ?>
<input type="checkbox" id="student_<?php echo $id;?>" value="<?php echo $id; ?>" name="students[]" />
<label for="student_<?php echo $id; ?>">
<span class="filter_name"><?php echo $student['surname']; ?></span>
</label>
<?php endforeach; ?>
</div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question