Solved

php index problem

Posted on 2011-02-10
4
305 Views
Last Modified: 2012-05-11
I'm randomizing the images in the partner section in the right side bar ( http://www.gvfi.org/) but the hyper link is being randomized as well.  Any help to fix the bug would be greatly appreciated


<?php
function display_slide($path, $elementId, $objectName, $links)
{
$format=array("jpg","JPG","jpeg","JPEG","GIF","gif","PNG","png"); // determine file types
$img_rand=array(); //Create New Array for Images
$c1=sizeof($format); //we get the number of file types
for($i=0; $i<$c1; $i++){ //set counter for loop
$add_img=glob(".".$path."*.".$format[$i].""); //Searches all names of images with current extension "./images/*.jpg
$img_rand=array_merge($add_img,$img_rand); //we add to array $img_rand array $add_img
}
$c2=sizeof($img_rand); //get length of array for loop

shuffle($img_rand); //it mixes all items of array randomly

print '<div id="'.$elementId.'" class="SlidingPanels" style="height: 105px; overflow-x: hidden; overflow-y: hidden; tabindex="0" ">';
print '<div class="SlidingPanelsContentGroup" style="left: 0px; top: 0px; ">';

for($i = 0; $i < $c2; $i++) // loop through array of randomized images
{
if(isset($links))
{
$href = NULL;
if($i<sizeof($links)-1)
{
$href = $links[$i];
}
else
{
$href = "#";
}

print '<div id="p'.($i + 1).'" class="SlidingPanelsContent">'; //name div for spry sliding panels
print '<p><a target="_blank" href="'.$href.'" ><img src="'.$img_rand[$i].'" ></a></p>';
print '</div>';
}
else
{
print '<div id="p'.($i + 1).'" class="SlidingPanelsContent">'; //name div for spry sliding panels
print '<p><img src="'.$img_rand[$i].'" ></p>';
print '</div>';
}
}

print '</div></div>';

print '<script type="text/javascript">var '.$objectName.' = new Spry.Widget.SlidingPanels("'.$elementId.'");</script>'; //initialize object for script
}
?>
0
Comment
Question by:bluetundra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 2

Expert Comment

by:Nicholas Ford
ID: 34868155
This code is adding the images out of sync of the links.  You'll need to pair up the matching links & images and then shuffle those pairs (not shuffling the individual parts separately).
0
 
LVL 11

Accepted Solution

by:
Amar Bardoliwala earned 500 total points
ID: 34868353
Hello bluetundra,

as said by ndford005 in above comment, you need to take image of partner and its link together.

You can do it by creating multidimensional array for link and image and than shuffle that array.. so that you will get image and link as you want... following is an example of how your array should be.

$myarr[0] =array("image" => "google.gif", "link" => "http://www.google.com");  
$myarr[1] =array("image" => "nih.gif", "link" => "http://www.nih.com") ;
$myarr[2] =array("image" => "yahoo.gif", "link" => "http://www.yahoo.com") ;

Now you can shuffle $myarr and you will get both image and link together for shuffled array.

Hope this will help you.

Thanks.
0
 
LVL 1

Expert Comment

by:htcang
ID: 34868365
Look like you can't archive your goal with your currently approach. I have a solution to quick fix this: try to rename the file of the image to the url.

e.g: the google logo filename will be google.com.jpg (or google_com.jpg and you will need to replace the "_" to ".")

And then in the place where you print out your href, try to print out the the image string in the $img_rand array and make sure to remove the $path and the last dot part (e.g .png, .jpg)

Hope this helps
0
 
LVL 8

Expert Comment

by:rationalboss
ID: 34868368
<?php
function shuffle_assoc($list) {
  if (!is_array($list)) return $list;

  $keys = array_keys($list);
  shuffle($keys);
  $random = array();
  foreach ($keys as $key)
    $random[$key] = $list[$key];

  return $random;
} 

function display_slide($path, $elementId, $objectName, $links)
{
$format=array("jpg","JPG","jpeg","JPEG","GIF","gif","PNG","png"); // determine file types
$img_rand=array(); //Create New Array for Images
$c1=sizeof($format); //we get the number of file types
for($i=0; $i<$c1; $i++){ //set counter for loop
$add_img=glob(".".$path."*.".$format[$i].""); //Searches all names of images with current extension "./images/*.jpg
$img_rand=array_merge($add_img,$img_rand); //we add to array $img_rand array $add_img
}
$c2=sizeof($img_rand); //get length of array for loop

shuffle_assoc($img_rand); //it mixes all items of array randomly

print '<div id="'.$elementId.'" class="SlidingPanels" style="height: 105px; overflow-x: hidden; overflow-y: hidden; tabindex="0" ">';
print '<div class="SlidingPanelsContentGroup" style="left: 0px; top: 0px; ">';

for($i = 0; $i < $c2; $i++) // loop through array of randomized images
{
if(isset($links))
{
$href = NULL;
if($i<sizeof($links)-1)
{
$href = $links[$i];
}
else
{
$href = "#";
}

print '<div id="p'.($i + 1).'" class="SlidingPanelsContent">'; //name div for spry sliding panels
print '<p><a target="_blank" href="'.$href.'" ><img src="'.$img_rand[$i].'" ></a></p>';
print '</div>';
}
else
{
print '<div id="p'.($i + 1).'" class="SlidingPanelsContent">'; //name div for spry sliding panels
print '<p><img src="'.$img_rand[$i].'" ></p>';
print '</div>';
}
}

print '</div></div>';

print '<script type="text/javascript">var '.$objectName.' = new Spry.Widget.SlidingPanels("'.$elementId.'");</script>'; //initialize object for script
}
?>

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question