Forum Moderators: coopster

Message Too Old, No Replies

help with text

         

Simone100

7:03 pm on Jan 10, 2008 (gmt 0)

10+ Year Member



Hello, I need an array so I can have it pick from more text if I need it. So I have arrays with a shuffle. With this I'm not getting any error messages but a number 1 is appearing and nothing else. Anyone know why? Its not picking up the 1 here [], I checked and changed it to 2 and its still showing a 1 and nothing else. Please let me know how to get "test1" or "test2" to show with a shuffle or php random commands. Thank you very much.

<?php
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
$one = Array();
$one[1] = "test";
$two = Array();
$two[1] = "test2";
$get = shuffle($one,$two);
echo $get;
?>

PHP_Chimp

7:12 pm on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$get = shuffle($one,$two);
echo $get;

From the manual -
bool shuffle ( array &$array )

shuffle returns true or false. So as shuffle worked $get = true = 1.

If you want to randomize the arrays then first you could put them together, then call array_rand [uk3.php.net] to randomize it.


<?php
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
// dont know why you were using 2 arrays so iv converted it all into 1
$main = array();
$main[0] = 'test1';
$main[1] = 'test2';
$rand = array_rand($main);
echo $main[$rand];
// if you want more than 1 answer
$rand_2 = array_rand($main, 2);
echo $main[$rand[0]];
echo $main[$rand[1]];
?>

[edited by: PHP_Chimp at 7:13 pm (utc) on Jan. 10, 2008]

Simone100

8:57 pm on Jan 10, 2008 (gmt 0)

10+ Year Member



Thanks very much. I'm trying to get it so that if it chooses a random item from array one, then next time it will choose a random item from array two. How do I write that? Does it need a cookie so it knows what it showed the time before?
setcookie("a", $notsurewhatgoeshere, time()+60*60*24*180);
Please let me know thank you very much.

[php]<?php
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
$one = Array();
$one= "test";
$two = Array();
$two[1] = "test2";
$get = shuffle($one,$two);
echo $get;
?>[/php]

[1][edited by: Simone100 at 9:11 pm (utc) on Jan. 10, 2008]

Simone100

12:58 am on Jan 11, 2008 (gmt 0)

10+ Year Member



I'm bringing it back to basics first per your great example to get the first part working, then I'll try to get the second working after that so here's what I have.

PHP Code:

<? php
$one = Array();
$one[1] = "test1";
$one[2] = "test2";
$one[3] = "test3";
$one[4] = "test4";
$get = array_rand($one);
echo $get;
?>

Instead of showing "test1" ect. its only showing 1,2,3, or 4. Anyone know why? Thanks a lot.

PHP_Chimp

6:48 pm on Jan 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



array_rand returns a key, so as you have 4 members of that array you should be getting 1-4 returned, as you started your array from 1.

From the manual -

If you are picking only one entry, array_rand() returns the key for a random entry. Otherwise, it returns an array of keys for the random entries. This is done so that you can pick random keys as well as values out of the array.

array_rand [uk.php.net] manual page.

So you use the return from array_rand, in your example stored in $get as the key for the answer that you want. So if you change the last line to -


echo $one[$get];

You should have the value that you were expecting.

Simone100

7:43 pm on Jan 11, 2008 (gmt 0)

10+ Year Member



Thanks a lot. Here's what I have now. The problem I'm having is its only showing a random item from array $two and but never from $one on other page loads. Anyone know why? Please let me know, thank you very much.

PHP Code:
<?php
setcookie("array", "two", time()+60*60*24*180);
$one = Array();
$one= "test1";
$one[2] = "test2";
$two = Array();
$two[5] = "test5";
$two[6] = "test6";
?>

PHP Code:
<?php
if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="two"){
setcookie("array", "one", time()+60*60*24*180);
echo $two[array_rand($two)];}
if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="one"){
setcookie("array", "two", time()+60*60*24*180);
echo $one[array_rand($one)];}
?>

[1][edited by: Simone100 at 7:44 pm (utc) on Jan. 11, 2008]

PHP_Chimp

8:37 pm on Jan 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are your cookies actually getting reset?

Simone100

8:53 pm on Jan 11, 2008 (gmt 0)

10+ Year Member



Good thinking, I don't know.
Do you think its just stacking the cookies instead of changing them? Like
array, one
array, two
both kept on computer instead of changing one into the two? Does anyone know the code that will change the second value of the cookie into another instead of just stacking it? That might not be whats doing it at all, just need some advice.

Simone100

9:41 pm on Jan 11, 2008 (gmt 0)

10+ Year Member



Here's the problem. This works fine, until it goes into a web page or is included in a web page with a php include. Then it quits working. It gives header errors because it doesn't like the cookies set near the echo's. Anyone know how I can keep this from giving header errors in a actual web page? Please let me know, thank you very much.

<?php
$one = Array();
$one[1] = "test1";
$one[2] = "test2";
$two = Array();
$two[5] = "test5";
$two[6] = "test6";

if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="two"){
setcookie("array", "one", time()+60*60*24*180);
echo $two[array_rand($two)];
}else{
setcookie("array", "two", time()+60*60*24*180);
echo $one[array_rand($one)];
}
?>

PHP_Chimp

9:54 pm on Jan 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have 2 choices -
1) use output buffering so that you can send headers after html content.
2) place the cookie code at the very top of your page, so that there is no html output before it. Then call the echo statement wherever you want it.

Buffering is the worst option here, as there is no need to increase server load in this case. There are some cases where it is difficult to reposition code so that headers are not send in the middle of a script. So you use buffering to get around that problem. This is not the case in your script. You may choose to use buffering to compress your pages, but that is a separate thing that we are not talking about.
So I would suggest option 2, as this should be easy to do.
i.e.


<?php
$one = Array();
$one[1] = "test1";
$one[2] = "test2";
$two = Array();
$two[5] = "test5";
$two[6] = "test6";
if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="two"){
setcookie("array", "one", time()+60*60*24*180);
// echo $two[array_rand($two)];
}else{
setcookie("array", "two", time()+60*60*24*180);
//echo $one[array_rand($one)];
}
$cookie = $_COOKIE['array'];
?>
<html>
<head>
</head>
<body>
<?php echo $cookie[array_rand[$cookie]];?>
<p>Did it work?</p>
</body>
</html>

Simone100

10:33 pm on Jan 11, 2008 (gmt 0)

10+ Year Member



That was it! Thanks a lot~!