Jump to content


Photo

help with arrays and filtering


  • Please log in to reply
3 replies to this topic

#1 joshberm

joshberm
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 14 September 2006 - 06:25 PM

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


#2 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 14 September 2006 - 06:52 PM

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.
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#3 joshberm

joshberm
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 14 September 2006 - 06:57 PM

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?

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 14 September 2006 - 07:32 PM

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

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

?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users