Jump to content

Archived

This topic is now archived and is closed to further replies.

joshberm

help with arrays and filtering

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>';
}

Share this post


Link to post
Share on other sites
rather than lots of if statements look up switch in the php manual.
now to check if they're blank also look at using explode and a for each loop to break the array, then use an if statement to check if that field is empty.

Share this post


Link to post
Share on other sites
I'm very new to PHP.... I really am not sure what you mean.

Can you give me an example of the syntax needed to actually REMOVE the unwanted values?

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.