Jump to content

Break articles into two sections


dachshund
 Share

Recommended Posts

Hi,

 

On our blog homepage we currently have 12 articles displayed. After 12 articles, I could like to add in an advert, followed by another 12 articles. I just wondered if there's an easy way to do this? Here's our current code:

 

 

$oldarticles = 12 * $pagenumber - 12;
$sql = "SELECT * FROM content WHERE `live` LIKE '0' AND '$today' > `date` ORDER BY date DESC LIMIT $oldarticles,12";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){

 

Thanks

Link to comment
Share on other sites

your pagination should retrieve the total number of rows you want to display on the page and it should use a variable (or defined constant) to specify the number of rows so that you don't have a hard-coded value in three different places in the code (and if you apply a little algebra to your equation, you only need to use the value in two different places.)

 

next, as you are looping over the data to display it, you would either have a loop counter and a conditional test to check the value in the counter or you can use array_chunk() to break up the result set into chunks of the size you want. if you only want to display an advert once, you would test the absolute value of the counter. if you want to display an advert every n number of blog posts, you would use the Modulus operator to test the value of the counter. you would also test if the number of rows in a result set is less then the point where you want to display the advert, for example the last page of results, then display it once on the end of the output.

 

lastly, the php mysql_ extension is obsolete and has been removed from php. you need to convert your code to use the php PDO extension, and use prepared queries to supply data values to your sql query statement.

Link to comment
Share on other sites


<?php
 
$recordsPerPage = 24;
$adPosition = 12;
 
$limitStart = $recordsPerPage * ($pagenumber - 1);
 
$sql = "SELECT *
        FROM content
        WHERE `live` LIKE '0' AND '{$today}' > `date`
        ORDER BY date DESC
        LIMIT {$limitStart}, {$recordsPerPage}";
$result=mysql_query($sql);
 
 
$recordCount = 0;
while($rows=mysql_fetch_array($result))
{
    //Increment the counter
    $recordCount++;
 
    //Code to display the current record goes here
    ##
    ##
 
    //Check if this is the 12th record
    if($recordCount == $adPosition)
    {
        //Code to display the ad goes here
        ##
        ##
    }
}
 
?>
Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.