I am brand new to PHP, and am creating a page with job postings separated by category. I would like for a message "Sorry, there are currently no openings in this division." to display under the category if there are no jobs in the database that match that category. Each row in the table will have some category assigned, so the code I have written checks for the value and puts the associated information in the div. IE - retail jobs go into Div id="retail". Wholesale jobs go into div id="wholesale"
I have written the following code but it doesn't work.
There are jobs in the table with the jobcat Retail, but the Sorry message appears 3 times underneath the first record returned, outside of a div with the class "job-details"
There are no jobs in the next category in the table, and with this code the "Sorry" message repeats 5 times.
Can anyone help me achieve what I am trying to do please? I have not found any similar questions where an echo is repeating like this but I may not be using the right search terms.
The code below repeats 4 times on my page, with different categories for jobcat.
<div id="retail"><h2>Retail</h2>
<?php do {
if ($row_info['jobcat'] == 'Retail'){
echo "<div class='job-post'>";
echo "<p><span class='header'>Position: </span>".$row_info['jobtitle'] . "</p>";
echo "<p><span class='header'>Location: </span>".$row_info['joblocation'] . "</p>";
echo "<p class='view-job'>View Details</p> <div class='job-details'>".nl2br($row_info['jobdesc']) ." <a href='job-application.php?id=".$row_info['id']."'>Apply</a></div>";
echo "</div>";
} else{
echo "<p>Sorry, there are currently no openings in this division.</p>";
}
} while ($row_info = mysql_fetch_assoc($info));
mysql_data_seek($info, 0);
$row_info = mysql_fetch_assoc($info);
?>
</div>
I tried modifying based on the answer below to check for the value rather than empty, and this isn't working either - surely its poor implementation on my part but wanted to show anyway. With this code, the first category pulls in the correct data, but the Sorry message shows for all other categories, even if there is a match in the table:
<?php
if ($row_info['jobcat'] == 'Retail')
{
while ($row_info = mysql_fetch_assoc($info)) {
echo "<div class='job-post'>";
echo "<p><span class='header'>Position: </span>".$row_info['jobtitle'] . "</p>";
echo "<p><span class='header'>Location: </span>".$row_info['joblocation'] . "</p>";
echo "<p class='view-job'>View Details</p> <div class='job-details'>".nl2br($row_info['jobdesc']) ."<a href='job-application.php?id=".$row_info['id']."'>Apply</a></div>";
echo "</div>";
}
}
else
{
echo "Sorry, there are no open positions at this time.";
}
mysql_data_seek($info, 0);
$row_info = mysql_fetch_assoc($info);
?>