MB Publishing Webmaster Resource ::
View previous topic :: View next topic
Author
Message
Colocation Site Admin Joined: Jan 07, 2004 Posts: 412
Posted: Tue Apr 13, 2004 10:28 pm Post subject: Random Links Block
How can this code be made to display random links as opposed to the top 10?
Code:
<?php
if (eregi("block-Top10_Links_test.php",$_SERVER['PHP_SELF'])) {
Header("Location: index.php");
die();
}
global $prefix, $db;
$a = 1;
$sql = "SELECT lid, title FROM ".$prefix."_links_links ORDER BY hits DESC LIMIT 0,10";
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result)) {
$transfertitle = str_replace (" ", "_", $row[title]);
$title2 = ereg_replace("_", " ", $row[title]);
$content .= "<strong><big>·;</big></strong> ;;$a: <a href=\"viewlinkdetails-$row[lid]-$transfertitle.html\">$title2</a><br>";
$a++;
}
?>
Back to top
Humpa Site Admin Joined: Feb 05, 2004 Posts: 23
Posted: Thu Apr 15, 2004 10:39 am Post subject:
Code:
<?php
if (eregi("block-Top10_Links_test.php",$_SERVER['PHP_SELF'])) {
Header("Location: index.php");
die();
}
global $prefix, $db;
$rand_num_array = array();
$total_links = $db->sql_num_rows(sql_query("select * from ".$prefix."_links_links"));
$a = 1;
while($a <= 10) {
mt_srand((double)microtime()*1000000);
$rand_num = mt_rand(0,($total_links-1));
if(!in_array($rand_num,$rand_num_array)) {
$rand_num_array[] = $rand_num;
$sql = "SELECT lid, title FROM ".$prefix."_links_links DESC LIMIT $rand_num,1";
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$transfertitle = str_replace (" ", "_", $row[title]);
$title2 = ereg_replace("_", " ", $row[title]);
$content .= "<strong><big>·;</big></strong> ;;$a: <a href=\"viewlinkdetails-$row[lid]-$transfertitle.html\">$title2</a><br>";
$a++;
}
}
?>
I think that will work.
Give it a try and let me know.
Back to top
Colocation Site Admin Joined: Jan 07, 2004 Posts: 412
Posted: Thu Apr 15, 2004 5:19 pm Post subject: Fatal Error
Thanks.
Getting this:
Code:
Fatal error: Call to undefined function: sql_num_rows() in /usr/www/virtual/filtered/www.blocktest.com/blocks/block-Top10_Links_test.php on line 10
Line 10 is this:
Code:
$total_links = $db->sql_num_rows(sql_query("select * from ".$prefix."_links_links"));
Back to top
Humpa Site Admin Joined: Feb 05, 2004 Posts: 23
Posted: Thu Apr 15, 2004 7:00 pm Post subject:
Sorry .. it should be sql_numrows
Back to top
Colocation Site Admin Joined: Jan 07, 2004 Posts: 412
Posted: Thu Apr 15, 2004 10:19 pm Post subject: Further Steps
I tried this updated code as you suggested:
Code:
<?php
if (eregi("block-Top10_Links_test.php",$_SERVER['PHP_SELF'])) {
Header("Location: index.php");
die();
}
global $prefix, $db;
$rand_num_array = array();
$total_links = $db->sql_numrows(sql_query("select * from ".$prefix."_links_links"));
$a = 1;
while($a <= 10) {
mt_srand((double)microtime()*1000000);
$rand_num = mt_rand(0,($total_links-1));
if(!in_array($rand_num,$rand_num_array)) {
$rand_num_array[] = $rand_num;
$sql = "SELECT lid, title FROM ".$prefix."_links_links DESC LIMIT $rand_num,1";
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$transfertitle = str_replace (" ", "_", $row[title]);
$title2 = ereg_replace("_", " ", $row[title]);
$content .= "<strong><big>·;</big></strong> ;;$a: <a href=\"viewlinkdetails-$row[lid]-$transfertitle.html\">$title2</a><br>";
$a++;
}
}
?>
Which now gives this:
Code:
Warning: Missing argument 2 for sql_query() in /usr/www/virtual/filtered/www.blocktest.com/includes/sql_layer.php on line 176
Fatal error: Maximum execution time of 30 seconds exceeded in /usr/www/virtual/filtered/www.blocktest.com/blocks/block-Top10_Links_test.php on line 16
Thanks!
Back to top
Humpa Site Admin Joined: Feb 05, 2004 Posts: 23
Posted: Fri Apr 16, 2004 4:32 am Post subject:
The problem is that I never use phpnuke's method of retrieving data using mysql, so I couldn't test it. This seems like a slow process (having you test it ).
So, I switched the code to the normal mysql functions, and I tested it.
Code:
<?php
if (eregi("block-Top10_Links_test.php",$_SERVER['PHP_SELF'])) {
Header("Location: index.php");
die();
}
$num_of_links = 10;
global $prefix;
$rand_num_array = array();
$total_links = mysql_num_rows(mysql_query("select * from ".$prefix."_links_links"));
if($total_links < $num_of_links) {
$num_of_links = $total_links;
}
$a = 1;
while($a <= $num_of_links) {
mt_srand((double)microtime()*1000000);
$rand_num = mt_rand(0,($total_links-1));
if(!in_array($rand_num,$rand_num_array)) {
$rand_num_array[] = $rand_num;
$result = mysql_query("SELECT lid, title FROM ".$prefix."_links_links ORDER BY hits DESC LIMIT $rand_num,1");
$row = mysql_fetch_row($result);
$transfertitle = str_replace (" ", "_", $row[1]);
$title2 = ereg_replace("_", " ", $row[1]);
$content .= "<strong><big>·</big></strong> $a: <a href=\"viewlinkdetails-$row[0]-$transfertitle.html\">$title2</a><br>";
$a++;
}
}
?>
Back to top
Colocation Site Admin Joined: Jan 07, 2004 Posts: 412
Posted: Fri Apr 16, 2004 7:55 am Post subject: For The Record
Now that is a beautiful block....
And the thing is, it presents ever changing content to a search engine bot.
Back to top
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum