array_filter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

array_filter

Post 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

?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Why not just do :
foreach($pics as $picname)
{
if($picname != 'default.gif'){
.......
}
}

??
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post 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?
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post 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 :(
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

What's the format of $pics? i.e what does a var_dump($pics); look like?
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post 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
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

did ya by any chance have time to take look? I'm still seeming to be lost
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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>
";
} 
}
?>
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post 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]
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

reason i ask is becuase it did not work.
Post Reply