dannybrazil Posted September 14, 2009 Share Posted September 14, 2009 Hello In my DB i have 5 upload col' (upload_1.....upload_5) if a photo was uploaded it will add the path as string if NOT in will stay NULL i want to tell the user "how many photos he uploaded up to now" i wrote this code but it seems NOT to work : $upload_1=$row['upload_1']; $upload_2=$row['upload_2']; $upload_3=$row['upload_3']; $upload_4=$row['upload_4']; $upload_5=$row['upload_5']; if($upload_1=="" && $upload_2=="" && $upload_3=="" && $upload_4=="" && $upload_5=="") { $upload='you have uploaded 0 / 5 photos'; } elseif(!$upload_1=="" && $upload_2=="" && $upload_3=="" && $upload_4=="" && $upload_5=="") { $upload='you have uploaded 1 / 5 photos'; } elseif(!$upload_1=="" && !$upload_2=="" && $upload_3=="" && $upload_4=="" && $upload_5=="") { $upload='you have uploaded 2 / 5 photos'; } elseif(!$upload_1=="" && !$upload_2=="" && !$upload_3=="" && $upload_4=="" && $upload_5=="") { $upload='you have uploaded 3 / 5 photos'; } elseif(!$upload_1=="" && !$upload_2=="" && !$upload_3=="" && !$upload_4=="" && $upload_5=="") { $upload='you have uploaded 4 / 5 photos'; } elseif(!$upload_1=="" && !$upload_2=="" && !$upload_3=="" && !$upload_4=="" && !$upload_5=="") { $upload='you have uploaded 5 / 5 photos'; } any help ? Thnaks Quote Link to comment https://forums.phpfreaks.com/topic/174196-solved-php-help/ Share on other sites More sharing options...
Adam Posted September 14, 2009 Share Posted September 14, 2009 $upload_count = 0; for ($i = 1; $i <= 5; $i++) { if (!empty($row['upload_' . $i])) { $upload_count++; } } echo 'you have uploaded ' . $upload_count . ' / 5 photos'; Quote Link to comment https://forums.phpfreaks.com/topic/174196-solved-php-help/#findComment-918301 Share on other sites More sharing options...
MatthewJ Posted September 14, 2009 Share Posted September 14, 2009 Was going to suggest something along the same lines as MrAdam... though your if/else code was a bit ugly, if you hardcode in a var for the first two, it worked so my guess would be that there is something wrong in code you didn't post. Quote Link to comment https://forums.phpfreaks.com/topic/174196-solved-php-help/#findComment-918303 Share on other sites More sharing options...
Zane Posted September 14, 2009 Share Posted September 14, 2009 Well I hate to ask how it isn't working because frankly, that is a horrible way of going about what you are trying to accomplish. I assume this is the code... after you have queried the database...am I correct? if so..then I would do this...mind you I just wrote this up..without testing. ------------------------------------------------------------- I would keep the 5 columns like you have...ALSO I would make sure that those columns are set to NULL by default. That way when you go to check them you can just check and see if they are NULL instead of if they are empty now the code $uploads = array(); $uploads[] = $row['upload_1']; $uploads[] = $row['upload_2']; $uploads[] = $row['upload_3']; $uploads[] = $row['upload_4']; $uploads[] = $row['upload_5']; $uploadCount = 0; foreach($uploads as $upload) if(!is_null($upload)) $uploadCount++; echo "You have uploaded " . $uploadCount . " / ", count($uploads), " photos"; again...may or may not work...and may not be the most accurate way ...but it's definitely better than what you are doing now...no offense because..the way you are doing it......let's say for instance...upload_3 had something and nothing else...figure that one out. Quote Link to comment https://forums.phpfreaks.com/topic/174196-solved-php-help/#findComment-918304 Share on other sites More sharing options...
dannybrazil Posted September 14, 2009 Author Share Posted September 14, 2009 Hey guys Thanks a lot for the quick help. First, i was never good at "for" loops , but i will dedicate my day to learn it i used this example : $upload_count = 0; for ($i = 1; $i <= 5; $i++) { if (!empty($row['upload_' . $i])) { $upload_count++; } } echo 'you have uploaded ' . $upload_count . ' / 5 photos'; and it work fine!! thnks again code tags CODE tags code tags -glad everything works now Quote Link to comment https://forums.phpfreaks.com/topic/174196-solved-php-help/#findComment-918309 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.