Page 1 of 1

stupid question

Posted: Fri Jul 18, 2003 3:35 am
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;
	
?>

Posted: Fri Jul 18, 2003 5:36 am
by Monk
With PHP you'll have to refresh the page, perhaps you want to look into JavaScript.

Posted: Fri Jul 18, 2003 2:20 pm
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

Posted: Fri Jul 18, 2003 6:44 pm
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.

Posted: Sat Jul 19, 2003 4:47 pm
by award
I find taking the '@' out sometimes helps, as it give the true errors.

Refresh

Posted: Sat Jul 19, 2003 6:31 pm
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