Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Conditional region based on record numbers

Posted on 2005-03-29
Medium Priority
Last Modified: 2008-03-06
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

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?

Question by:cbeaudry1
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Accepted Solution

Andy earned 1000 total points
ID: 13659543
Check the DB for blank answers...

$query_blanks = "SELECT * FROM table_name WHERE answer = NULL;
$blanks = mysql_query($query_blanks, $conn) or die(mysql_error());
$totalRows_blanks = mysql_num_rows($blanks);

Then enclose your form in an If block...

if($totalRows_blanks < 5){
     // Form HTML

Expert Comment

ID: 13659571
$iDisplay = 0;

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

$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) {


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

Author Comment

ID: 13662368
Reapz posted first and should also got full points since his answer had less code.

Expert Comment

ID: 13664276
Glad to help :)

Author Comment

ID: 13666475
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....

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question