Forum Moderators: coopster

Message Too Old, No Replies

Need PHP Wiz, only one echo instead of two

         

101Jen

6:54 pm on Nov 18, 2008 (gmt 0)

10+ Year Member



Hello put a couple images in here from somewhere just to show what I'm doing. I wrote this script so search engines could read some of it. This picks an item randomly from array $a on one page load and then from array $b on the next page load so the same random item won't be picked twice in a row. I need preg_grep because I use it to help me pick just what I want it to out of both arrays. Problem I'm having is its showing two random items on the page each page load instead of only one at a time. How can I have it show only one random item at a time from both the images and text from an array instead? Please let me know, thank you very much for your help.


<?php

$a[1] = "<p style='color: rgb(0,200,0)'>text 1</p>";
$a[2] = "<img border='0' title='title' src='http://images.bestbuy.com/BestBuy_US/en_US/images/abn/2007/cam/dept/n_cam_webcam.gif'/>";
$a[3] = "<p style='color: rgb(153,0,0)'>text 2</p>";

$b[1] = "<p style='color: rgb(0,0,200)'>text 3</p>";
$b[2] = "<img border='0' title='title' src='http://images.bestbuy.com/BestBuy_US/en_US/images/abn/2007/cam/dept/n_cam_photoprint.gif'/>";
$b[3] = "<p style='color: rgb(150,150,150)'>text 4</p>";

$geta = preg_grep('/(?:jpg)¦(?:gif)¦(?:png)/', $a);
$geta2 = preg_grep('/(?:<span)¦(?:<p)/', $a);
$getb = preg_grep('/(?:jpg)¦(?:gif)¦(?:png)/', $b);
$getb2 = preg_grep('/(?:<span)¦(?:<p)/', $b);

if(isset($_COOKIE["w"]) && $_COOKIE["w"]=="one"){
$img = $geta[array_rand($geta)];
$txt = $geta2[array_rand($geta2)];
setcookie("w", "two", time()+60*60*24*730);
}
else {
$img = $getb[array_rand($getb)];
$txt = $getb2[array_rand($getb2)];
setcookie("w", "one", time()+60*60*24*730);
}
echo $img;
echo $txt;
?>

cameraman

5:24 pm on Nov 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not seeing anything in the segment you posted which would result in more than one set being displayed.

101Jen

7:20 pm on Nov 19, 2008 (gmt 0)

10+ Year Member



Thank you. Right now the echo's are pulling two things on each page load instead of one.
echo $img;
echo $txt;

echo $img; pulls any images if there are images in the arrays.
echo $txt; pulls any text if there are any text in the arrays. So both pulls two results per page load.

How can I get it to pull only one random result per page load from all the images and all the text in an array instead of two results?

Please let me know, thank you very much for your help.

[edited by: 101Jen at 7:25 pm (utc) on Nov. 19, 2008]

cameraman

1:27 am on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I must not be understanding what you're asking.
echo $img;
displays one image, yes?

Then
echo $txt;
displays one text, yes?

Are you saying that you don't want both the image and the text, or that you want to combine the two arrays into one array, or...? I apologize, I just am not understanding what you're after.

101Jen

1:43 am on Nov 20, 2008 (gmt 0)

10+ Year Member



Thank you. I want it to pick one item randomly from both the images AND text, so whichever one it picks on that page load will appear so would only be 1 result at the very end instead of 2. Thank you very much for your help.

cameraman

3:45 am on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Alrightie, I think I understand now. The easiest way is to modify the regular expressions to keep the elements in a single array:
$geta = preg_grep('/(?:jpg)¦(?:gif)¦(?:png)¦(?:<span)¦(?:<p)/', $a);
$getb = preg_grep('/(?:jpg)¦(?:gif)¦(?:png)¦(?:<span)¦(?:<p)/', $b);

Alternatively, you could merge the two arrays:
$geta = array_merge($geta,$geta2);

101Jen

5:57 am on Nov 20, 2008 (gmt 0)

10+ Year Member



Thanks a lot for the great help. Hoping you wouldn't say that, in first post said I wouldn't be able to do that. Because sometimes I have the preg_greps look for different things. For instance sometimes I have them look for web site files or other things I need such as: (?:.html)etc.
Hope you or someone can help but really glad you know what I mean now.

[edited by: 101Jen at 5:58 am (utc) on Nov. 20, 2008]

cameraman

2:56 pm on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Leave the regex's as they are and just do the merge, then:
$geta = array_merge($geta,$geta2);
$getb = array_merge($getb,$getb2);

101Jen

7:11 pm on Nov 20, 2008 (gmt 0)

10+ Year Member



Thank you very much I tried this and no result is showing on the page. I'll send you some great links I collected if I can get this working.

