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
Bigun
Forum Contributor
Posts: 237 Joined: Tue Jun 13, 2006 10:50 am
Post
by Bigun » Sun Jun 25, 2006 7:35 pm
The code below will not execute...
Code: Select all
for($i=0; $i < 10 && $deletefilelist[$i] != ""; $i++) {
echo "hello";
$filetodelete=$deletefilelist[$i];
$deletesid=mysql_result($songdata,$filetodelete,"sid");
$deletefile=mysql_result($songdata,$filetodelete,"file");
$deletedir="./bands/songs/".$uid."/";
//Delete File
unlink($deletedir."/".$deletefile);
echo $deletedir."/".$deletefile."<br>";
//Remove from Song Database
$query="DELETE FROM `songs` WHERE `sid` = '".$deletesid."' LIMIT 1;";
echo $query;
mysql_query($query);
}
The 'hello' portion of the code doesn't show..
Yet an echo of $deletefilelist[0] right before the loop echo's a zero.
Last edited by
Bigun on Sun Jun 25, 2006 7:54 pm, edited 1 time in total.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Sun Jun 25, 2006 7:54 pm
Do your comparison inside the loop...
Code: Select all
<?php
for($i=0; $i < 10; $i++) {
if (!empty($deletefilelist[$i]))
{
echo "hello";
$filetodelete=$deletefilelist[$i];
$deletesid=mysql_result($songdata,$filetodelete,"sid");
$deletefile=mysql_result($songdata,$filetodelete,"file");
$deletedir="./bands/songs/".$uid."/";
//Delete File
unlink($uploaddir."/".$deletefile);
echo $uploaddir."/".$deletefile."<br>";
//Remove from Song Database
$query="DELETE FROM `songs` WHERE `sid` = '".$deletesid."' LIMIT 1;";
echo $query;
mysql_query($query);
}
}
?>
Last edited by
RobertGonzalez on Sun Jun 25, 2006 7:54 pm, edited 1 time in total.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Jun 25, 2006 7:54 pm
I think the logic surrounding the $deletefilelist comparison would be the problem.
If what you say about echoing $deletefilelist[0] does, in fact, echo a zero, your comparison will short circuit the loop. To check out what I mean,
var_dump($deletefilelist[0] != "")
Bigun
Forum Contributor
Posts: 237 Joined: Tue Jun 13, 2006 10:50 am
Post
by Bigun » Sun Jun 25, 2006 7:56 pm
But why doesn't the '$i' value get inserted into the conditional portion of the loop?
Code: Select all
for($i=0; $i < 10 && $deletefilelist[$i] != ""; $i++) {
Bigun
Forum Contributor
Posts: 237 Joined: Tue Jun 13, 2006 10:50 am
Post
by Bigun » Sun Jun 25, 2006 7:58 pm
feyd wrote: I think the logic surrounding the $deletefilelist comparison would be the problem.
If what you say about echoing $deletefilelist[0] does, in fact, echo a zero, your comparison will short circuit the loop. To check out what I mean,
var_dump($deletefilelist[0] != "")
Bigun
Forum Contributor
Posts: 237 Joined: Tue Jun 13, 2006 10:50 am
Post
by Bigun » Sun Jun 25, 2006 8:01 pm
feyd wrote: I think the logic surrounding the $deletefilelist comparison would be the problem.
If what you say about echoing $deletefilelist[0] does, in fact, echo a zero, your comparison will short circuit the loop. To check out what I mean,
var_dump($deletefilelist[0] != "")
How would you redo the comparison then?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Jun 25, 2006 8:04 pm
I don't know what you're trying to do so I can't tell you what to do.
Bigun
Forum Contributor
Posts: 237 Joined: Tue Jun 13, 2006 10:50 am
Post
by Bigun » Sun Jun 25, 2006 8:10 pm
feyd wrote: I don't know what you're trying to do so I can't tell you what to do.
Simply put there is a difference between:
and
That number correlates to a number on a mysql database that would need to be deleted. But if there is no number, just skip the loop altogether.
It more or less just bugs me that the loop is telling me that 0 is the same as "".
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Jun 25, 2006 8:13 pm
== and != are typeless comparisons.. therefore 0, null, false, and "" are all equals. If you want type strict, it's === and !==.
Bigun
Forum Contributor
Posts: 237 Joined: Tue Jun 13, 2006 10:50 am
Post
by Bigun » Sun Jun 25, 2006 8:21 pm
feyd wrote: == and != are typeless comparisons.. therefore 0, null, false, and "" are all equals. If you want type strict, it's === and !==.
Bingo...
Learned yet another oddity in code.