?
Solved

php index problem

Posted on 2011-02-10
4
Medium Priority
?
308 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

718 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