SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    '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:

    PHP Code:
    $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.

    Code:
    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.

  2. #2
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,268
    Mentioned
    116 Post(s)
    Tagged
    1 Thread(s)
    Considering you selected for that specific category in your query, that if statement will always be true....
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  3. #3
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm what do I use instead of:
    PHP Code:
    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

  5. #5
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,268
    Mentioned
    116 Post(s)
    Tagged
    1 Thread(s)
    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 (parent_Category='$parentcat') 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....
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Instead of the while, put an if. After all, a while is practically an if statement that can run more than once:
    PHP Code:
    $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.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    PHP 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

  9. #9
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Use mysql_num_rows() to check if any rows are returned by the query:
    PHP Code:
    <?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
        

      } 
    ?>

  10. #10
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  11. #11
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by multichild View Post
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •