0

我目前在我的一个网站上使用sphider,我的问题是如何将结果页面分成两部分以添加 200 像素的间隔来放置广告位。

代码:

    <?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>

我也不知道有一个实时 PHP 代码编辑器,如果你知道,请评论和分享,以便我添加链接!

4

1 回答 1

1

假设 $from 和 $to 是结果编号,因此您将显示“Showing results 10 to 30 of 100”,例如:

<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

这将在结果页面的一半左右放置一个 div(给或取一个)。您显然可以将广告替换为您想要的任何内容。

添加类似的东西:

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

如果结果列表太短,将确保它根本不显示。

如果你事先不知道 $to 和 $from,你仍然可以这样做,但你必须先从查询结果中计算出等值。

于 2012-09-15T08:31:06.327 回答