i am trying to loop rows from my database but i want to loop the first three rows and place an ad on the fouth row then i continue with my main loop.Aany help will be helpful. Thank you

Recommended Answers

All 18 Replies

So just keep row count in a variable, and use that to determine when to insert your ad.

pls can u explain a bit further.if possible with an example

Show your code, the part that loops your results.

 <?php                        
while($row = mysql_fetch_array($result)){
    $id = $row['sale_property_id'];
    $type_id = $row['property_type_id'];
    $town = $row['town_name'];
    $meter = $row['meter_square'];
    $bed = $row['num_of_bedroom'];
    $bath= $row['bath'];
    $ftd = $row['featured'];
    $title = $row['title'];
$price = $row['price'];
    $description=$row['description'];
    $image = $row['photo_front'];
    $path = 'includes/'.''.$image;
    //$post_id = $row['post_id'];
    $post = 'Post Date';
    //echo $post_id; 


                      echo ' <div class="row">




            <!-- start property post -->
            <div class="col-lg-12 col-md-12">
                <div class="propertyItem">
                    <div class="propertyContent row">
                        <div class="col-lg-5 col-md-4 col-sm-4">
                         <span class="openHouse" style="margin-right:10px;"></span>                        <a href="property_details.php?id='.$id.'">'.($ftd == 1 ? "<a class='featured' href='property_details.php?id=$id'><span class='propertyType'>Featured</span></a>" : "") .'                     
                             <a class="propertyImgLink" href="property_details.php?id='.$id.'">
                             <img width="1000" height="700" alt="house1" class="propertyImgRow wp-post-image" src='.$path.'></a>
                        </div>
                        <div class="col-lg-7 rowText">
                        <p class="price">'.$price.'</p>                    

                            <a class="forSale" href="property_details.php?id='.$id.'">For Sale</a>                       
                             <p> <h4><a href="property_details.php?id='.$id.'">'.$title.'</a></h4></p>
                        <p><a href="">'.$town.'</a></p>                      
                          <p>'.substr($description,0,400).'</p>
                        <table class="propertyDetails">
                            <tbody><tr>
                            <td><img style="margin-right:7px;" alt="" src="icons/icon-area.png">'.$meter.'<span style="font-size:9px;"> sq ft</span></td>
                            <td><img style="margin-right:7px;" alt="" src="icons/icon-bed.png">'.$bed.' Beds</td>
                            <td><img style="margin-right:7px;" alt="" src="icons/icon-drop.png">'.$bath.' Baths</td>
                            </tr>
                        </tbody></table> 
                        </div>
                    </div>
                </div>
            </div>
            <!-- end property post -->
                </div>';
}?>

sorry if it too long. bare with me.thnx

$rowCount = 0;
while ($row = mysql_fetch_array($result)) {
    $rowCount++;

    $id = $row['sale_property_id'];
    // etc.

    echo ' <div class="row">
        // etc.
        ';

    if ($rowCount == 3)
    {
        echo 'YOUR AD HERE';
    }
}

Hi pritaeas,
you missed to reset the rowcount value if he wants to repeat the process for every 3 records.

i think so we should add the below condition

if ($rowCount == 3)
{
       echo 'YOUR AD HERE';

       $rowCount = 0; // added here
}

advice me if i am wrong

worked. thnx sir

@ihelpyou

if he wants to repeat the process for every 3 records.

That assumption wasn't clear from the OP, so I left it out on purpose.

pls i guess it was working well. But now i want to call both from a database and it seems the ad loop is calling all the rows before the next three rows come.Any help pls

hello team.Any help would be appreciated

Can you explain? It is not clear what your problem is.

ok then. i have a page which fetches listing from a database. Now between every three listing there is suppose to be an ad which is also coming from the database but different tables. Now am able to fetch results from the database but instead of one add at a time it fetches all the ads after first three rows. then loos that again. but is not wat i wanted

Are you saying that you want the database retrieval to be three records at a time, rather than getting them all them you processing them 3 at a a time? Why? Do you have shares in a server hardware company?

No what i mean is i mean is after every of 3 records display one row from a databse then continue your initial loop.Then it goes on and on

Member Avatar for diafol

