I have something strange going on here and it doesnt make sense.
I have an if else statement, as below:
$parentcat = $_GET['cat_Id'];
<?php $qw=mysql_query("select * from sub_category where (parent_Category='$parentcat') order by sub_Name ASC") or die (mysql_error());
while($r=mysql_fetch_assoc($qw))
{
if($r['parent_Category']==$parentcat)
{ ?>
<p style="font-weight:bold; text-decoration:underline;">Sub_Categories:</p>
<? } else { ?>
<p>stock instead</p>
<? }?>
<? } ?>
and this is the url.
category.php?cat_Id=2&choice=Furniture
Whats happening is I have got three entries at the moment in table sub_category, and what I would liek to happen is that when field sub_category matches up with the value of cat_Id from the url path it outputs all the entries in that field, which it doesm but what it isnt doing is recognising that when there isnt a field there that matches up with cat_id its not outputting the else statement. I dont get it.
I don’t think you’re understanding what I’m saying. What I’m saying is you’ve got this SQL statement (take note of bolded portion):
select * from sub_category where B[/B] order by sub_Name ASC
then you’ve got this if statement.
if($r[‘parent_Category’]==$parentcat)
So, if you select all the records that have that parent category (which is what your SQL statement has), your if statement will ALWAYS be true since that’s the condition you specified. I think your logic behind your query is flawed, but since I don’t know your table structure nor what you’re exactly trying to do (though I have a good guess - you’re building a hierarchical structure), I can’t give you any more information that trying to help you identify where your logic is flawed…
Instead of the while, put an if. After all, a while is practically an if statement that can run more than once:
$parentcat = $_GET['cat_Id'];
$qw=mysql_query("select * from sub_category where (parent_Category='$parentcat') order by sub_Name ASC LIMIT 1") or die (mysql_error());
if($r = mysql_fetch_assoc($qw)){
?>
<p style="font-weight:bold; text-decoration:underline;">Sub_Categories:</p>
<?php
}
The if statement will pass if whatever’s returned from the mysql_fetch_assoc function isn’t the array you’re expecting.
OK good news I got it working so that it can decide which to out put, but I have a slight problem involving a title thats present in the while loop, which I would like only outputted once, rather than everytime it outputs a vale from the database in the while loop.
Here is the code:
$grab=0;
<?php
$qw=mysql_query("select * from sub_category where (parent_Category='$parentcat')");
while($r=mysql_fetch_assoc($qw)){
?>
<p style="font-weight:bold; text-decoration:underline;">Sub_Categories:</p>
<a href="#" title="<?=$r['sub_MetaTitle']?>"><?=$r['sub_Name']?></a> |
<? $grab=1;
}
if ($grab==0)
{ ?>
<p>stock instead</p>
<? } ?>
Here is the website where Im workign on it, so you can see.
w w w .accendsandbox.co.uk/category.php?cat_Id=2&choice=Furniture
The sub categories work on the last category ‘furniture’ so you can see for yourself. Is there a way around this.