Jump to content


Random facts


5 replies to this topic

#1 Maxx-Designs

    P2L Jedi

  • Members
  • PipPipPip
  • 681 posts
  • Location:Australia

Posted 26 September 2005 - 03:06 AM

Hey, Im mkaing a site, and I have a box, I just wanht like random facts to come in on everypage, ive seen this done. But im not sure if it was php.

Does anyboey know the code\

Thanks!

Cheers
maxx-designs

#2 illusion

    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 26 September 2005 - 04:09 AM

depending on what your are using (database wise) this is using MySQL
$fact = mysql_fetch_array(mysql_query("SELECT * FROM `facts` ORDER BY RAND()"));

echo $fact[0];


#3 Maxx-Designs

    P2L Jedi

  • Members
  • PipPipPip
  • 681 posts
  • Location:Australia

Posted 26 September 2005 - 04:54 AM

im very n00b at php, i can do it databsae but can you put some example facts in there for me plz and teach mo how i cwould incopratre it

Thanks

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 26 September 2005 - 09:11 PM

Basically, you need to set up a mysql database. Create a table (according to illusions example, the table would be named facts). And then add the facts to the database.
$fact = mysql_fetch_array(mysql_query("SELECT * FROM `facts` ORDER BY RAND() LIMIT 1", MYSQL_NUM));
echo $fact[0];
Would actually be slightly better, because it would use less memory, and make things faster.

To figure out how to use mysql, check out the tutorials in the database (http://www.pixel2life.com/tutorials/PHP_Co...1&d=1&ss=random or http://www.pixel2life.com/tutorials/PHP_Co...QL_Integration/) and php.net

#5 Maxx-Designs

    P2L Jedi

  • Members
  • PipPipPip
  • 681 posts
  • Location:Australia

Posted 27 September 2005 - 01:02 AM

Thanks very much!

#6 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 27 September 2005 - 01:38 PM

<?php
$facts = array (
	'This is fact 1',
	'This is fact 2',
	'This is fact 3',
);
echo $facts[rand(0, count($facts)-1)];
?>
You can add more facts, just keep the structure that way

Edited by HaloprO, 27 September 2005 - 01:40 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users