I am looking for help with IF statements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
firephoenix
Forum Newbie
Posts: 5
Joined: Sat Sep 29, 2007 5:20 pm

I am looking for help with IF statements

Post by firephoenix »

I am trying to make a pge pull a list from a mysql db and display results I also have buttons to sort by the different types and I am using 4 pages with 36 records each i am using $_GET such as index.php?page=1 and so on... also the ability to list only record from a specific company such as ford chevy and what not here is the code I have

Code: Select all

<?php
      mysql_connect($dbhost,$dbuser,$dbpass) or die("Unable to connect");
			@mysql_select_db($dbname) or die ("Unable to select database");
			  if ($page = '1') {
        $query="SELECT * from $tbl_listings LIMIT 0, 36";
        
        }
        else
        {
        
        }
        
        if ($page = "2"){$query="SELECT * from $tbl_listings LIMIT 36, 72";
        
        }
        else
        {
        }
        
        if ($page = "3"){$query="SELECT * from $tbl_listings LIMIT 72, 144";
        
        }
        else
        {
        }
        if ($page = "4"){$query="SELECT * from $tbl_listings LIMIT 144, 288";
        
        }
        else
        {
        }
        
        if ($company <> ""){
        
        $query="SELECT 
                * 
        FROM 
                $tbl_listings 
        WHERE 
                `make`='$company'";
        }
        else
        {
        }
        
        if ($orderby = "`year`"){$query="SELECT * from $tbl_listings ORDER BY `listings`.`year` ASC";
        }
        else
        {
        }
        
        
      $result=mysql_query($query);
     
      $num=mysql_numrows($result);

      
      
      $i=0;
			while ($i < $num) {

			$id=mysql_result($result,$i,"id");
                                                $milage=mysql_result($result,$i,"milage");
			$model=mysql_result($result,$i,"model");
                                                $year=mysql_result($result,$i,"year");
                                                $price=mysql_result($result,$i,"price");
			$imgmain=mysql_result($result,$i,"imgmain");
       

      ?>
        <tr>
        <td width="34%" height="20" style="border-left-width: 1; border-right-width: 1; border-top-width: 1"   bordercolor="#FFFFFF">
        <center>
        <table border="1" cellpadding="2" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="50%">
        <tr>
            <td width="100%">
            <p align="center">
            <a href="http://">
            <img border="0" src="<?php echo $imgmain; ?>" width="150" height="100"></a><br>
            [<a style="font-family: Verdana; text-decoration: none; font-size: 8pt"href="loadcar.php?id=<?php echo $id; ?>" onClick="OpenPopup('loadcar.php')">Enlarge</a>]<br><a style="font-family: Verdana; text-decoration: none; font-size: 8pt"href="emainform.php?id="<?php echo $id; ?></a></td>
         </tr>
        </table>
        </center>
        </td>
        <td width="16%" height="20" style="border-left-width: 1; border-right-width: 1; border-top-width: 1" bordercolor="#FFFFFF">
        <p align="center"><?php echo $year; ?></td>
        <td width="16%" height="20" style="border-left-width: 1; border-right-width: 1; border-top-width: 1" bordercolor="#FFFFFF">
        <p align="center"><?php echo $model; ?></td>
        <td width="17%" height="20" style="border-left-width: 1; border-right-width: 1; border-top-width: 1" bordercolor="#FFFFFF">
        <p align="center"><?php echo $milage; ?></td>
        <td width="17%" height="20" style="border-left-width: 1; border-right-width: 1; border-top-width: 1" bordercolor="#FFFFFF">
        <p align="center">$<?php echo $price; ?></td>
      </tr>     
       <?
	    $i++;
	    }
      mysql_close(mysql_connect($dbhost,$dbuser,$dbpass));
      ?>
Now it doesnt seem to work and I think I am doing the if's wrong but im not sure Can someone help?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Hm... You need some reading about PHP (and programming) basics.
0. You could separate your SQL query in several clasues - LIMIT, ORDER, WHERE ... Try to generate the SQL query by defining each of it in separate IF statement.
1. Your approach for doing paging is wrong ... Follow this idea: generate the LIMIT clause by using ( ($page-1)*36, $page*36 )
2. I think there is no such operator in PHP - "<>". I's Pascal style ... In PHP you need "!=" or "!==" ()PHP Operators. Then you could generate the WHERE clause.
3. "=" is for setting value, and "==" is for checking for equality, don't mistake them in IF statements.
4. After you've genarated the 3 SQL parts (WHERE, ORDER BY, LIMIT) in separate string variables concatenate them in a regular SQL query.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply