Forum Moderators: coopster

Message Too Old, No Replies

Posting a PHP variable in HTML without a form

php html pass variable

         

t1mmy

4:07 pm on May 23, 2006 (gmt 0)

10+ Year Member



First off, I'm an über n00b when it comes to PHP; very skilled in HTML and CSS though, and am hoping that someone can help me. My server is PHP enabled, however I can't get this code to work to save my life. I've searched this site extensively and the web, with no luck.

What I'm trying to do is to display a random testimonial quote that would change on each HTML page. The quotes are located called randomTestimonial.php.

PHP code:
<?php
$testimonial[] = "copy1";
$testimonial[] = "copy2";
$testimonial[] = "copy3";

shuffle($testimonial);
?>

I'm trying to place these quotes in a formatted <div> container. Here's the HTML that I've tried to get working.

<div class="nav_testimonial">
<?
include "css/randomTestimonial.php"
print($testimonial);
?>
</div>

Makes sense to me, but doesn't work.

alce

4:22 pm on May 23, 2006 (gmt 0)

10+ Year Member



Google for "php random quotes". You should find at least a dozen little scripts that do what you need.

t1mmy

4:28 pm on May 23, 2006 (gmt 0)

10+ Year Member



THANK YOU!

that just gets me though, why didn't i think to search for those specific terms? i typed in post html php variable, etc. never thought of typing in quote. DUH!

benlieb

4:29 pm on May 23, 2006 (gmt 0)

10+ Year Member



this won't do much if you don't give it an index:
<?php
$testimonial[] = "copy1";
?>

try:
<?php
$testimonial[1] = "copy1";
?>

or use array_push() to get strings into an array.

Since $testimonial is an array, you can't print() it without specifying which index to print.
<?
print($testimonial);
?>

<?
print($testimonial[1]);
?>

proper_bo

4:30 pm on May 23, 2006 (gmt 0)

10+ Year Member




Shuffle is a very unrandom way of getting a random item. You should search for how to make a truly random item.

But:

You are asking it to print $testimonial but that is an array not a single part of an array.

print($testimonial[0]); would work but will be far ideal. So you really want is:

include('css/randomTestimonial.php');
$num = (random number between 0 and your number of testimonials)
print $testimonial[$num];

Hope that helps. Search how to get a random item;

dreamcatcher

4:52 pm on May 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why not just use:

print($testimonial[1]);

Its not necessary to specify an indice number.

dc

eelixduppy

5:57 pm on May 23, 2006 (gmt 0)



>>>Shuffle is a very unrandom way of getting a random item. You should search for how to make a truly random item.

For this particular purpose, shuffle is perfectly fine to use to get a random item from an array. And technically, there is no such thing as a perfect random number :)

You could also use rand [us3.php.net], or one of its brother functions, to generate a random array index.

proper_bo

11:13 pm on May 23, 2006 (gmt 0)

10+ Year Member



For this particular purpose, shuffle is perfectly fine to use to get a random item from an array. And technically, there is no such thing as a perfect random number :)

You could also use rand, or one of its brother functions, to generate a random array index.


Not 'you could also use rand' more like 'you should use rand'.

The idea was to show a random testimonial. Using shuffle will show some testimonials far more often than others. Not random at all.

I know that 'technically, there is no such thing as a perfect random number' but you can get far closer to it than using shuffle.

eelixduppy

11:47 pm on May 23, 2006 (gmt 0)



I created a little script to test both:

shuffle


<?php
for($i = 1; $i < 25; $i++)
{
$nums = array('1','2','3','4','5','6','7','8','9','10');
shuffle($nums);
echo $i.": ".$nums[1]."<br>";
}
?>

This produced the following on my computer:
6,10,9,2,1,10,6,3,5,3,6,7,8,2,4,9,4,2,4,6,1,4,8,1

rand


$nums = array('1','2','3','4','5','6','7','8','9','10');
for($i = 1; $i < 25; $i++)
{
$rand = rand(0,9)
echo $i.": ".$nums[$rand]."<br>";
}

This produced the following on my computer:
7,7,3,8,9,5,9,5,6,6,8,4,3,10,3,2,8,4,10,3,8,9,2,6

My conclusion is, because of how rand() works, it is not good if you are calling it within a loop because it will frequently have the same value as the number before. Shuffle, however, seems to be random in the loop. Both overall display random results.

jatar_k

11:59 pm on May 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



there seems to be a fundamental understanding here, well actually a couple

firstly

ran·dom
1. Having no specific pattern, purpose, or objective: random movements. See Synonyms at chance.
2. Mathematics & Statistics. Of or relating to a type of circumstance or event that is described by a probability distribution.
3. Of or relating to an event in which all outcomes are equally likely, as in the testing of a blood sample for the presence of a substance.

to have something be random just means patternless or "all outcomes are equally likely"

>> because it will frequently have the same value as the number before

not true, if it is random, then it is random, the same number may reoccur because for each iteration every number in your sequence has an equal probability of occuring

there are functions that are more truly random than others. You used to always have to seed rand functions because otherwise they would produce the same sequence over and over. There is an explanation as to why, you can look it up if you like. ;)

secondly there seems to be confusion as to how shuffle works
[php.net...]

This function shuffles (randomizes the order of the elements in) an array.

shuffle is also random and as far as I know the quality of randomness is equal for both functions

therefore

the argument is moot, the two are equal ;)

if we wanted to show the testimonials with an equal weighting then that would be something completely different

t1mmy

8:46 pm on May 24, 2006 (gmt 0)

10+ Year Member



thanks everyone! this site is amazing because the great community that helps a lot of people like me.