View previous topic :: View next topic |
Author |
Message |
GamerZ
Joined: 15 May 2002 Posts: 537 Location: Singapore
|
Posted: Tue Dec 10, 2002 8:42 am Post subject: [Hacks] Related Stories |
|
|
I have created a related stories feature that will automatic such the headlines and display 5 related stories(default).
Copy the function and put it inside b2template.functions.php
If you want to exclude some search word, just add it in the $exclude array.
To use it just use [php:1:388f58132d]<?php relatedstories(); ?>[/php:1:388f58132d] outside b2 loop.
If you want 10 related stories just add in 10 which becomes[php:1:388f58132d]<?php relatedstories(10); ?>[/php:1:388f58132d]
Here is the function:
[php:1:388f58132d]
function relatedstories($howmanystories=5) {
global $tableposts, $siteurl,$blogfilename, $id, $querystring_start, $querystring_equal, $querystring_separator;
$titlesearch = get_the_title();
$exclude = array ("A", "Are", "Do", "It", "Its", "Is", "In", "I", "My", "The", "To", "With", "You");
$qry = "SELECT DISTINCT ID, DATE_FORMAT(post_date, '%e/%c/%y'), post_content, post_title FROM $tableposts WHERE (";
$titlesearch = preg_replace('/, +/', '', $titlesearch);
$titlesearch = trim($titlesearch);
$words = explode (" ", $titlesearch);
$words = array_diff($words,$exclude);
foreach ($words as $word) {
$qry .= "(post_content LIKE '%$word%' OR post_title LIKE '%$word%') OR ";
}
$qry = substr ($qry, 0, strlen ($qry) - 4);
$qry .= ") AND (ID != $id) ORDER BY post_date DESC LIMIT $howmanystories";
$sql = mysql_query($qry);
while ($relatedstories = mysql_fetch_row($sql)) {
echo "<b>»</b> <a href=\"".$siteurl."/".$blogfilename.$querystring_start."p".$querystring_equal.$relatedstories[0].$querystring_separator."c".$querystring_equal."1\">";
echo stripslashes($relatedstories[3]) ."</a>";
echo " (".$relatedstories[1].")";
echo "<br>";
}
}
[/php:1:388f58132d] _________________
++ GamerZ.Per.Sg - Complex Simplicity
Last edited by GamerZ on Sun Jan 05, 2003 4:40 pm; edited 1 time in total |
|
Back to top |
|
 |
turtelina
Joined: 25 Jan 2002 Posts: 35 Location: Austria
|
Posted: Tue Dec 10, 2002 10:29 pm Post subject: |
|
|
Hi Gamerz,
I installed it. It´s awesome!!! Thank you so much for sharing it!
|
|
Back to top |
|
 |
GamerZ
Joined: 15 May 2002 Posts: 537 Location: Singapore
|
Posted: Wed Dec 11, 2002 7:15 am Post subject: |
|
|
turtelina wrote: | Hi Gamerz,
I installed it. It´s awesome!!! Thank you so much for sharing it!
| NP glad you like it _________________
++ GamerZ.Per.Sg - Complex Simplicity |
|
Back to top |
|
 |
JEDOK
Joined: 19 Dec 2002 Posts: 4 Location: korea
|
Posted: Thu Dec 19, 2002 4:59 am Post subject: |
|
|
it's a nice idea and works fine. but, there is just one bug.
if the title is consisted of the excluded words(words in $exclude array) only, the error occur
example: when i put "I", "love", "you" in $exclude array and write a post titled "I love you", then,
Quote: | Warning: Supplied argument is not a valid MySQL result resource in /home/u2ii/public_html/b2test/b2-include/b2template.functions.php on line 1217 |
i hope someone can fix this. and will it be possible to make it search through only the titles(not the whole "content" of the posts)? |
|
Back to top |
|
 |
