Solved

php index problem

Posted on 2011-02-10
4
301 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
4 Comments
 
LVL 2

Expert Comment

by:ndford005
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

930 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now