2

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.

4
  • is it run no-projects div or projects-alert and what is you status field type? Commented May 24, 2013 at 14:58
  • The HTML code is not related with the whole PHP code. Commented May 24, 2013 at 15:00
  • take a look here stackoverflow.com/questions/214419/… Commented May 24, 2013 at 15:01
  • did you checked the query directly in any gui query browser .. if so what result you getting for status in gui query browser... Commented May 24, 2013 at 15:06

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I know that the problem is somewhere here but can't see it:SELECT COUNT(id) AS Status FROM Directors WHERE Status = '0'"
No the problem is NOT there. I have already told you what the problem is AND what you must do to fix it!
I don't care if the value is more than 0. I just need to know if the values > 0 are >0.
And THAT is what I have told you how to do!!!!!! If there are more than 0 values >0 then your computed field "Status" will have a value >0.
1

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)
    ....

4 Comments

He doesn't need to use another name, but I agree it would be more sensible. Your code will not work because you have a rogue space. I have been telling him to do this but he is insistent that he doesn't want to. Some times you just have to give up!
I just don't know what to fix. I really want to understand this... If you could explain me a little descriptive, I will be extremely thankful.
I HAVE explained in my answer below. You are counting the number of zeros in the status column. So the value of the computed column (whether it be called Status or count) will be >0 if there are any zeros in the real Status column. I have said that the query will ALWAYS return one row. That is why what you are trying does not work as you would wish. So now I have explained it twice.
I have corrected the rogue space. I agree with Captain Payalytic. The query will always return a row
0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.