Dynamic Related News

Discuss news styles, css code, and other Coranto customizations in here...the possibilities are endless, so have at it!

Moderator: Spunkmeyer

Dynamic Related News

Postby bozoka45 » Fri Dec 17, 2004 11:02 am

I just wanted to share this simple PHP script I wrote to display dynamic related news on my site. Its very SQL intensive and requires news be stored in an SQL database to work.

I'll be happy to provide support for it to a certain extent, but this is more so just a personal mod I'm letting anyone use.

Code: Select all
<?php

//This is an advanced news searching program that helps me in generating related news. The script is VERY SQL involved, so you will need your Coranto news stored in an SQL database, either by the CorantoSQL or FakeSQL addons.
//Look towards the end of this scipt for formatting options.

/*Call this into your coranto-generated php page with the following parameters: It's bet to use this in Coranto styles.

Note that you don't want your searchfield to be too long, as it searches for each word invidually, at least in its original state. So, if you enter one or two keywords, it should work great.

<?php
$search = "<Field: SEARCHFIELD>";
$search = urlencode($search);
include ( 'http://' . $_SERVER['HTTP_HOST'] . '/related.php?s=' . $search);
<close php tag>

*/


//DB INFO

$dbuser = ''; //DB username
$dbpass = ''; //DB pass
$dbhost = 'localhost'; //DB Host
$newstable = ''; //Table where news is stored

//List the fields that you need searched, following the below format.

$searchfield[] = 'Text';
$searchfield[] = 'Subject';

function dbConnect(
$database="" //The database where you store Coranto news goes here.
) {
   
   global $dbhost, $dbuser, $dbpass;

   $db = mysql_connect($dbhost, $dbuser, $dbpass);

   $selected = mysql_select_db($database);
   if (!(isset($selected))) {
            die ("Error!");
      }

}

//Here's where all the fun starts.

dbConnect();

//Get the search string, we're going to search for all the words in the fields we've specified.

$search = urldecode($_REQUEST['s']);

$search = explode(" ", $search);

$SearchSQL = '';

foreach ($searchfield as $field) {
   foreach ($search as $s) {
      $SearchSQL[] = "SELECT * FROM `" . $newstable . "` WHERE `" . $field . "` LIKE '%" . $s . "%'";
   }
}

$SQL = implode(" UNION ", $SearchSQL);

$SQL .= " ORDER BY `newstime` DESC";

$query = mysql_query($SQL) or die(mysql_error());

//Count the results with rcount
$rcount = 0;


//Formating section starts here!
//I've left the formatting I have in order to help you.

//Image this part as what goes about the <Field: Content> in a template

$relatedHTML = "<table cellpadding=\"0\" cellspacing=\"2\" border=\"0\"  class=\"bodyline\" width=\"90%\">\n";
$relatedHTML .= "<tr><td><b>" . urldecode($_REQUEST['s']) . " News From OTC:</b></td></tr>\n";
$relatedHTML .= "<tr><td>&nbsp;</td></tr>";

//Now, this is where the style goes.

while($results=mysql_fetch_array($query)) {
   if($results['xMultiSubmitSP'] == '69421072513396') { //If the XMS Profile is draft news.
      if($results['CustomField_outside'] == "Yes") { //If it links to the outside, surl will be filled, so just send the user there
         $relatedHTML .= "<tr><td><a href=\"" . $results['CustomField_surl'] . "\" target=\"new\">" . $results['Subject'] . "</a> <i>(from " . $results['CustomField_source'] . ")</i></td></tr>\n";
         $rcount++;
      } else { //If its not, its either a maginot page or its a link to a page.
         if( $results['CustomField_surl'] != '' ) {
            $relatedHTML .= "<tr><td><a href=\"" . $results['CustomField_surl'] . "\">" . $results['Subject'] . "</a></td></tr>\n";
            $rcount++;
         } else {
            $relatedHTML .=  "<tr><td><a href=\"/2005/content/article/" . $results['newsid'] . ".php\">" . $results['Subject'] . "</a></td></tr>\n";
            $rcount++;
         }
      }
   }
}

//And finally, this is below the <Field: Content> in a template

$relatedHTML .= "</table>\n";

//This will only dispaly your result box if at least one result is returned.

if($rcount > 0) {
   print $relatedHTML;
}

?>
User avatar
bozoka45
 
Posts: 1159
Joined: Sun Mar 23, 2003 8:38 pm
Location: New Orleans

Postby jayo » Sun Dec 26, 2004 11:35 pm

Hey..

This script looks great, but I am fairly new and dont understand how to implement this.. Could you help me out?
No need for a SIG!
jayo
 
