dachshund Posted March 17, 2017 Share Posted March 17, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/303474-break-articles-into-two-sections/ Share on other sites More sharing options...
mac_gyver Posted March 17, 2017 Share Posted March 17, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303474-break-articles-into-two-sections/#findComment-1544293 Share on other sites More sharing options...
dachshund Posted March 17, 2017 Author Share Posted March 17, 2017 very much appreciate your response! that's gone way over my head though - beyond my current capabilities I think Quote Link to comment https://forums.phpfreaks.com/topic/303474-break-articles-into-two-sections/#findComment-1544297 Share on other sites More sharing options...
dachshund Posted March 17, 2017 Author Share Posted March 17, 2017 Is there any easier way to do it. For example something that says after 12th article show this advert div? Quote Link to comment https://forums.phpfreaks.com/topic/303474-break-articles-into-two-sections/#findComment-1544301 Share on other sites More sharing options...
Psycho Posted March 17, 2017 Share Posted March 17, 2017 (edited) <?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 March 17, 2017 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/303474-break-articles-into-two-sections/#findComment-1544309 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.