Jump to content

matching arrays


11Tami

Recommended Posts

Hello, this pulls an image with a title on every next page load. Problem is when it pulls image one I need it to also pull "title 1" and when it pulls image 3 I need it to pull "title 3" right now its not doing that. Right now its sticking whatever title it wants in with the images. How can I get to match the arrays while its also pulling something randomly? Please let me know, thank you very much.

 

<?php
$title1[1] = "title1";
$title1[2] = "title2";
$title2[3] = "title3";
$title2[3] = "title3";
$img1[1] = "http://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/BahnhofBludenz.JPG/120px-BahnhofBludenz.JPG";
$img1[2] = "http://upload.wikimedia.org/wikipedia/commons/thumb/3/3c/Korean_stew-Budae_jjigae-01.jpg/120px-Korean_stew-Budae_jjigae-01.jpg";
$img2[3] = "http://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Basilica_Cistern%2C_Constantinople.jpg/120px-Basilica_Cistern%2C_Constantinople.jpg";
$img2[4] = "http://upload.wikimedia.org/wikipedia/commons/thumb/c/c4/A330-200F.jpg/120px-A330-200F.jpg";
if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="two"){ 
  setcookie("array", "one", time()+60*60*24*180); 
$get2 = $title2[array_rand($title2)];
$get3 = $img2[array_rand($image2)];
}else{ 
  setcookie("array", "two", time()+60*60*24*180); 
$get2 = $title1[array_rand($title1)];
$get3 = $img1[array_rand($image1)];
}
echo "<img title='$get2' src='$get3' border='0'>"; 
?>

Link to comment
Share on other sites

The ordering of your array doesn't make sense here.

 

$title2[3] = "title3";

$title2[3] = "title3";

 

Those 2 lines are the same thing. And what happened to $title2[1] and $title2[2]?

 

Edit:

 

Same here:

$img2[3] = "http://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Basilica_Cistern%2C_Constantinople.jpg/120px-Basilica_Cistern%2C_Constantinople.jpg";

$img2[4] = "http://upload.wikimedia.org/wikipedia/commons/thumb/c/c4/A330-200F.jpg/120px-A330-200F.jpg";

Link to comment
Share on other sites

Change this

<?php
if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="two"){ 
  setcookie("array", "one", time()+60*60*24*180); 
$get2 = $title2[array_rand($title2)];
$get3 = $img2[array_rand($image2)];
}else{ 
  setcookie("array", "two", time()+60*60*24*180); 
$get2 = $title1[array_rand($title1)];
$get3 = $img1[array_rand($image1)];
}
?>

To this

<?php
if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="two"){
  setcookie("array", "one", time()+60*60*24*180);
  $image = rand(1,count($image2) );
  $get2 = $title2[$image];
  $get3 = $img2[$image];
}else{
  setcookie("array", "two", time()+60*60*24*180);
  $image = rand(1,count($image2) )
  $get2 = $title2[$image];
  $get3 = $img2[$image];
}
?>

Link to comment
Share on other sites

Just generate a random number instead of using array_rand() or assign the return of array_rand() so you know what it is and can use it on the title.

 

Example - do this if array starts with zero:

 

$nbr = mt_rand(0, count($title1) - 1);      or this if starts with 1:  $nbr = mt_rand(1, count($title1));

 

or this:

 

$nbr = array_rand($title1);

 

 

Then you can use it in this way:

 

$get2 = $title1[$nbr];

$get3 = $img1[$nbr];

 

I assume there's always going to be the same number of titles and images.

 

 

Link to comment
Share on other sites

Sorry for the numbers. Ok thanks a lot, its almost there now. Its showing whats in array title1 and img1 and matching them now but its not showing the rest. I have this.

 

<? php
$title1[1] = "title1";
$title1[2] = "title2";
$title2[3] = "title3";
$title2[4] = "title4";
$img1[1] = "http://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/BahnhofBludenz.JPG/120px-BahnhofBludenz.JPG";
$img1[2] = "http://upload.wikimedia.org/wikipedia/commons/thumb/3/3c/Korean_stew-Budae_jjigae-01.jpg/120px-Korean_stew-Budae_jjigae-01.jpg";
$img2[3] = "http://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Basilica_Cistern%2C_Constantinople.jpg/120px-Basilica_Cistern%2C_Constantinople.jpg";
$img2[4] = "http://upload.wikimedia.org/wikipedia/commons/thumb/c/c4/A330-200F.jpg/120px-A330-200F.jpg";
if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="two"){ 
  setcookie("array", "one", time()+60*60*24*180);
$nbr1 = mt_rand(1, count($title2));
$nbr2 = mt_rand(1, count($img2));
}else{ 
  setcookie("array", "two", time()+60*60*24*180); 
$nbr1 = mt_rand(1, count($title1));
$nbr2 = mt_rand(3, count($img1));
}
$get1 = $title1[$nbr1];
$get2 = $img1[$nbr1];
echo "<img title='$get1' src='$get2' border='0'>"; 
?>

Link to comment
Share on other sites

Sorry about that everything froze, I couldn't finish my post.

 

I have what I just listed above and I can't get it to show whats in $title2

and $img2 arrays. Everything I try doesn't work. Please let me know how to fix this. Thank you very much.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.