Count results of if statement for no results message

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bluebaboon
Forum Newbie
Posts: 2
Joined: Tue Jul 10, 2007 4:05 am

Count results of if statement for no results message

Post by bluebaboon »

I am trying to create a statement in PHP which counts the results of this if statement. If there are results, my if statement shows them, but I want to be able to put something that says there are no kayaks available if there are no results from my recordset that meet the requirements of this if statement.

<?php if ($row_kayaks['categoryID'] == $row_kayakcategories['kayakcategoryID']) { ?>

Anyone got any ideas?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Try using an inner join.

see: http://www.w3schools.com/sql/sql_join.asp

This will not count the occurrences but you could simply use

Code: Select all

mysql_num_rows
in order to count records.
bluebaboon
Forum Newbie
Posts: 2
Joined: Tue Jul 10, 2007 4:05 am

Post by bluebaboon »

Thanks for the suggestion - I am aware I could do it that way, however I am trying to steer clear of that, as I want the page to dynamically pull in the whole list of kayaks in the one recordset and the separate them under the kayak categories using if statements because that way my site will be more dynamic rather than having a recordset for each kayak category which would have to have a set SELECT WHERE EQUAL to value.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

I think you should follow the advice of aceconcepts and use mysql_num_rows or COUNT.
Put this in your looping function:

Code: Select all

$i=0;
$number=false;
if ($row_kayaks['categoryID'] == $row_kayakcategories['kayakcategoryID'])
{
   $number=true; 
}
else
{$number=false}
if ($number==true)
{
   $i=$i+1;
}
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Code: Select all

$i=0;
$number=false;
if ($row_kayaks['categoryID'] == $row_kayakcategories['kayakcategoryID'])
{
   $number=true;
}
else
{$number=false}
if ($number==true)
{
   $i=$i+1;
}
To condense this further you could write

Code: Select all

//INITIALISE A COUNTER TO GO INSIDE A LOOP
$i=0;

if ($row_kayaks['categoryID'] == $row_kayakcategories['kayakcategoryID'])
{
$i++; //INCREMENT THE COUNTER BY 1 EACH TIME THE "IF" CONDITION IS SATISFIED
}
Essentially, this is the same as Rovas' code...just more condensed.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

bluebaboon wrote:I am trying to steer clear of that, as I want the page to dynamically pull in the whole list of kayaks in the one recordset and the separate them under the kayak categories.
I think the most logical and simplest way to do this would be to use a nested loop - a loop within a loop.

Setup loop 1 to loop all the categories and then nest loop 2 within loop 1 extracting each kayak related to each category.

e.g.

Code: Select all

//QUERY DATABASE FOR CATEGORIES

//LOOP1
while($row=mysql_fetch_array($query)) 
{
   extract($row);
   //DISPLAY CATEGORY TITLE

   //QUERY DATABASE FOR KAYAKS e.g. WHERE kayakCatId = '$loop1_catId'
      //LOOP 2
      while($row2=mysql_fetch_array($query2))
      {
         extract($row2);
         //DISPLAY KAYAKS
       }

}
This will get all categories with the kayaks related to those categories providing you have setup your database tables correctly. To incorporate a thrid table simply alter the '$query' SQL (joins etc...).

i.e.

1. Create a table to store all the categories (tblCategory). Give each category a unique id.
2. Create a table to store all the kayaks (tblKayak). Give each kayak a unique id
3. Now create a third table to store the relationships between each category and kayak (record their unique id's).

Creating a thrid table will help normalise the database i.e. if a kayak belongs to more than one category then this third table will allow you to create as many relationships as you like.
Post Reply