How do you echo content before the while loop (PHP & MySQL)?

I’m trying to figure out how to echo out content above the while loop and underneath the check for num_rows, but I need to get the content from the while loop before doing that.

$sql = "SELECT * FROM table WHERE column = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param('s', $test);
$result = $stmt->execute();
$stmt_result = $stmt->get_result();
if ($stmt_result->num_rows > 0) {
echo "<div id='wrapper'>"; //I need to echo out rows here if $status === 2
    while ($row = $stmt_result->fetch_assoc()) {
       $title = $row['title'];
       $description = $row['descript'];
       $status = $row['status'];
       if ($status === 2) {
          echo $status;
          continue; //skip to the next iteration
       }
       echo $title;
       echo $description;
	}
}

How is it done?

To summarize, this is the output I’m looking for:

//if status !== 2: (i get 3 results)
<div id='wrapper'>
//title
//description

//title
//description

//title
//description
</div>
//if status === 2: (i get 1 result)
<div id='other_wrapper'>
//title
//description
</div>
//if status === 3: (i get 5 results)
<div id='yet_another_wrapper'>
//title
//description

//title
//description

//title
//description

//title
//description

//title
//description
</div>

what do you need to echo out?

Html content, or anything. As long as it can be done before the while loop and after the num_rows check…

you already output html (div id wrapper) between that condition and the loop…? im not sure i understand the issue

It must fulfil that condition ^

oh, you wrote num_rows in the last post which is outside the loop so it was a bit confusing

but cant each row have a different status? if you want to do something based on that value before the loop you need to decide which row should be looked at.

i think it would help if you posted an example of your current output and what you wanted it to be

Okay, i edited the post.

do you want to output the rows grouped together in wrappers based on their status? sorry if im being dumb

Yes, that’s exactly right

Ok there are quite a few ways to achieve that. A simple method is to change your loop that fetches rows from the db from outputting the data to organizing the data for the grouped lists.

As an example the code below will build the following array

$wrappers = [
   'wrapper' => [/* items from db with status not 2 or 3 */]
   'other_wrapper' => [/* items from db with status 2 */]
   'yet_another_wrapper' => [/* items from db with status 3 */]
];

we can then easily iterate over the wrapper names, and then in each wrapper iterate over the items.

As long as you don’t have a lot of records this double looping should be fine.

<?php
function getWrapperNameForStatus($status) {
	if($status === 2) {
		return 'other_wrapper';
	}
	
	if($status === 3) {
		return 'yet_another_wrapper';
	}
	
	return 'wrapper';
}

$wrappers = [];
while ($row = $stmt_result->fetch_assoc()) {
	$wrapperName = getWrapperNameForStatus($row['status']);
	
	$wrappers[$wrapperName][] = [
		'title' => $row['title'],
		'description' => $row['description'],
		'status' => $row['status']
	];
}

?>

<div>some html</div>
<?php foreach ($wrappers as $wrapper => $items): ?>
	<div id="<?= $wrapper ?>">
		<?php foreach ($items as $item): ?>
			<h2><?= $item['title'] ?></h2>
			<p><?= $item['description'] ?></p>
		<?php endforeach; ?>
	</div>
<?php endforeach; ?>
1 Like

Jim, you are officially a genius :slight_smile:
From all the solutions I tried this one is by far the best, and the cleanest, plus this way you’re not limited to how much content can be put into each wrapper.

Sponsor our Newsletter | Privacy Policy | Terms of Service