PHP Noob needs assistance

Discussion in 'PHP' started by thewindmaster, May 16, 2006.

  1. #1
    I am trying to put in vairable html base on the site:

    Page One: http://www.maxwelltrailers.com/inventory/index.php?cat=1

    Page Two: http://www.maxwelltrailers.com/inventory/index.php?cat=2

    I want test2 to appear on the second page.

    Code:

    <?
    include("common.php");
    include("header.php");
    ?>

    <?
    if($cat) $where = "AND cat='$cat'";
    $qr1 = mysql_query("SELECT * FROM product WHERE status=1 $where ORDER BY id DESC");
    while( $row = mysql_fetch_object($qr1) ){
    ?>
    <table cellpadding=5 cellspacing=0 width=100%>
    <tr>
    <td align=left width=25%>
    <?
    $handle=opendir('./uploads/');
    while (false!==($file = readdir($handle))) {
    if ($file != "." && $file != "..") {
    if( strstr($file,"x_".$row->id.".") ){
    ?>
    <a href="<?=$siteurl?>/product.php?id=<?=$row->id?>" class="nav">
    <img src="<?=$siteurl?>/uploads/<?=$file?>" border=0 width="70" height="59">
    </a>
    <?
    }
    }
    }
    ?>
    </td>
    <td align=left><div align=left>
    <strong><a href="<?=$siteurl?>/product.php?id=<?=$row->id?>" class="nav"><?=stripslashes($row->name)?></a></strong><br>
    <?=stripslashes($row->summary)?><br>
    </div></td>
    </tr>
    </table>
    <br>
    <?
    }
    ?><? if ($cat = 1){echo test1;}else{echo test2;}?>
    <?
    include("footer.php");
    ?>


    Any thoughts?
     
    thewindmaster, May 16, 2006 IP
  2. Alpha-beta

    Alpha-beta Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Shouldn't it be like this:
    
    if ($cat == 1) {
    echo test1;
    }
    else {
    
    echo test2;
    
    }
    
    PHP:
     
    Alpha-beta, May 16, 2006 IP
  3. ajscottsr

    ajscottsr Peon

    Messages:
    388
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That's definitely an error...

    To be more descriptive for the benefit of someone learning PHP, when you do:

    
    if ( $cat = 1 ) {
    
    Code (markup):
    You are basically saying the following:

    Set the value of $cat = 1 and then test to see whether that is true or not. This will ALWAYS evaluate true because you will always be able to set the value of $cat to 1 if there are no other errors in the program.

    What you want to say is:

    If and only if the value of $cat is already 1, then do the following.

    The == is a comparison. = changes the value.

    So, if you had the following:

    
    $cat = 5;  // value of $cat is now 5
    if ( $cat = 1 ) { // sets the value of $cat = 1 and will always evaluate true
         echo $cat; // echo's 1
    }
    
    Code (markup):
    As opposed to:

    
    $cat = 5; // Sets the value of $cat to 5
    if ( $cat == 1 ) { // Evaluates as false because $cat is not 1
        echo $cat;
    }
    else {
        echo "Category is not set to 1"; // This gets echo'd
    }
    
    Code (markup):
    Sorry if I rambled. -> Give a man a fish, feed him for a day. Teach him to fish, feed him for a lifetime.
     
    ajscottsr, May 16, 2006 IP
  4. thewindmaster

    thewindmaster The Man with the Plan

    Messages:
    845
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks a lot guys. This really helps.

    How would I go about putting a range of numbers.

    Say for example I wanted if $cat was equal to 1 thru 8 output test1
    If $cat was equal to 9-12 output test2
    and if $cat was equal to 13-21 output test3
     
    thewindmaster, May 17, 2006 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You need to look at something like:
    
    if (($cat <= 1) && ($cat >= 8)) {
         echo 'test1';
    }
    else if (($cat <= 9) && ($cat >= 12)) {
         echo 'test2';
    }
    else if (($cat <= 13) && ($cat >= 21)) {
         echo 'test3';
    } 
    
    Code (markup):
     
    TwistMyArm, May 17, 2006 IP
  6. thewindmaster

    thewindmaster The Man with the Plan

    Messages:
    845
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Excellent, Thanks. On my way to becoming PHP guru :)



    Edit: BTW your < and > signs were mixed up. Just in case anyone else is using this too.
     
    thewindmaster, May 17, 2006 IP
  7. ajscottsr

    ajscottsr Peon

    Messages:
    388
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Glad we could help :)

    PHP is a fun language. As you learn and do things, you mind will turn endlessly with the possibilities of applying that to other things. If you get really proficient at it, you will realize just how poorly done most things are.
     
    ajscottsr, May 17, 2006 IP