Reputation: 6625
The foreach prints out each of the headers, and I get all the accordions, but everything but the first accordion are empty. What am I missing?
$result2 = mysqli_query($con, "SELECT * FROM sections ORDER BY `order`");
$sectionnames = array();
while($row = mysqli_fetch_array($result2)) {
$sectionnames[] = $row['sectionname'];
}
$result = mysqli_query($con,"SELECT * FROM faq ORDER BY `order`");
foreach ($sectionnames as $sectionname) {
echo '<h3 id="sectionname">' . $sectionname . '</h3>';
echo '<div id="accordion">';
while($row = mysqli_fetch_array($result)) {
if ($sectionname == $row['section']) {
echo '<h3>' . $row['heading'] . '</h3>';
echo '<div>' . $row['content'] . '</div>';
}
}
echo '</div>';
}
Upvotes: 0
Views: 143
Reputation: 6625
I just needed to add this code right before the while loop
mysqli_data_seek($result, 0);
Upvotes: 0
Reputation: 2115
Without your schema I can't be sure, but it looks like faq is related to section by sectionname. If that's true, something like this:
foreach ($sectionnames as $sectionname) {
echo '<h3 id="sectionname">' . $sectionname . '</h3>';
echo '<div id="accordion">';
$result = mysqli_query($con,"SELECT * FROM faq where section = '$sectionname' ORDER BY `order`");
while($row = mysqli_fetch_array($result)) {
echo '<h3>' . $row['heading'] . '</h3>';
echo '<div>' . $row['content'] . '</div>';
}
echo '</div>';
}
Upvotes: 1
Reputation: 2154
echo '<div id="accordion">';
while($row = mysqli_fetch_array($result))
This will effectively consume every row from your query result and put it inside the first accordion.
For every other $sectionname
, your $result
will already have been exhausted and will therefore generate empty accordion
s, since you do not realize any new queries.
Upvotes: 0