Search code examples
phpsearch-engine

Show Ads In Middle Of Results


I'm currently using sphider on one of my websites, my questions is how can I break the results page into 2 parts to add a 200px break to place a ad slot.

Code:

    <?php 
extract($search_results);
?>

<?php if ($search_results['did_you_mean']){?>
    <div id="did_you_mean">
    <?php echo $sph_messages['DidYouMean'];?>: <a href="<?php print 'index.php?query='.quote_replace(addmarks($search_results['did_you_mean'])).'&search=1'?>"><?php print $search_results['did_you_mean_b']; ?></a>?
    </div>
<?php  }?>  

<?php if ($search_results['ignore_words']){?>
    <div id="common_report">
    <?php while ($thisword=each($ignore_words)) {
        $ignored .= " ".$thisword[1];
    }       
    $msg = str_replace ('%ignored_words', $ignored, $sph_messages["ignoredWords"]); 
    echo $msg; ?>
    </div>
<?php  }?>  


<?php if ($search_results['total_results']==0){?>
    <div id ="result_report">
        <?php 
        $msg = str_replace ('%query', $ent_query, $sph_messages["noMatch"]);
        echo $msg;
        ?>
    </div>
<?php  }?>  


<?php if ($total_results != 0 && $from <= $to){?>
    <div id ="result_report">
    <?php  
    $result = $sph_messages['Results'];
    $result = str_replace ('%from', $from, $result);
    $result = str_replace ('%to', $to, $result);
    $result = str_replace ('%all', $total_results, $result);
    $matchword = $sph_messages["matches"];
    if ($total_results== 1) {
        $matchword= $sph_messages["match"];
    } else {
        $matchword= $sph_messages["matches"];
    }

    $result = str_replace ('%matchword', $matchword, $result);   
    $result = str_replace ('%secs', $time, $result);
    echo $result;
    ?>
    </div>
<?php  }?>  


<?php if (isset($qry_results)) {
?>

<div id="results">

<!-- results listing -->

    <?php foreach ($qry_results as $_key => $_row){
        $last_domain = $domain_name;
        extract($_row);
        if ($show_query_scores == 0) {
            $weight = '';
        } else {
            $weight = "[$weight%]"; 
        }
        ?>
        <?php  if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?>
        <div class="idented">
        <?php }?>
        <b><?php print $num?>.</b> <?php print $weight?>
        <a href="<?php print $url?>" class="title"> <?php print ($title?$title:$sph_messages['Untitled'])?></a><br/>
        <div class="description"><?php print $fulltxt?></div>
        <div class="url"><?php print $url2?> - <?php print $page_size?></div>
        <?php  if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?>
            [ <a href="<?php print 'index.php?query='.quote_replace(addmarks($query)).'&search=1&results='.$results_per_page.'&domain='.$domain_name?>">More results from <?php print $domain_name?></a> ]
            </div class="idented">
        <?php }?>
        <br/>
    <?php  }?>
 </div>
<?php }?>

<!-- links to other result pages-->
<?php if (isset($other_pages)) {
    if ($adv==1) {
        $adv_qry = "&adv=1";
    }
    if ($type != "") {
        $type_qry = "&type=$type";
    }
?>
    <div id="other_pages">
    <?php print $sph_messages["Result page"]?>:
    <?php if ($start >1){?>
                <a href="<?php print 'index.php?query='.quote_replace(addmarks($query)).'&start='.$prev.'&search=1&results='.$results_per_page.$type_qry.$adv_qry.'&domain='.$domain?>"><?php print $sph_messages['Previous']?></a>
    <?php  }?>  

    <?php  foreach ($other_pages as $page_num) {
                if ($page_num !=$start){?>
                    <a href="<?php print 'index.php?query='.quote_replace(addmarks($query)).'&start='.$page_num.'&search=1&results='.$results_per_page.$type_qry.$adv_qry.'&domain='.$domain?>"><?php print $page_num?></a>
                <?php } else {?>    
                    <b><?php print $page_num?></b>
                <?php  }?>  
    <?php  }?>

    <?php if ($next <= $pages){?>   
            <a href="<?php print 'index.php?query='.quote_replace(addmarks($query)).'&start='.$next.'&search=1&results='.$results_per_page.$type_qry.$adv_qry.'&domain='.$domain?>"><?php print $sph_messages['Next']?></a>
    <?php  }?>  

    </div>

<?php }?>


<div class="divline">
</div>

I'm also not aware of a live PHP code editor, if you know of one please comment and share so I can add a link!


Solution

  • Presuming $from and $to are the result numbers, so you're displaying "Showing results 10 to 30 of 100" for example:

    <div id="results">
    
    <!-- results listing -->
    
    <?php $adbreak = ($to - $from) / 2;
        <?php foreach ($qry_results as $_key => $_row){
        <?php if ($adbreak == 0) { ?>
            <div id="results-adbreak">
                <img src="buy-a-car.jpg" alt="one careful owner!" />
            </div>
        <?php }
              $adbreak--;
         ?>
    
    // rest of your code
    

    This will put a div approximately (give or take one) half way down your page of results. You can obviously replace the ad with a call to whatever you want.

    adding something like:

    <?php $adbreak = ($to - $from) / 2;
    <?php if ($adbreak < 5) $adbreak = -1; ?>
    

    will ensure that it doesn't display at all if the results list is too short.

    If you don't know $to and $from in advance, you can still do it, but you'll have to calculate the equivalent from the query result first.