stupid question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amdayton
Forum Newbie
Posts: 3
Joined: Fri Jul 18, 2003 3:35 am

stupid question

Post by amdayton »

hey i am brand new to php and i have what could very easily be a stupid question.

i am using random quote generator to implement in my new website, and i would like to make it easy for the user to randomly come up with another quote (without having to refresh the page). is this even possible?
it would be nice if there was some way i could do it through a simple HTML link.

thanks

(i have included the code for the generator here - much of it is from a tutorial i found on evilwalrus.com)

Code: Select all

<?php

	function randomQuote() &#123;
	$dbcx = mysql_connect(localhost, username, password);
	if(!$dbcx) &#123;
		echo "there was an error connecting to the database!<br>" ;
		exit();
		&#125;
	if(!@mysql_select_db("amdayton_text",$dbcx)) &#123;
		echo "there was an error selecting the database!<br>" . mysql_error();
		exit();
		&#125;
	$result = mysql_query("SELECT * FROM quotes"); 
	if(!$result) &#123;
		echo "there was an error reading the data!" . mysql_error();
		exit();
		&#125;
	$num_rows = mysql_num_rows($result);
	$rand_row = rand(0, $numRows + 1);
	$result = mysql_query("SELECT * FROM quotes LIMIT $rand_row, 1");
	if(!$result) &#123;
		echo "there was an error reading the data!" . mysql_error();
		exit();
		&#125;
	while($row = mysql_fetch_array($result)) &#123;
		$quoteArray&#1111;0] = $row&#1111;quote];
		$quoteArray&#1111;1] = $row&#1111;author];
	&#125;
	return $quoteArray;
	&#125;
	
?>
Last edited by amdayton on Mon Jul 21, 2003 11:20 am, edited 1 time in total.
Monk
Forum Newbie
Posts: 8
Joined: Wed Jul 16, 2003 3:19 am
Location: Germany - Temporarily

Post by Monk »

With PHP you'll have to refresh the page, perhaps you want to look into JavaScript.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Unless....

You put that function in an include file, and simply refresh the include file. I am not sure if that'll refresh your whole page or not. You might also want to try and work with frames. Here's my include idea:

Code: Select all

//quote.php, random quote generator
   $dbcx = mysql_connect(localhost, amdayton, falilv33); 
   if(!$dbcx) { 
      echo "there was an error connecting to the database!<br>" ; 
      exit(); 
      } 
   if(!@mysql_select_db("amdayton_text",$dbcx)) { 
      echo "there was an error selecting the database!<br>" . mysql_error(); 
      exit(); 
      } 
   $result = mysql_query("SELECT * FROM quotes"); 
   if(!$result) { 
      echo "there was an error reading the data!" . mysql_error(); 
      exit(); 
      } 
   $num_rows = mysql_num_rows($result); 
   $rand_row = rand(0, $numRows + 1); 
   $result = mysql_query("SELECT * FROM quotes LIMIT $rand_row, 1"); 
   if(!$result) { 
      echo "there was an error reading the data!" . mysql_error(); 
      exit(); 
      } 
   while($row = mysql_fetch_array($result)) { 
      $quoteArray[0] = $row[quote]; 
      $quoteArray[1] = $row[author]; 
   } 
   echo $quoteArray; 
   echo "<meta http-equiv="refresh" content="3;url=quote.php">"; //this would refresh it every 3 seconds.
And in your index.php file, simply put:

Code: Select all

include ("quote.php");
//the rest of your code here
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

You can use frames or JavaScript. JavaScript is the best way to do this if you don't want to refresh the page and don't want to play around with frames.
User avatar
award
Forum Newbie
Posts: 13
Joined: Tue Jul 15, 2003 10:45 am
Location: Wakefield, UK
Contact:

Post by award »

I find taking the '@' out sometimes helps, as it give the true errors.
mrchris
Forum Newbie
Posts: 2
Joined: Sat Jul 19, 2003 6:31 pm

Refresh

Post by mrchris »

I seem to recall being about to set a refresh value in the http header.

So call header () with some time value. Sorry, I don't remember the exact syntax and I am too lazy to look it up.

Javascript will work too. Basically you need to put something in the html/javascript which will force it to issue another get or post so that you can generate a new quote.

Chris
Post Reply