Results 1 to 7 of 7
  1. #1

    Inserting advertising between rows php

    Hi, I have a bit of a conundrum. I am trying to get my site page to look like this:

    First 5 rows
    (advertisment)
    Next 10 rows
    (advertisment)

    Can anyone advise the easiest (least code required) way to achieve this? I tried to use row-id but I have multiple row categories so the id is not consistent (eg. it might go 6,7,43,2,18 etc. which is where I got stuck). I'm wondering how I can get it to count the rows as the number the query has outputted so far instead of the db id's...

  2. #2
    try:
    PHP Code:
    $query "SQL statement";
    $counter 1;
    // assuming that you have more than 5 entries
    while(mysql_fetch_array($query))
    {
        
    $counter++;
        
    $result_row .= "...";
        if (
    $counter == 5)
        {
            
    $result_row .= "ads code";
            
    $counter 1;
        }
    }
    print 
    $result_row

  3. #3
    That didn't work, but I got it working by declaring an int variable outside the repeat region loop and getting it to increment by 1 inside the repeat region loop and then put the ad in the repeat region loop for conditions if counter1 = 5 or 15 etc.

  4. #4
    Join Date
    Sep 2005
    Location
    Southern California
    Posts
    179
    A more logical approach is to use mod instead of a bunch of hard coded numbers...

    PHP Code:
    $query "SELECT...";
    $i 0;

    while(
    mysql_fetch_array($query))
    {
        
    //print the row here
        
    if ( ++$i == )
            
    //print the ad here


  5. #5
    Using mod would confine it to having an ad every x number of rows rather than having one towards the top, one towards the middle and one towards the bottom. But I can see how that could be a more logical and better spread out approach.

  6. #6
    Join Date
    Sep 2005
    Location
    Southern California
    Posts
    179
    Quote Originally Posted by FG-alex View Post
    Using mod would confine it to having an ad every x number of rows rather than having one towards the top, one towards the middle and one towards the bottom. But I can see how that could be a more logical and better spread out approach.
    Well if you only want one at the top/middle/bottom then you could use mysql_num_rows() and print the advertisement when $i is 0, numrows/2, or numrows.

    Based on your example i figured you wanted it every 5 rows, so my mistake there.

  7. #7
    Well it could work either way, I may stick to the 5 every rows and change it if I need to later, at least i know the gist of the code now. Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •