Page 1 of 1
Count results of if statement for no results message
Posted: Tue Jul 10, 2007 4:07 am
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?
Posted: Tue Jul 10, 2007 5:36 am
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
in order to count records.
Posted: Tue Jul 10, 2007 6:04 am
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.
Posted: Tue Jul 10, 2007 6:14 am
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;
}
Posted: Tue Jul 10, 2007 7:45 am
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.
Posted: Tue Jul 10, 2007 8:01 am
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.