THis isn't that clear. Maybe something like this? Assume $regularData and $adData are the DB result arrays.

for($i=0;$i<count($regularData);$i++)
{
    if($i > 0 && $i % 3 == 0 && isset($adData[$j]))
    {
        $data = $adData[$j];
        echo include('includes/advert_template.php');
        $j++;
    }
    $data = $regularData[$i];
    echo include('includes/regular_template.php');
}

Few ways to do this I'm sure. I'm hiding the "templates" in include files and populating the "placeholders" with $data items. E.g. regular_template.php:

<?php
$ftd = ($data['ftd'] == 1) ? "<a class='featured' href='property_details.php?id={$data['id']}'><span class='propertyType'>Featured</span></a>" : "";
$description = substr($data['description'],0,400);

$template = <<<EOT

<div class="col-lg-12 col-md-12">
    <div class="propertyItem">
        <div class="propertyContent row">
            <div class="col-lg-5 col-md-4 col-sm-4">
                <span class="openHouse" style="margin-right:10px;"></span> 
                <a href="property_details.php?id={$data['id']}">
                $ftd
                <a class="propertyImgLink" href="property_details.php?id={$data['id']}">
                    <img width="1000" height="700" alt="house1" class="propertyImgRow wp-post-image" src="{$data['path']}">
                </a>
            </div>
            <div class="col-lg-7 rowText">
                <p class="price">{$data['price']}</p>
                <a class="forSale" href="property_details.php?id={$data['id']}">For Sale</a>
                <h4><a href="property_details.php?id={$data['id']}">{$data['title']}</a></h4>
                <p><a href="">{$data['town']}</a></p>
                <p>$description</p>
                <table class="propertyDetails">
                    <tbody>
                        <tr>
                            <td><img style="margin-right:7px;" alt="" src="icons/icon-area.png">{$data['meter']}<span style="font-size:9px;"> sq ft</span></td>
                            <td><img style="margin-right:7px;" alt="" src="icons/icon-bed.png">{$data['bed']} Beds</td>
                            <td><img style="margin-right:7px;" alt="" src="icons/icon-drop.png">{$data['bath']} Baths</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
EOT;
return $template;
        <?php
                            $sql="select * from kama_rate";
                            $result=mysql_query($sql);
                            $row=mysql_fetch_assoc($result);
                            $ratio = $row['ration'];

        ?>
 <?php 

                            $rec_limit = 10;
                            $sql = "SELECT count(sale_property_id) FROM kama_property_sale ";
$retval = mysql_query( $sql,$connect);
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];

if( isset($_GET{'page'} ) )
{
   $page = $_GET{'page'} + 1;
   $offset = $rec_limit * $page ;
}
else
{
   $page = 0;
   $offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);

                            $sql="select * from kama_property_sale,kama_towns where status=1 && kama_property_sale.location_id=kama_towns.town_id  ORDER BY sale_property_id DESC LIMIT $offset, $rec_limit";
                            $result=mysql_query($sql);


                            ?>
        <!-- start properties loop -->



  <div class="row"> 



                <div class="col-lg-9 col-md-9">
                    <!-- start properties loop -->
                             <?php
                             $rowCount = 0;                      
