Page 1 of 1

array_filter

Posted: Thu Jan 22, 2004 2:19 pm
by pinehead18
Can i use array filter to tell this script only to post the results of the foreach statement only if they don't equal default.gif? If so suggestions on how to do that. The manual on array filter is a little not so clear for me.

$sql = "SELECT * FROM users WHERE user='$name'";
$result = mysql_query($sql,$con);
$row = mysql_fetch_array($result) or die(mysql_error());

$mainpic = $row['mainpic'];

$pic1 = $row['pic1'];
$pic2 = $row['pic2'];
$pic3 = $row['pic3'];
$pic4 = $row['pic4'];

$pics = array($pic1,$pic2,$pic3,$pic4);


foreach ($pics as $key => $value)
{

if ($value == "default.gif") { unset ($pics[$key]); }

}



foreach($pics as $picname)
{
$table .= "
<td align=center><a href=http://lc.com/viewimage.php?image=$picname&name=$name> <span style='text-decoration: none'><img
src=http://lc.com/pics/$picname height=80 width=80></a></span></td>
";
}

Code: Select all

<?php

?>

Code: Select all

<?php

?>

Posted: Thu Jan 22, 2004 2:34 pm
by markl999
Why not just do :
foreach($pics as $picname)
{
if($picname != 'default.gif'){
.......
}
}

??

Posted: Thu Jan 22, 2004 8:10 pm
by pinehead18
Doesn't this tell the script that even if one of the vars in the array == default.gif dont' show all of them?

Posted: Thu Jan 22, 2004 8:11 pm
by pinehead18
reason i ask is becuase

foreach($pics as $picname)
{
if($picname != 'default.gif'){
$table .= "
<td align=center><a href=http://l.com/viewimage.php?image=$picname&name=$name> <span style='text-decoration: none'><img
src=http://li.com/pics/$picname height=80 width=80></a></span></td>
";
}
}


That doesn't show anything even though i have 3 pics set.
When i echo $picname it shows the vars in the array. 3 of them are default.gif then one is the right image name. Not seperated by anything.

So if i'm reading it right, from what i understand is the if statement says that if $picname has default.gif anywhere in it then don't show the table.

Sorry for being such a noob this is like the last part of code on my site then it is finished WOOT just gotta get past this bug :(

Posted: Thu Jan 22, 2004 8:23 pm
by markl999
What's the format of $pics? i.e what does a var_dump($pics); look like?

Posted: Fri Jan 23, 2004 8:18 am
by pinehead18
array(4) { [0]=> string(11) "default.gif" [1]=> string(23) "image20040121094938.jpg" [2]=> string(11) "default.gif" [3]=> string(11) "default.gif" }


that is var_dump oif $pics before the foreach loop

Thank you again for your help

Posted: Fri Jan 23, 2004 8:33 pm
by pinehead18
did ya by any chance have time to take look? I'm still seeming to be lost

Posted: Fri Jan 23, 2004 8:39 pm
by qads
pinehead18 wrote:if $picname has default.gif anywhere in it then don't show the table
so $picname can be "bblablabla_default.gif"?

if so, give this a try:

Code: Select all

<?php
foreach($pics as $picname)
{
if(!strstr('default.gif', $picname))
{ 
$table .= "
<td align=center><a href=http://l.com/viewimage.php?image=$picname&name=$name> <span style='text-decoration: none'><img
src=http://li.com/pics/$picname height=80 width=80></a></span></td>
";
} 
}
?>

Posted: Sat Jan 24, 2004 9:02 am
by pinehead18
And this is going to filter out all the vars in the array that == default.gif while still displaying the ones that do not equal default.gif?

Thank you all for your help on this matter.

My one last bug i have to deal with :([/list]

Posted: Sat Jan 24, 2004 11:30 am
by pinehead18
reason i ask is becuase it did not work.