Classified script help

Discussion in 'PHP' started by Fewski, Apr 19, 2007.

  1. #1
    I have a custom classified script, but it doesn't make the ads go to a different page after so many of them.

    I'm trying to get some type of code that when there are 15 ads on one page, the lower ads (Posted earlier) go to the second page, and continue that.


    Can anyone help?

    My code for the page where it shows all the ads is:

    <?php
    require('header.php');
    
    echo '<img src="images/2spacer.gif"><center><img src="images/ads.gif"></center>';
    
    //If $_GET['cat'] isn't set...
    if(!isset($_GET['cat']) || $_GET['cat'] == '')
    	echo 'No ads in this category. Please try again.'; //Print the error and stop.
    	
    //Otherwise...
    else
    {
    	//Grab all the ads in the category, and show 'em. Order them by views if $_GET['Hot'] == 'true'; else, order them by date
    	
    	 $sql->query("SELECT * FROM ads WHERE category = '%s' ORDER BY ".(($_GET['Hot'] == 'true') ? "views" : "date")." DESC", $_GET['cat']);
    	if($sql->num_rows() == 0)
    		echo 'Sorry. There are no ads in this category. Please try again later.';
    	
    	foreach($sql->getrowset() as $ad)
    	{
    		$ad2 = new CAd($sql);
    		$ad2->LoadAd($ad['ID']);
    		echo $ad2->OutputAd();
    	}
    }
    require('footer.php');
    ?>
    PHP:

    What do I need to change?

    Thanks! :)
     
    Fewski, Apr 19, 2007 IP
  2. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't know what your sql class does but if it doesn't escape your sql queries you potentially have a serious security issue with sql injection in this script.

    You'll need to do two things. Firstly count how many ads you have in the database and then return the first 15 or the second block of 15 on the second page etc.

    It looks like you can count rows using the num_rows function in your sql class and to return the subset of rows you'll need to use the LIMIT term in your sql statement.
     
    streety, Apr 20, 2007 IP