Posts: 104
Joined: Tue Mar 18, 2003 9:38 pm
Location: Los Angeles

Postby bozoka45 » Mon Dec 27, 2004 7:43 am

You need to use PHP and SQL, do you use that?
User avatar
bozoka45
 
Posts: 1159
Joined: Sun Mar 23, 2003 8:38 pm
Location: New Orleans

Postby jayo » Mon Dec 27, 2004 7:24 pm

bozoka45 wrote:You need to use PHP and SQL, do you use that?

Yes I am.. Im using Coranto v1.23 and Ive just install CorantoSQL.. im not sure exactly if Im using MYsql to generate my stories or if its just storing my info in MYSQL tables,.
No need for a SIG!
jayo
 
Posts: 104
Joined: Tue Mar 18, 2003 9:38 pm
Location: Los Angeles

Postby bozoka45 » Mon Dec 27, 2004 10:08 pm

jayo wrote:Yes I am.. Im using Coranto v1.23 and Ive just install CorantoSQL.. im not sure exactly if Im using MYsql to generate my stories or if its just storing my info in MYSQL tables,.


Well, first of, all, I would recommend using Coranto 1.24, it fixes a few of the bugs present in Coranto 1.23. remember to take a backup first, just in case :)

Secondly, it's known that CorantoSQL is not comptaible with versions of Coranto higher than 1.02. So either you can downgrade to that, or you can disable CSQL and use my addon FakeSQL.
User avatar
bozoka45
 
Posts: 1159
Joined: Sun Mar 23, 2003 8:38 pm
Location: New Orleans

Postby jayo » Tue Dec 28, 2004 4:41 pm

bozoka45 wrote:
jayo wrote:Yes I am.. Im using Coranto v1.23 and Ive just install CorantoSQL.. im not sure exactly if Im using MYsql to generate my stories or if its just storing my info in MYSQL tables,.


Well, first of, all, I would recommend using Coranto 1.24, it fixes a few of the bugs present in Coranto 1.23. remember to take a backup first, just in case :)

Secondly, it's known that CorantoSQL is not comptaible with versions of Coranto higher than 1.02. So either you can downgrade to that, or you can disable CSQL and use my addon FakeSQL.


Ok, I implemented the FakeSQL version 3.3 and upgraded to the new version of coranto.. whats next?
No need for a SIG!
jayo
 
Posts: 104
Joined: Tue Mar 18, 2003 9:38 pm
Location: Los Angeles

Postby bozoka45 » Tue Dec 28, 2004 8:36 pm

Well first, you need to know a little bit of PHP. Secondly, your server must support PHP pages and you must generate PHP pages with Coranto.

The script is heavily commented (copy and paste the script into a text editor the colors everything for you and the comments will be green most of the time).

You need to put this little bit of code into the page where you want the related news to appear:

Code: Select all
<?php
$search = "<Field: SEARCHFIELD>";
$search = urlencode($search);
include ( 'http://' . $_SERVER['HTTP_HOST'] . '/related.php?s=' . $search);
?>


Then fill in DB variables, the field variables, and the database variable in the dbConnect function (right below the field variables section).

Then look for "//Formating section starts here!" Below that line is where you'll want to put how the output is going to look. I've already included the style that I use, and commented it some, so it shouldn't be too hard to figure out.
User avatar
bozoka45
 
Posts: 1159
Joined: Sun Mar 23, 2003 8:38 pm
Location: New Orleans

Postby jayo » Tue Dec 28, 2004 8:55 pm

bozoka45 wrote:Well first, you need to know a little bit of PHP. Secondly, your server must support PHP pages and you must generate PHP pages with Coranto.

The script is heavily commented (copy and paste the script into a text editor the colors everything for you and the comments will be green most of the time).

You need to put this little bit of code into the page where you want the related news to appear:

Code: Select all
<?php
$search = "<Field: SEARCHFIELD>";
$search = urlencode($search);
include ( 'http://' . $_SERVER['HTTP_HOST'] . '/related.php?s=' . $search);
?>


Then fill in DB variables, the field variables, and the database variable in the dbConnect function (right below the field variables section).

Then look for "//Formating section starts here!" Below that line is where you'll want to put how the output is going to look. I've already included the style that I use, and commented it some, so it shouldn't be too hard to figure out.


Ok cool, thanks for that help.. I understand alot better. The thing I dont get is how do you decide waht keys words to use for each specific posts. Is there somewhere that you specificy the keyswords on Coranto?
No need for a SIG!
jayo
 
Posts: 104
Joined: Tue Mar 18, 2003 9:38 pm
Location: Los Angeles

Postby bozoka45 » Tue Dec 28, 2004 8:56 pm