<?php
$a= "<p style='color: rgb(0,200,0)'>text 1</p>";
$a[2] = "<img border='0' title='title' src='http://images.bestbuy.com/BestBuy_US/en_US/images/abn/2007/cam/dept/n_cam_webcam.gif'/>";
$a[3] = "<p style='color: rgb(153,0,0)'>text 2</p>";

$b[1] = "<p style='color: rgb(0,0,200)'>text 3</p>";
$b[2] = "<img border='0' title='title' src='http://images.bestbuy.com/BestBuy_US/en_US/images/abn/2007/cam/dept/n_cam_photoprint.gif'/>";
$b[3] = "<p style='color: rgb(150,150,150)'>text 4</p>";

$get1 = preg_grep('/(?:jpg)¦(?:gif)¦(?:png)/', $a);
$get2 = preg_grep('/(?:<span)¦(?:<p)/', $a);
$get3 = preg_grep('/(?:jpg)¦(?:gif)¦(?:png)/', $b);
$get4 = preg_grep('/(?:<span)¦(?:<p)/', $b);

$geta = array_merge($get1,$get2);
$getb = array_merge($get3,$get4);

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

Also tried this
$geta = array_rand($get1,$get2);
$getb = array_rand($get3,$get4);
if(isset($_COOKIE["w"]) && $_COOKIE["w"]=="one"){
$getresult = $geta;
setcookie("w", "two", time()+60*60*24*730);
}
else {
$getresult = $getb;
setcookie("w", "one", time()+60*60*24*730);
}
echo $getresult;
Then something shows on the page but its numbers of the array and not the content, or the variables $geta or $getb themselves.

[1][edited by: 101Jen at 7:27 pm (utc) on Nov. 20, 2008]

cameraman

8:16 pm on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's your original post, modified:
<?php

$a[1] = "<p style='color: rgb(0,200,0)'>text 1</p>";
$a[2] = "<img border='0' title='title' src='http://images.bestbuy.com/BestBuy_US/en_US/images/abn/2007/cam/dept/n_cam_webcam.gif'/>";
$a[3] = "<p style='color: rgb(153,0,0)'>text 2</p>";

$b[1] = "<p style='color: rgb(0,0,200)'>text 3</p>";
$b[2] = "<img border='0' title='title' src='http://images.bestbuy.com/BestBuy_US/en_US/images/abn/2007/cam/dept/n_cam_photoprint.gif'/>";
$b[3] = "<p style='color: rgb(150,150,150)'>text 4</p>";

$geta = preg_grep('/(?:jpg)¦(?:gif)¦(?:png)/', $a);
$geta2 = preg_grep('/(?:<span)¦(?:<p)/', $a);
$getb = preg_grep('/(?:jpg)¦(?:gif)¦(?:png)/', $b);
$getb2 = preg_grep('/(?:<span)¦(?:<p)/', $b);

$geta = array_merge($geta,geta2);
$getb = array_merge($getb,getb2);

if(isset($_COOKIE["w"]) && $_COOKIE["w"]=="one"){
$result = $geta[array_rand($geta)];
setcookie("w", "two", time()+60*60*24*730);
}
else {
$result = $getb[array_rand($getb)];
setcookie("w", "one", time()+60*60*24*730);
}
echo $result;
?>
That should work.

101Jen

9:26 pm on Nov 20, 2008 (gmt 0)

10+ Year Member



Thank you very much, adjusted a few missing variables. Nothing is still showing on the page.

<?php
$a[1] = "<p style='color: rgb(0,200,0)'>text 1</p>";
$a[2] = "<img border='0' title='title' src='http://images.bestbuy.com/BestBuy_US/en_US/images/abn/2007/cam/dept/n_cam_webcam.gif'/>";
$a[3] = "<p style='color: rgb(153,0,0)'>text 2</p>";

$b[1] = "<p style='color: rgb(0,0,200)'>text 3</p>";
$b[2] = "<img border='0' title='title' src='http://images.bestbuy.com/BestBuy_US/en_US/images/abn/2007/cam/dept/n_cam_photoprint.gif'/>";
$b[3] = "<p style='color: rgb(150,150,150)'>text 4</p>";

$get1 = preg_grep('/(?:jpg)¦(?:gif)¦(?:png)/', $a);
$get2 = preg_grep('/(?:<span)¦(?:<p)/', $a);
$get3 = preg_grep('/(?:jpg)¦(?:gif)¦(?:png)/', $b);
$get4 = preg_grep('/(?:<span)¦(?:<p)/', $b);

$geta = array_merge($get1,$get2);
$getb = array_merge($get3,$get4);

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

cameraman

1:00 am on Nov 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's working ok for me.
If you pasted code back from here, make sure you change the broken pipe ¦ characters to solid pipes in your regular expressions.

101Jen

1:57 am on Nov 21, 2008 (gmt 0)

10+ Year Member



The dratted broken pipe characters that was it. Thanks for the help! Fogot about the great merge.

[edited by: 101Jen at 2:12 am (utc) on Nov. 21, 2008]