Jump to content

help with arrays and filtering


joshberm

Recommended Posts

Hello all.... hoping somebody can help me with this.  I've got a little image swapper script I'm trying to install.  The image displaying and swapping part works, but what I need to figure out is how to hide certain images.

I pass $usepic1 and $usepic2 etc. into the function as 'yes' or 'no'.  These will be used to create an array to hold the image source, thumbnail source, and ALT text.

What I'd like to do is filter this array so that the 'blank' records are removed completely.  Then re-order the array so that $i can start at 0 and display all the results.  Any other suggestions would be appreciated.

I have tried setting the $imagesArray["thumbsrc"][0] to empty single quotes, and this would seem to be the easiest way.  Firefox hides the image altogether but IE displays the red X placeholder image.

I'm pretty new to all this, so I would really appreciate any ideas.  Thanks!




function slideshow($usepic1, $usepic2, $usepic3, $usepic4, $usepic5, $usepic6, $idoflink, $titleoflink, $urlofdirectory) {

$imagesArray=array();
if ($usepic1 == 'yes') {
$imagesArray["thumbsrc"][0]=$urlofdirectory . '/thumbnail.php?id=' . $idoflink . '&field=attachpic1';
$imagesArray["src"][0]=$urlofdirectory . '/download.php?id=' . $idoflink . '&field=attachpic1';
$imagesArray["alt"][0]=$titleoflink; }
else {
$imagesArray["thumbsrc"][0]='blank'; }

if ($usepic2 == 'yes') {
$imagesArray["thumbsrc"][1]=$urlofdirectory . '/thumbnail.php?id=' . $idoflink . '&field=attachpic2';
$imagesArray["src"][1]=$urlofdirectory . '/download.php?id=' . $idoflink . '&field=attachpic2';
$imagesArray["alt"][1]=$titleoflink; }
else {
$imagesArray["thumbsrc"][1]='blank'; }

if ($usepic3 == 'yes') {
$imagesArray["thumbsrc"][2]=$urlofdirectory . '/thumbnail.php?id=' . $idoflink . '&field=attachpic3';
$imagesArray["src"][2]=$urlofdirectory . '/download.php?id=' . $idoflink . '&field=attachpic3';
$imagesArray["alt"][2]=$titleoflink; }
else {
$imagesArray["thumbsrc"][2]='blank'; }

if ($usepic4 == 'yes') {
$imagesArray["thumbsrc"][3]=$urlofdirectory . '/thumbnail.php?id=' . $idoflink . '&field=attachpic4';
$imagesArray["src"][3]=$urlofdirectory . '/download.php?id=' . $idoflink . '&field=attachpic4';
$imagesArray["alt"][3]=$titleoflink; }
else {
$imagesArray["thumbsrc"][3]='blank'; }

if ($usepic5 == 'yes') {
$imagesArray["thumbsrc"][4]=$urlofdirectory . '/thumbnail.php?id=' . $idoflink . '&field=attachpic5';
$imagesArray["src"][4]=$urlofdirectory . '/download.php?id=' . $idoflink . '&field=attachpic5';
$imagesArray["alt"][4]=$titleoflink; }
else {
$imagesArray["thumbsrc"][4]='blank'; }

if ($usepic6 == 'yes') {
$imagesArray["thumbsrc"][5]=$urlofdirectory . '/thumbnail.php?id=' . $idoflink . '&field=attachpic6';
$imagesArray["src"][5]=$urlofdirectory . '/download.php?id=' . $idoflink . '&field=attachpic6';
$imagesArray["alt"][5]=$titleoflink; }
else {
$imagesArray["thumbsrc"][4]='blank'; }

$useImage=1; // set default image value (equal to array index number)

if(isset($_REQUEST)){
while(list($key,$val)=each($_REQUEST)){
  if(strstr($key,"_img_x")){
  $useImage=str_replace("_img_x","",$key);
  break;
  }
}
}

echo '<form method="post" action="' . $_SERVER["PHP_SELF"] . '" id="imageSwapperForm" onsubmit="return false">';

for($i = 0; $i < count($imagesArray["src"]); $i++){
echo '<input style="border: 0; margin: 15px" type="image" name="'.$i.'_img" value="'.$i.'_img" src="'.$imagesArray["thumbsrc"][$i].'" alt="'.$imagesArray["alt"][$i].'" onclick="document.getElementById(\'img\').src=\''.$imagesArray["src"][$i].'\'; document.getElementById(\'img\').alt=this.alt">';
}

echo '<p><img id="img" src="' . $imagesArray["src"][$useImage] . '" border="0" alt="' . $imagesArray["alt"][$useImage] . '"></p></form>';
}
Link to comment
Share on other sites

I'd be tempted to make $usepic an array also, then the function becomes a single loop

[code]<?php

function slideshow($usepic, $idoflink, $titleoflink, $urlofdirectory) {
    $imagesArray = array();
    foreach ($usepic as $k => $val) {
        $k++;
        if ($val=='yes')  {
            $imagesArray[] = array (
                'thumbsrc' => "$urlofdirectory/thumbnail.php?id=$idoflink&field=attachpic$k",
                'src' => "$urlofdirectory/download.php?id=$idoflink&field=attachpic$k",
                'alt' => $titleoflink
                );
        }
    }
    return $imagesArray;
}

$usepic = array ('yes', 'no', 'yes', 'yes', 'no', 'no');
$slideshow = slideshow ($usepic, 'theID', 'theTitle', '/path/to/dir');

//view slideshow array
echo '<pre>', print_r($slideshow, true), '</pre>';

?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.