Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

Help with a simple PHP variable assignment script.

Links for php scripts

Moderators: macek, egami, gesf

Help with a simple PHP variable assignment script.

Postby cashn247 » Thu Jul 22, 2004 11:40 am

Hello. I really hope you guys can help me with this query.

Say I have a simple PHP code like the following in a web page called display.php:

<?PHP
$ShowBid = 0;
$Terms = "home based business";
$Page = "1";
?>

Now I have a list of keywords in a text file called 'terms_array.txt,' one per line like so:

work from home
work at home
home job
home business
web business

I want the variable '$Term,' to be assigned a randomly selected keyword found in the terms_array.txt file each time the display.php page is loaded.

So it could say:

$Terms = "web business";

or...

$Terms = "work at home";

or...

$Terms = "home job";

And so forth.

How can such a routine be scripted using PHP? I want the code to be very fast, meaning I don't want the page taking forever to load, because 25 lines of PHP code is being performed beneathe the page.

Please help. Thanks.
cashn247
New php-forum User
New php-forum User
 
Posts: 5
Joined: Thu Jul 22, 2004 11:32 am
Location: United States

Postby swirlee » Thu Jul 22, 2004 11:52 am

Load the text file into an array using the file() function.

Count the number of items in the array using the count() count function.

Pick a random array index (less than the above number) using the rand() function.
Last edited by swirlee on Thu Jul 22, 2004 1:23 pm, edited 1 time in total.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby cashn247 » Thu Jul 22, 2004 1:14 pm

Thanks for the tip. Is it possible you can show me exactly how this type of script would be constructed? Maybe use the example I've given? I am by no means knowledgable in PHP programming. :-D
cashn247
New php-forum User
New php-forum User
 
Posts: 5
Joined: Thu Jul 22, 2004 11:32 am
Location: United States

Postby swirlee » Thu Jul 22, 2004 1:24 pm

This is really basic PHP. I appreciate your quandry, but your best bet is to start with the Introductory Tutorial and maybe a good PHP book.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby Dabear » Thu Jul 22, 2004 1:32 pm

<?php

$file
= file('terms_array.txt');

$counted = count($file);


echo
$file[rand(0, $counted)];
?>
User avatar
Dabear
New php-forum User
New php-forum User
 
Posts: 24
Joined: Thu Jul 22, 2004 12:48 pm
Location: Norway

Postby cashn247 » Thu Jul 22, 2004 3:14 pm

Ok. I can respect what this forum is all about, and that is helping yourself.

I have come up with the below code:

<?PHP
srand((float) microtime() * 10000000);
$input = array("home job", "work at home", "home based business", "business opportunity", "home employment");
$rand_terms = array_rand($input, 5);
$ShowBid = 0;
$Terms = "$rand_terms";
$Page = "1";
?>

There is one major problem with this code, the $Terms variable is given the phrase "rand_terms," instead of one of the five search terms (home job, work at home, home based business...)

I also tried:
$Terms = echo $rand_terms; with the same rotten results.

Can someone at least help me out here. I am sincerely trying to do this on my own.
cashn247
New php-forum User
New php-forum User
 
Posts: 5
Joined: Thu Jul 22, 2004 11:32 am
Location: United States

Postby swirlee » Thu Jul 22, 2004 3:27 pm

You're trying to do this:
Code: Select all
<?php
$Terms = $rand_terms;
?>

But there's no need to do that at all, since you're just copying the same data from one variable name to another. Just use $rand_terms (or change its name to $Terms.

By the way, if you're using PHP 4.2 or greater, there's no need to use srand(). Rands are seeded automatically.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby cashn247 » Thu Jul 22, 2004 3:48 pm

Ok, I have narrowed the script down to:

<?PHP
$input = array("home job", "work at home", "home based business", "business opportunity", "home employment");
$ShowBid = 0;
$Terms = array_rand($input, 5);
$Page = "1";
?>

I still get the problem of $Terms being assigned the value of array_rand. It is like the array_rand function does not have time to run, before the actual <?php script runs as a whole. Is there anyway to tell php to, Run this code first, then continue on with running the rest of the code.
cashn247
New php-forum User
New php-forum User
 
Posts: 5
Joined: Thu Jul 22, 2004 11:32 am
Location: United States

Postby swirlee » Thu Jul 22, 2004 3:53 pm

The trouble is that, per the documentation, array_rand() returns the key(s) for the random element(s) chosen, not the value of the element. To retrieve the value, use the key just like any array key. Also, you're telling it to return 5 keys, but I'm assuming that you only want one, not all of them, and since the function defaults to returning just one, you can skip the second argument. So this is probably more like what you want

Code: Select all
<?php
$Terms = $input[array_rand($input)];
?>
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby cashn247 » Thu Jul 22, 2004 4:15 pm

Yes! That did the trick! Thanks so much. Have learned a great deal from you guys assistance. Quite HAPPY! :D
cashn247
New php-forum User
New php-forum User
 
Posts: 5
Joined: Thu Jul 22, 2004 11:32 am
Location: United States


Return to PHP Scripts

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.