PDA

View Full Version : PHP +Javascript?


RenegadeLeader
11-18-2003, 02:25 PM
Ok, im gunna use a random script to select random things out of a DB. Now, heres the Random script in JavaScript:

<SCRIPT LANGUAGE='Javascript'><!--


function text() {
};

text = new text();
number = 0;

// textArray
text[number++] = 'Random text string 1.'
text[number++] = 'Random text string 2.'
text[number++] = 'Random text string 3.'
text[number++] = 'Random text string 4.'
text[number++] = 'Random text string 5.'
// keep adding items here...

increment = Math.floor(Math.random() * number);

document.write(text[increment]);

//--></script>";



So how would i use that with PHP?

Rigger
12-16-2003, 09:21 AM
If I understand, you just need write on page randomly some message stored in DB?
I thing you can do it in cooler way. Something like this:

<?php
mysql_connect(<db_server>, <user>, <password>);
mysql_Select_DB(<db_name>);
$num_rows=mysql_Result(mysql_Query("SELECT Count(*) FROM <table_with_texts>"), 0, 0); // getting count of texts
$rand=Rand(0, $num_rows); // generating random index in the range
$text=mysql_Result(mysql_Query("SELECT message FROM <table_with_texts> LIMIT $rand,1"), 0, 0); // get just the one random message

echo $text; // writing the text
?>