Solved

php index problem

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
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 …

777 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