while($row = mysql_fetch_array($result)){
    $rowCount++;
    $id = $row['sale_property_id'];
    $type_id = $row['property_type_id'];
    $town = $row['town_name'];
    $meter = $row['meter_square'];
    $bed = $row['num_of_bedroom'];
    $bath= $row['bath'];
    $ftd = $row['featured'];
     $verified = $row['verified'];
    $title = $row['title'];
    $currency = $row['currency'];
$price = $row['price'];
    $description=$row['description'];
    $image = $row['photo_front'];
    $path = 'includes/'.''.$image;
    //$post_id = $row['post_id'];
    $post = 'Post Date';
    //echo $post_id; 


                      echo ' <div class="row">       
            <!-- start property post -->
            <div class="col-lg-12 col-md-12">
                <div class="propertyItem">
                    <div class="propertyContent row">
                        <div class="col-lg-5 col-md-4 col-sm-4">
                         <span class="openHouse" style="margin-right:10px;"></span>                        <a href="property_details?id=' . $id . '">' . ($ftd == 1 ? "<a class='featured' href='property_details?id=$id'><span class='propertyType'><span style='padding-left:70px;'><img src='images/yellowcallout.png'/></span></span></a>" : "") . '                     
                             <a class="propertyImgLink" href="property_details?id=' . $id . '">
                             <img width="1000" height="700" alt="house1" class="propertyImgRow wp-post-image" src=' . $path . '></a>
                        </div>


                        <div class="col-lg-7 rowText">
                        ' . ($verified == 1 ? "<span><img src='images/veri.png' style='margin-bottom:10px; padding-left:50px; padding-top:10px;' data-toggle='tooltip_veri' title='Verified' data-placement='top' /></span>" : "") . '
                         <p class="price"><span style="font-size:18px">GH¢ ' . ($currency == 2 ? number_format($price*$ratio) : number_format($price)) . '</span></p>                    

                            <a class="forSale" href="property_details?id='.$id.'" style="margin-top:12px;">For Sale</a>                       
                             <h4><a href="property_details?id='.$id.'" style="font-weight:bold"> '.$title.'</a></h4><br>
                        <br/><div class="row">
                        <div class="col-md-8">
                        <a href="property_details?id='.$id.'">'.$town.'</a></div></div>                      
                          <br/><p>'.substr($description,0,400).'</p>
                        <table class="propertyDetails">
                            <tbody><tr>

                            <td><img style="margin-right:7px;" alt="" src="images/bed.png">' . $bed . ' Beds</td>
                            <td><img style="margin-right:7px;" alt="" src="images/photogrey.png">' . $bath . ' Baths</td>
                            <td><img style="margin-right:7px;" alt="" src="images/IMG2.png">' . $meter . '<span style="font-size:9px;"> sq ft</span></td>
                            </tr>
                        </tbody></table> 
                        </div>
                    </div>
                </div>
            </div>
            <!-- end property post -->
                </div>';
                if ($rowCount == 3)
    {
        $query="select * from kama_ads,kama_firms where kama_ads.company_id=kama_firms.company_id";
        $group=mysql_query($query);
         $rowCount1 = 0;        
        while($lane = mysql_fetch_array($group)){
            $rowCount1++;
        $id = $lane['ads_id'];
    $com_id = $lane['company_id'];
    $name = $lane['name'];
    $image = $lane['image'];
    $website = $lane['website'];
    $location = $lane['location'];
    $description = $lane['description'];
    $phone = $lane['phone'];
        echo '<div class="row">          
            <!-- start property post -->
            <div class="col-lg-12 col-md-12">
                <div class="propertyItem">
                    <div class="propertyContent row">
                        <div class="col-lg-5 col-md-4 col-sm-4">
                         <span class="openHouse" style="margin-right:10px;"></span>                        <a href="'.$website.'" target="new">' . "<a class='featured' href=''.$website.'' target='new'><span class='propertyType'>'.$phone.'</span></a>" . '                     
                             <a class="propertyImgLink" href="'.$website.'" target="new">
                             <img width="1000" height="700" alt="house1" class="propertyImgRow wp-post-image" src="images/sale.jpg"></a>
                        </div>


                        <div class="col-lg-7 rowText">
                         <p class="price"><span style="font-size:18px">'.$name.'</span></p>                    


                             <h4><a href="'.$website.'" target="new" style="font-weight:bold">'.$website.'</a></h4><br>
                        <br/><div class="row">
                        <div class="col-md-8">
                        <a href="'.$website.'" target="new" >'.$location.'</a></div></div>                      
                          <br/><p>'.$description.'</p>
                        <table class="propertyDetails">
                            <tbody><tr>
                            <td><img style="margin-right:7px;" alt="" src="icons/icon-area.png"><span style="font-size:9px;"> </span></td>
                            <td><img style="margin-right:7px;" alt="" src="icons/icon-bed.png"></td>
                            <td><img style="margin-right:7px;" alt="" src="icons/icon-drop.png"></td>
                            </tr>
                        </tbody></table> 
                        </div>
                    </div>
                </div>
            </div>
            <!-- end property post -->
                </div>';
         $rowCount = 0;     
    }
    }
}?>

Sorry though just wanted you to have an idea of wat i wanted to do.thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.