GamerZ
Joined: 15 May 2002 Posts: 537 Location: Singapore
|
Posted: Thu Dec 19, 2002 3:21 pm Post subject: |
|
|
JEDOK wrote: | it's a nice idea and works fine. but, there is just one bug.
if the title is consisted of the excluded words(words in $exclude array) only, the error occur
example: when i put "I", "love", "you" in $exclude array and write a post titled "I love you", then,
Quote: | Warning: Supplied argument is not a valid MySQL result resource in /home/u2ii/public_html/b2test/b2-include/b2template.functions.php on line 1217 |
i hope someone can fix this. and will it be possible to make it search through only the titles(not the whole "content" of the posts)? | good bug there, but normally I dun think anyone will use all excluded word in the title?
for the title only replace with this[php:1:fb41819051]$qry .= "(post_title LIKE '%$word%') OR ";[/php:1:fb41819051] _________________
++ GamerZ.Per.Sg - Complex Simplicity |
|
Back to top |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 1075 Location: Oregon
|
Posted: Sat Jan 04, 2003 6:54 pm Post subject: |
|
|
I don't understand the concept of this hack. Related stories of what? _________________ Michael P. |
|
Back to top |
|
 |
BDKR
Joined: 04 Jan 2003 Posts: 20 Location: Venezuela
|
Posted: Sat Jan 04, 2003 11:00 pm Post subject: |
|
|
Cool hack! |
|
Back to top |
|
 |
dtdgoomba
Joined: 05 Aug 2002 Posts: 179 Location: Cambridge, MA
|
Posted: Sun Jan 05, 2003 2:11 am Post subject: |
|
|
i think the hack finds stories within your site that are 'related' to your current post by finding similar phrases/words in other posts? Is that it? |
|
Back to top |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 1075 Location: Oregon
|
Posted: Sun Jan 05, 2003 2:19 am Post subject: |
|
|
Oh, I guess it uses the latest post. Yeah? What a great idea . . .  _________________ Michael P. |
|
Back to top |
|
 |
GamerZ
Joined: 15 May 2002 Posts: 537 Location: Singapore
|
Posted: Sun Jan 05, 2003 7:49 am Post subject: |
|
|
it just seacrh thru the DB for content or title tt matches the current blog subject and displays it in order of date.
You can exclude some words tt dun want to be searched _________________
++ GamerZ.Per.Sg - Complex Simplicity |
|
Back to top |
|
 |
annia
Joined: 09 Apr 2002 Posts: 18
|
Posted: Sun Jan 05, 2003 8:58 pm Post subject: |
|
|
i love that hack :) it works good for me |
|
Back to top |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 1075 Location: Oregon
|
Posted: Tue Jan 07, 2003 11:52 pm Post subject: |
|
|
I get . . .
Quote: |
mysql_fetch_row(): supplied argument is not a valid MySQL result resource
|
Can you fix this? _________________ Michael P. |
|
Back to top |
|
 |
GamerZ
Joined: 15 May 2002 Posts: 537 Location: Singapore
|
Posted: Wed Jan 08, 2003 5:11 am Post subject: |
|
|
Cyberian75 wrote: | I get . . .
Quote: |
mysql_fetch_row(): supplied argument is not a valid MySQL result resource
|
Can you fix this? | at line? _________________
++ GamerZ.Per.Sg - Complex Simplicity |
|
Back to top |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 1075 Location: Oregon
|
Posted: Wed Jan 08, 2003 5:15 am Post subject: |
|
|
At line 17 of your function... _________________ Michael P. |
|
Back to top |
|
 |
GamerZ
Joined: 15 May 2002 Posts: 537 Location: Singapore
|
Posted: Wed Jan 08, 2003 11:21 am Post subject: |
|
|
Cyberian75 wrote: | At line 17 of your function... | wierd, but it seems okie to me? _________________
++ GamerZ.Per.Sg - Complex Simplicity |
|
Back to top |
|
 |
|