Jump to content

if statement help needed


Recommended Posts

Hi all,

 

I hope someone can help me on this and many thanks in advanced if you can.

 

My website is a news site set up in to 4 section of news (sport, travel, entertainment and news) that is inserted into one database.  Each section is drawn onto each respective page via a while clause (see below) sometimes one of the sections of 4 are empty.  if it is empty i need it to recognize this and echo  'There are currently no jobs available in this section'

 

i'm trying to use an if clause but i can't seem to get this to work  :-[

 

 

<?php

$query = 'SELECT * FROM recruitment WHERE sector = "sport" ORDER BY recruitment_id DESC LIMIT 0,5';

 

$result = mysql_query ($query);

 

while ($row = mysql_fetch_array($result)){

 

echo '<img src='. "images/" . $row ['title] . '"align="right"/>';

echo "<span class=tittleheader>" . $row ['article']  ."</span>" . '<p/>';

 

}

if (empty($row ['section'])){

echo "<span class=bluetwo>" . 'There are currently no jobs available in this section' . "</span>";

 

}

 

?>

Link to comment
https://forums.phpfreaks.com/topic/206873-if-statement-help-needed/
Share on other sites

Have you tried to echo out your $row['section'] to see what data is being displayed, if any?  Ensure that your data is not containing NULL or anything like that, as on that basis, it may not actually be "empty".

 

If you can check that, it may help solve your query.

 

PS, can you please put code into the code tags?  ;)

I believe you're after something like this. It will see if there results before allowing the main query to run. If there are no results, the error is displayed. You'll need to change the field name in COUNT(`id`) to match an actual field (preferably an index) in your database.

 

<?php
$check = "SELECT COUNT(`id`) AS check FROM `recruitment` WHERE `sector` = 'sport'";
$ch_res = mysql_query($check);
$array = mysql_fetch_row($ch_res);
if( $array[0] > 0 ) {
$query = 'SELECT * FROM recruitment WHERE sector = "sport" ORDER BY recruitment_id DESC LIMIT 0,5';
$result = mysql_query ($query);

while ($row = mysql_fetch_array($result)){
	echo '<img src="'. "images/" . $row ['title'] . '"align="right"/>';
	echo "<span class=tittleheader>" . $row ['article']  ."</span>" . '<p/>';
}
} else {
echo "<span class=bluetwo>" . 'There are currently no jobs available in this section' . "</span>";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.