You specify the keywords with this:

Code: Select all
<?php
$search = "<Field: SEARCHFIELD>";
$search = urlencode($search);
include ( 'http://' . $_SERVER['HTTP_HOST'] . '/related.php?s=' . $search);
?>


Where Field: SearchField is the keyword. You can either create a customfield and specify the keywords, or you could search by subject or something.
User avatar
bozoka45
 
Posts: 1159
Joined: Sun Mar 23, 2003 8:38 pm
Location: New Orleans

Postby dweiss » Fri Jan 07, 2005 9:21 pm

bozoka45 wrote:Where Field: SearchField is the keyword. You can either create a customfield and specify the keywords, or you could search by subject or something.


How can you manage "and" "or" "not" in the searhfield?

Thanks,
Regards,
Saludos,
Daniel,
dweiss
 
Posts: 82
Joined: Mon Feb 02, 2004 2:21 pm
Location: Buenos Aires - Argentina

Postby bozoka45 » Sat Jan 08, 2005 1:39 am

dweiss wrote:How can you manage "and" "or" "not" in the searhfield?

Thanks,


You can't. You'd have to change around the SQL for that.
User avatar
bozoka45
 
Posts: 1159
Joined: Sun Mar 23, 2003 8:38 pm
Location: New Orleans

Postby tkaplan06 » Wed Mar 09, 2005 2:37 am

Hi,
I am desperately trying to get this to work on my install of Coranto despite my extremely minimal knowledge of PHP. I have used straightforwarded installations of Coranto many times but I am just lost with the PHP and despite my dozens of searches, I just can't seem to get this to work.

I have FakeSQL installed and that's working fine, and I changed all the paremeters in related.php. But what I want to have happen is for visitors to be able to click the headline and then go to the article -- which I can do -- but then I want the related links to appear at the bottom of the article. I am lost at making this happen.

I would greatly appreciate it if someone could help me, either by posting here, PMing me, or, preferrably, IMing me (ArcticWebDesign on AIM).

Thanks so much.
Tom
tkaplan06
 
Posts: 9
Joined: Wed Mar 09, 2005 2:28 am

Postby bozoka45 » Wed Mar 09, 2005 7:40 am

You need to use this code in order to show related news. The page needs to end in php (or php3) on some servers). The only change you have to make is on the first line (or change the path to related.php).

Code: Select all
<?php
$search = "<Field: SEARCHFIELD-CHANGE ME>";
$search = urlencode($search);
include ( 'http://' . $_SERVER['HTTP_HOST'] . '/related.php?s=' . $search);
?>
User avatar
bozoka45
 
Posts: 1159
Joined: Sun Mar 23, 2003 8:38 pm
Location: New Orleans

Postby kriko » Wed Mar 09, 2005 9:04 am

I just noticed this and here are few suggestions to enhance/fix the code:

a) Why do you use a function (dbConnect()) to connect to the mySQL database when you only call it once?

b) First you declare $SearchSQL as a string (='';) then later on you add values to it like it was an array... PHP is relatively loose about these things, but it will issue a notice in a development platform. I'd reccommend writing code that follows all the requirements and standards. Therefore you should declare $SearchSQL as an array:
$SearchSQL = array();

c) Try using this line for $search:
Code: Select all
$search = explode(" ",stripslashes(urldecode($_REQUEST['s'])));


d) And the following for appending new items to the SearchSQL array:
Code: Select all
$SearchSQL[] = "SELECT * FROM `" . addslashes($newstable) . "` WHERE `" . addslashes($field) . "` LIKE '%" . addslashes($s) . "%'";


f) I didn't really get the point of this line:
Code: Select all
($results['xMultiSubmitSP'] == '69421072513396')

Wouldn't it only work on your installation?!


I hope the suggestions I gave will make your script a bit nicer and secure. :)
Kristjan aka. kriko
now a wireless expert
User avatar
kriko
 
Posts: 3328
Joined: Sat Feb 24, 2007 8:13 pm

Postby tkaplan06 » Tue Mar 15, 2005 2:47 am

Thanks for all your help - I finally got it working. However, it always prints the current article as one of the related articles -- is there any way that I can have it not print the current article that is being viewed on that list?

I have "Related Links" as a sidebar on each of my static news article pages. For example:
http://www.arcticwebdesign.com/~choate/ ... riKSKK.php

The other problem I am having is the script is printing the keywords that are specified in the custom field I have created right above the related links. How can I stop it from doing this?

Any help would be appreciated.
tkaplan06
 
Posts: 9
Joined: Wed Mar 09, 2005 2:28 am

Next

Return to Customization

Who is online

Users browsing this forum: No registered users and 1 guest

cron