Reputation: 105
I have the following table:
ID Name Phone Email SchoolGymnasium City Password Status
1 0
I want to check if in the whole 'Directors' table in the column 'Status' exists value 0, if so to run the following HTML code:
<h1 class="page-title projects">Директори</h1>
<a href="javascript:;" id="add-project" class="btn add-project">Add Project</a>
<div id="projects-alert" class="alert">
<a href="#" class="close" title="Close">X</a>Quick tip: You can re-order projects by dragging and dropping them into place.</div>
else to run the following code:
<div id="no-projects">
<h2>В момента няма подадени заявки.</h2>
<p>Ако желаете може да добавите директор, като кликнете не бутон по-долу. Информация за вход в системата ще бъде изпратен на посоченият е-мейл.</p>
<a href="javascript:;" class="btn add-project">Добавете Директор</a>
</div>
To do this I've done the following, and it didn't happen:
<?php $jojo = mysql_query("SELECT COUNT(id) AS Status FROM Directors WHERE Status = '0'");
if(mysql_num_rows($jojo) > 0){?>
<h1 class="page-title projects">Директори</h1>
<a href="http://denismm778.dunked.com/admin/projects/new" id="add-project" class="btn add-project">Add Project</a>
<div id="projects-alert" class="alert">
<a href="#" class="close" title="Close">X</a>Quick tip: You can re-order projects by dragging and dropping them into place.</div>
<?php } else{ ?>
<div id="no-projects">
<h2>В момента няма подадени заявки.</h2>
<p>Ако желаете може да добавите директор, като кликнете не бутон по-долу. Информация за вход в системата ще бъде изпратен на посоченият е-мейл.</p>
<a href="javascript:;" class="btn add-project">Добавете Директор</a>
</div><?php }?>
The idea is this. If there is Status 0 anywhere in the column Status to show the first HTML code, else to show the second code.
Upvotes: 2
Views: 269
Reputation: 528
In your query :
SELECT COUNT(id) AS Status FROM Directors WHERE Status = '0'
You are checking if the number of rows returned are greater than 0 or not.
But your query will always return one row.
i.e the row containing the count of rows with Status as 0. So your condition will ALWAYS be true.
so instead of
if(mysql_num_rows($jojo) > 0)
use
$result = mysql_fetch_array($jojo);
if($result['Status'] > 0){
//display html
}
else{
//display alternate html
}
Upvotes: 0
Reputation: 36
Use another name for the count. Try it this way:
$query = "SELECT COUNT(id) AS count FROM Directors WHERE Status = '0'";
$results = mysql_query($query);
$values = mysql_fetch_assoc($results);
$num_rows = $values['count'];
if($num_rows > 0)
....
Upvotes: 1
Reputation: 1071
Your query will ALWAYS return a row. You need to check the value of field "Status" from that row to see if IT is >0.
Upvotes: 1