'if' working in 'if else' statement, but not 'else'

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.

Considering you selected for that specific category in your query, that if statement will always be true…

right, mmm thats going to be a problem as I’m short on what to use to call in the statement.

Thanks for replying though

Umm what do I use instead of:


while($r=mysql_fetch_assoc($qw))

To only make one query rather than using while, I got somehting in mind and this might work.

Cheers

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.

Sure, sorry Dave I can see what your saying now.

What is the action when in phpmyadmin, I can run sql to output the table, so I can post in on here.

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.

Cheers

Use mysql_num_rows() to check if any rows are returned by the query:


<?php 
  $qw=mysql_query("select * from sub_category where (parent_Category='$parentcat')");
  if (mysql_num_rows($qw) == 0) {
?>
    <p>stock instead</p>
<?php
  } else {
?>
    <p style="font-weight:bold; text-decoration:underline;">Sub_Categories:</p>
<?php
    while ($r=mysql_fetch_assoc($qw)) { 
?>
      <a href="#" title="<?=$r['sub_MetaTitle']?>"><?=$r['sub_Name']?></a> | 
<?php
    } 
  } 
?>

Hi guido2004,

It makes me laugh it does. I struggle for days and make progress, then you post your code, its fixed and better and all in a few minutes.

So grateful for your input always, thanks again. This forum makes my learning enjoyable rather than a worry.

Cheers

That’s good to hear. I’ve learned it the same way, and a lot of what I know I learned here as well :slight_smile: