You are here: Stuck on a PHP random link script « All PHP « IT news, forums, messages
Stuck on a PHP random link script

Posted by SEO Dave on 06/09/05 15:53

Plan is to use a MySQL database to randomly select a link from to add
to the menu of a page.

Database is setup, have got it to work within a *.php page, but when I
try to convert to a tpl template it fails and not sure why.

The script code is-

define ('HOSTNAME', 'localhost');
define ('USERNAME', '****');
define ('PASSWORD', ****');
define ('DATABASE_NAME', '****');

$db = mysql_connect(HOSTNAME, USERNAME, PASSWORD) or die ('I cannot
connect to MySQL.');

mysql_select_db(DATABASE_NAME);

$query = "SELECT Site,URL FROM links ORDER by rand() LIMIT 1";

$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
$linked1 = "<a href=\"" , ($row['URL']) , "\"><img class=\"menu\"
src=\"dot.jpg\" alt=\"" , nl2br($row['Site']) , "\">" ,
nl2br($row['Site']) , "</a>";
}

mysql_free_result($result);
mysql_close();

Then include %%linked1%% within the menu of the tpl file associated
with the php file above. When I try this the link doesn't show (the
footer.php file isn't used at all with the above included). Have tried
it in the header.php file and same result.

The code I've got working is as above, but this-

$linked1 = "<a href=\"" , ($row['URL']) , "\"><img class=\"menu\"
src=\"dot.jpg\" alt=\"" , nl2br($row['Site']) , "\">" ,
nl2br($row['Site']) , "</a>";

Is replaced with this-

echo "<a href=\"" , ($row['URL']) , "\"><img class=\"menu\"
src=\"dot.jpg\" alt=\"" , nl2br($row['Site']) , "\">" ,
nl2br($row['Site']) , "</a>";

Which means all I've changed is echo to $linked1 =

This (the echo one) results in the random link showing at the bottom
of the footer php file (as expected since the code is at the bottom of
the file). So the concept works.

Since I'm using a tpl based template it needs to be converted from
using echo to the above type format and it seems I've made an error in
the conversion.

What I'm after is this format-

<a href="URL"><img class="menu" src="dot.jpg" alt="Site">Site</a>

Where URL is the url from the database and Site is the links anchor
text from the database.

BTW I have this working with the script below, but it uses a txt file
not a database for the links-

$lim1 = "\n";
$quote1 = "links1.txt";
$fp1 = fopen($quote1, "r");
$content1 = fread($fp1, filesize($quote1));
$quotearr1 = explode($lim1,$content1);
fclose($fp1);
srand((double)microtime()*1000000);
$index1 = (rand(1, sizeof($quotearr1)) - 1);
$rquote1 = $quotearr1[$index1];

Then add %%rquote1%% to the menu via the tpl template. This works, but
uses text files for the link source and I want to use the same set of
links over multiple websites on the same server. Currently have to
have the same txt files on every site, which is time consuming to
update with new links.

Thanks

David
--
Free Search Engine Optimization Tutorial
http://www.seo-gold.com/tutorial/

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация