Solved

php index problem

Posted on 2011-02-10
4
300 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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

706 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

16 Experts available now in Live!

Get 1:1 Help Now