Log in

View Full Version : PHP Noob



maverick
06-29-2006, 02:38 AM
Hey,

I'v been working with html and javascript for a while but when it comes to php i really suck,I was wondering if anyone had a script to display random text,description & link,I have a small movie review site and I wanted to have a table that displays a new movie everytime you reload the page,with a small ss of the the movies,small description and link to the big description.:o thank you for the support

kind regards

Mav

djr33
06-29-2006, 06:56 AM
The php for this is easy. the issue is getting the information.
Would you store it in a database (best idea)? text file? Directly within the php code (worst idea)?

maverick
06-29-2006, 09:34 AM
since i'm planning on iframing that page i think directly on the script would be easier.

djr33
06-29-2006, 05:23 PM
It would create a really bulky page, and be limited later, especially for changing the values later on.
It is a bit easier to code... might work for your purposes. Integrating with a database or maybe text file could help you later on... something to think about later.

Basically... you could do something like this:

Put this at the top of your page:
<?php
$array[0] = "this is a quote";
$array[1] = "something else";
//add as many as you want, increasing the number each time.
$n = rand (0, 1); //Change the "1" to the highest value above.
?>

This outputs the quote.
Put it where you want the quote to go on your page:
<?php
echo $array[$n];
?>

The top part assigns values to an array then chooses a random number, assigning it to $n.
The bottom part takes that $n value and chooses the corresponding part of the array.
If you need more than just the quotes, you can use more than one array, assigning in the same exact way, just using something different than "array" in $array[0]. Like $quote[0], etc.
Then, when displaying them, just display all the variables there.

Note: Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.
that is from php.net. If you happen to be using a php version earlier than 4.2, you WILL need to seed the random number generator.

Twey
06-29-2006, 05:55 PM
<?php
$quote = array(
"Quote one",
"Quote two",
"Quote three"
);
$quote = $quote[rand(0, count($quote) - 1)];
?>

...

<?php echo($quote); ?>... would be a more elegant way to do it, I think.

djr33
06-29-2006, 08:33 PM
Indeed. I was going for simple, but that's much easier to manage. Good call.

Additionally, I figured my way taught what going on a little better, in hopes that maverick would learn php :)
But... now he's got two example, so that's better anyway, right?

Twey
06-29-2006, 08:47 PM
Lol, you may be right with the latter :)

djr33
06-29-2006, 08:50 PM
Sure, but either way. :)
(And, your example teaches me, so it's win-win-win :))

maverick
06-29-2006, 10:04 PM
thats for the help :) guys,so how would the text document script gets abouts?

Twey
06-29-2006, 10:37 PM
Text document script?

djr33
06-29-2006, 11:43 PM
As opposed to having it directly in the file?

Basically... you'd put the quotes in a text file, seperate by a particular character, likely a line break.
Then you'd have the php read the file, and seperate each quote by finding that character, splitting, then assigning the chunks into the array, and using the rest of the code like it is now.

Twey
06-29-2006, 11:49 PM
The file (http://www.php.net/file)() function is perfect here. It'll do all that for you, as simple as
$quote = file (http://www.php.net/file)("file");It's not very flexible, but it's convenient.

If you wanted to use another character (or string of characters) you could use:
$quote = file_get_contents (http://www.php.net/file-get-contents)("file");
$quote = explode (http://www.php.net/explode)("--- END OF QUOTE ---", $quote);That would give you the array you desire.

djr33
06-29-2006, 11:50 PM
Then the substr or other various functions for splitting strings will do that part for you, and it's pretty much done.

I think explode() would help :)

Twey
06-29-2006, 11:52 PM
Sorry, I edited :)

djr33
06-30-2006, 01:06 AM
Wait.... file() actually splits it into an array by default? I didn't know that...

Twey
06-30-2006, 01:14 AM
Indeed, by the system's linebreak, which makes it more portable than explode("\n", $str). That's the only difference between file() and file_get_contents().

djr33
06-30-2006, 01:48 AM
Hmm.. interesting. :)

Does file only do that? Is there a way to make it not? Or, I suppose, that's just using file_get_contents... always used that, never had a need for file. Interesting now, though.

Twey
06-30-2006, 02:09 AM
Does file only do that? Is there a way to make it not? Or, I suppose, that's just using file_get_contents...Yup :) I spent a long time using implode("\n", file("file")) before I noticed file_get_contents() :p

djr33
06-30-2006, 03:19 AM
Heh. I've just used file_get_contests()... never understood what the heck file() did, so just ignored it :p