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'];?>: <?php print $search_results['did_you_mean_b']; ?>?
</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?>
<?php print ($title?$title:$sph_messages['Untitled'])?><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 == "") {?>
[ More results from <?php print $domain_name?> ]
</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){?>
<?php print $sph_messages['Previous']?>
<?php }?>
<?php foreach ($other_pages as $page_num) {
if ($page_num !=$start){?>
<?php print $page_num?>
<?php } else {?>
<b><?php print $page_num?></b>
<?php }?>
<?php }?>
<?php if ($next <= $pages){?>
<?php print $sph_messages['Next']?>
<?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!
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.
Related
I think this is a simple problem. But, I don't know how to solve this problem. I have posted my codes below. Here, In this below code if else statement always shows No Sarees. I got the result from mysql table but it always shows with No Sarees text. I have checked this using var_dump($categories) it returns array(0){} . How can I solve this problem?
<?php if ($categories) { ?>
<!--BOF Refine Search Result-->
<div class="refine-search-result">
<?php if (count($categories) <= 5) { ?>
<?php foreach ($categories as $category) { ?>
<div class="refine-block">
<p><?php echo $category['name']; ?></p>
</div>
<?php } ?>
<?php } else { ?>
<?php for ($i = 0; $i < count($categories);) { ?>
<?php $j = $i + ceil(count($categories) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($categories[$i])) { ?>
<div class="refine-block">
<p><?php echo $categories[$i]['name']; ?></p>
</div>
<?php } ?>
<?php } ?>
<?php } ?>
<?php } ?>
<div class="clear"></div>
</div><!--EOF Refine Search Result-->
<?php } else {
?>
<div class="refine-search-result">
<div class="refine-block">
<p>No Sarees</p>
</div>
<div class="clear"></div>
</div>
<?php } ?>
If your $categories is empty array then in if($categories) your array is converted to boolean value. If it's empty array (and you say it is) then it's get converted to false.
Check this:
$categories = array(); //empty array
var_dump((boolean)$categories); //this will show what variable will look like after converting to boolean value
if ($categories){
echo '$categories IS NOT empty';
}else{
echo '$categories IS empty';
}
How could I find the last post and NOT put <hr> below it?
<?php foreach($db->query("SELECT * FROM news") as $row): ?>
<div class="NewsHeadline">
<div class="NewsDate"><?php echo $date; ?> - </div>
<div class="NewsTitle"><?php echo $title; ?></div>
</div>
<div class="NewsBody">
<?php echo $body; ?>
</div>
<hr>
<?php endforeach ?>
Set a counter equal to 1 which increments each round, and check if it is equal to the count() of the $query array returned. If it is, you are on the last round!
<?php
$counter = 1;
$total = count($db->query("SELECT * FROM news"));
?>
<?php foreach($db->query("SELECT * FROM news") as $row): ?>
<div class="NewsHeadline">
<div class="NewsDate"><?php echo $date; ?> - </div>
<div class="NewsTitle"><?php echo $title; ?></div>
</div>
<div class="NewsBody">
<?php echo $body; ?>
</div>
<?php if($counter != $total){
?>
<hr>
<?php
}
?>
<?php $counter++; ?>
<?php endforeach ?>
A solution would be to build an array and then use join :
<?php
$arr=array();
foreach($db->query("SELECT * FROM news") as $row):
$arr[] = '<div class="NewsHeadline"><div class="NewsDate">'.$date
.' - </div><div class="NewsTitle">'.$title'
.</div></div><div class="NewsBody">'.$body.'</div>';
endforeach
echo join("<hr>",$arr);
?>
You can convert to regular for loop and then compare $i to the size of your query result:
<?php
$news = $db->query("SELECT * FROM news");
for($i=0; $i<sizeof($news);$i++){ ?>
<div class="NewsHeadline">
<div class="NewsDate"><?php echo $date; ?> - </div>
<div class="NewsTitle"><?php echo $title; ?></div>
</div>
<div class="NewsBody">
<?php echo $body; ?>
</div>
<?php if(($i+1) < sizeof($news)) echo '<hr>'; ?>
<?php } ?>
If you get a count of the rows prior to the loop, you can check the count and avoid adding the HR.
$rows = $db->query("SELECT * FROM news");
$row_count = $rows.count();
$iter = 0;
foreach($rows as $row)
{
//...
if ($iter < $row_count)
{
// render hr
}
$iter++;
}
You could also go with a pure CSS approach:
div.containerClass:last-child hr { display:none; }
where "containerClass" would be whatever element your posted code sits inside of.
I would add each two result one div class
<?php while ($fetch = $db->fetch($query)) { ?>
<?php echo $fetch['title']; ?>
<?php } ?>
Output should be like this
<div class="one">
<div class="two">
<article>Title</article>
<article>Title</article>
</div>
</div>
<div class="one">
<div class="two">
<article>Title</article>
<article>Title</article>
</div>
</div>>
<?php
$i=0;
while ($fetch = $db->fetch($query)) { ?>
<?php if ($i%2==0) { ?>
<div class="one">
<div class="two">
<?php } ?>
<article><?php echo $fetch['title']; ?></article>
<?php if ($i++%2==1) { ?>
</div>
</div>
<?php } ?>
<?php } ?>
//Also is a good idea to verify if the <div> tags are closed
<?php if ($i%2==1) { ?>
</div>
</div>
<?php } ?>
$count = 0;
while ($fetch = $db->fetch($query))
{
if ($count == 0)
echo '<div class="one"><div class="two">';
if ($count < 2)
echo '<article>'.$fetch['title'].'</article>';
if ($count == 2)
{
echo '</div></div>';
$count = 0;
}
$count++;
}
I have few drop down boxes from where I can get an id of a category. For example, from drop down box 1, i get $cat1, from box 2 i get $cat2 and so on.
Then I want to get the entries from db for each of the cat id. Currently I am repeating same code for each of the variable, like:
<?
$cat1 = 1;
$cat2 = 3;
$cat3 = 4;
$cat4 = 8;
<? if ($var1 != ""){ ?>
<div>
Entries for <? echo $var1; ?>
..
</div>
<? } ?>
<? if ($var2 != ""){ ?>
<div>
Entries for <? echo $var2; ?>
..
</div>
<? } ?>
<? if ($var3 != ""){ ?>
<div>
Entries for <? echo $var3; ?>
..
</div>
<? } ?>
I'd like to know if I can use a loop and avoid writing code for each variable.
Try
<?
$cats = array(1,3,4,8);
foreach($cats as $value) {
if($value != "") {
?>
<div>Entries for <?= $value; ?></div>
<?
}
}
?>
Use an array like this:
$cat[1] = 'bla';
$cat[2] = 'Bla2';
foreach ($cat as $c){
if ($c != ""){
echo '
<div>
Entries for '. $c.'
..
</div>';
}
}
I want 3 item per div (the sample has a total of 7 items) like this:
<div>
<item/>
<item/>
<item/>
</div>
<div>
<item/>
<item/>
</div>
But I can't do
while($r = mysql_fetch_array($q){
?><item/><?
}
if(++$i%3==1){
//blabla:)
}
Because it incorrectly prints out
<div>
<item/>
<item/>
<item/>
</div>
<div>
<item/>
...
How do I correctly print out the items in blocks of 3?
You were most of the way there.
$result = mysql_query("SELECT ....");
$recordCounter = 0;
$record = mysql_fetch_assoc($result);
while ($record) {
if ($recordCounter % 3 == 0) {
?>
<div class="item-block">
<?php
}
?>
<div class="item"></div>
<?php
$record = mysql_fetch_assoc($result);
$recordCounter++;
if ($record === false || $recordCounter % 3 == 0) {
?>
</div>
<?php
}
}
mysql_free_result($result);
somewhat shortened :))
<div class="items">
<? $q = mysql_query("SELECT * FROM table ");
$i=1;
while($r = mysql_fetch_array($q))
{
?><item /><?
if($i%4==0){?></div><div class="item"><? }?>
<? $i++;
}?>
</div>
Here is another option, maybe there are too much php tags =)
<?php
$items = array(1,2,3,4,5,6,7,8);
$count = 1;
?>
<html>
<body>
<?php foreach ($items as $item) : ?>
<?php if ($count == 1) : ?>
<div>
<?php endif; ?>
<p><?php echo $item; ?></p>
<?php if ($count == 3) : ?>
</div>
<?php $count = 0; ?>
<?php endif; ?>
<?php $count++; ?>
<?php endforeach; ?>
</body>
</html>