Link to home
Start Free TrialLog in
Avatar of cbeaudry1
cbeaudry1

asked on

Conditional region based on record numbers

I'm using PHP with MySQL for a question/answer section of a website. The process is that a user will ask a question. The person answering logs into a CMS and deyermines whether or not the question should be answered or deleted. We get many repeat questions....

The simple way of determining whether the question should be displayed on the website is to see if there is any value inside the answer section. If the question hasn't been answered it simply doesn't display.

What I would like to do, however, is to have the "ask a question" section be disabled if there are 5 questions waiting to be answered. So in effect, the logic would be along the lines of:

    If there are five records with blank "answer" fields, then do not display form
    else display form
    end

I'm rather rusty on PHP but I basically need a routine tthat will go through all the records and see if there are 5 records with null values in the answer field. If so, that would disable the user form. If not, the user form would appear. Make sense? Good! How do I do it?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Andy
Andy
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of esculentus
esculentus

<?php
$iDisplay = 0;

mysql_connect('host', 'username', 'password') Or die('Error opening connection to mySQL server.');
mysql_select_db('yourdb');

$mSQL = "SELECT * FROM tableName";
$mQuery = mysql_query($mSQL);
$iRows = mysql_num_rows($mQuery);

$iCount = 0;

for($i=0;$i<$iRows;$i++) {
   if($iCount > 4) {
      $iDisplay = 1; //in this case there are more than 5 unanswered questions so do what you will
      break 1;
   }
   $mResult = mysql_fetch_row($mQuery);
   if($mResult[fieldNumberYouWantToCheck] == null) {
      $iCount++;
   }

}
mysql_close($mDB);

if($iDisplay == 1) {
//html code follows
?>
Put your html code here

<?
}

if($iDisplay == 0) {
//html code follows
?>
Put your html code here for the other case
<?
{
?>

I think something like that should work for you, if not then I appologize
Avatar of cbeaudry1

ASKER

Reapz posted first and should also got full points since his answer had less code.
Glad to help :)
I forgot to mention that the following line needed to be fixed a bit to work:

$query_blanks = "SELECT * FROM table_name WHERE answer = NULL;

changed to

$query_blanks = "SELECT * FROM table_name WHERE answer IS NULL";

The rest was fine....