Vivid Lust Posted October 29, 2008 Share Posted October 29, 2008 Hey! I got some help here: http://www.phpfreaks.com/forums/index.php/topic,223290.0.html And here's my loop: for($i = 1; $i <= $checks; ++$i){ if ($site[$i] != ""){ $foo[$i] = $site[$i]; } } The problem is, is that if $site[1] is equal to "", then $foo[1] becomes "" ... I dont wont this to happen, I want $foo[1] to store the first $site[] which has a value which isnt "". Thanks for all the help guys. Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/ Share on other sites More sharing options...
Vivid Lust Posted October 29, 2008 Author Share Posted October 29, 2008 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677470 Share on other sites More sharing options...
Maq Posted October 29, 2008 Share Posted October 29, 2008 if $site[1] is equal to "", then $foo[1] becomes "" ... I dont wont this to happen, It doesn't happen. Look at your code. You only assign $foo[$i] = $site[$i] when $site[$1] IS NOT EQUAL to "". if ($site[$i] != ""){ $foo[$i] = $site[$i]; } Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677473 Share on other sites More sharing options...
rhodesa Posted October 29, 2008 Share Posted October 29, 2008 for($i = 1; $i <= $checks; ++$i){ if (!empty($site[$i])){ $foo[1] = $site[$i]; break; } } Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677474 Share on other sites More sharing options...
Vivid Lust Posted October 29, 2008 Author Share Posted October 29, 2008 that just stores the first value in the $foo[1] ... what about the others? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677490 Share on other sites More sharing options...
rhodesa Posted October 29, 2008 Share Posted October 29, 2008 I want $foo[1] to store the first $site[] which has a value which isnt "". sounds like you need to explain what you want a little better then.... ...maybe you meant this: for($i = 1; $i <= $checks; ++$i){ if (!empty($site[$i])){ $foo[$i] = $site[$i]; } } Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677494 Share on other sites More sharing options...
Vivid Lust Posted October 29, 2008 Author Share Posted October 29, 2008 Still doesnt work, and sorry Code(part of): <?php for($i = 1; $i <= $checks; ++$i){ if (!empty($site[$i])){ $foo[$i] = $site[$i]; } } // set some variables $user_pass = $_SESSION['user']; $user_email = $_SESSION['user2']; $sql ="UPDATE users SET bg=\"$back\",box1=\"$foo[1]\",box2=\"$foo[2]\",box3=\"$foo[3]\",box4=\"$foo[4]\",box5=\"$foo[5]\",box6=\"$foo[6]\",box7=\"$foo[7]\",box8=\"$foo[8]\",box9=\"$foo[9]\",box10=\"$foo[10]\",box11=\"$foo[11]\",box12=\"$foo[12]\",box13=\"$foo[13]\",box14=\"$foo[14]\",box15=\"$foo[15]\" WHERE email=\"$user_email\" AND pass=( \"$user_pass\" )"; $query=mysql_query($sql, $link) or die(mysql_error()); ?> If i have one box checked (please referr to post linked in first post), it is stored in the table column in relation to when it has been counted. Help anyone? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677504 Share on other sites More sharing options...
bobbinsbro Posted October 29, 2008 Share Posted October 29, 2008 ok, after doing a little testing, it appears that checkboxes that are unchecked are not added to the $_POST array at all. so, if you assign a $_POST[] that doesn't exist (of a checkbox that wasn't checked) to $site[$i], then $site[$i] would contain NULL (and you should probably get a warning). so maybe using is_null() instead of !empty() will work. Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677538 Share on other sites More sharing options...
Vivid Lust Posted October 29, 2008 Author Share Posted October 29, 2008 just tried that, same result as last thanks for the suggestion though Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677554 Share on other sites More sharing options...
Vivid Lust Posted October 29, 2008 Author Share Posted October 29, 2008 Ideas Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677661 Share on other sites More sharing options...
bobbinsbro Posted October 29, 2008 Share Posted October 29, 2008 could you please var_dump($site, $foo) at the end of the code? Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677670 Share on other sites More sharing options...
Vivid Lust Posted October 29, 2008 Author Share Posted October 29, 2008 My loop code: <?php for($i = 1; $i <= $checks; ++$i){ if (!isset($_POST['$i'])){ $foo[$i] = $site[$i]; } } ?> When one box is checked this displays: array(15) { [1]=> NULL [2]=> string(18) "http://www.bbc.com" [3]=> NULL [4]=> NULL [5]=> NULL [6]=> NULL [7]=> NULL [8]=> NULL [9]=> NULL [10]=> NULL [11]=> NULL [12]=> NULL [13]=> NULL [14]=> NULL [15]=> NULL } array(15) { [1]=> NULL [2]=> string(18) "http://www.bbc.com" [3]=> NULL [4]=> NULL [5]=> NULL [6]=> NULL [7]=> NULL [8]=> NULL [9]=> NULL [10]=> NULL [11]=> NULL [12]=> NULL [13]=> NULL [14]=> NULL [15]=> NULL } Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677682 Share on other sites More sharing options...
rhodesa Posted October 29, 2008 Share Posted October 29, 2008 <?php for($i = 1; $i <= $checks; ++$i){ if (!empty($_POST[$i])){ $foo[$i] = $site[$i]; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677697 Share on other sites More sharing options...
bobbinsbro Posted October 29, 2008 Share Posted October 29, 2008 try this: $site2 = array_unique($site); $loopCount = count($site2); for($i = 1; $i <= $loopCount; ++$i){ if ($site2[$i] != NULL){ $foo[$i] = $site2[$i]; } } this should at least get rid of a while lot of null elements... Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677701 Share on other sites More sharing options...
Vivid Lust Posted October 29, 2008 Author Share Posted October 29, 2008 Hi, still not working, still inserts a blank value before the actuall value Thanks for the work though guys Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677709 Share on other sites More sharing options...
sasa Posted October 29, 2008 Share Posted October 29, 2008 try <?php $foo = $site; for($i = 1; $i <= $checks; $i++) if (!$site[$i]) unset($foo[$i]); $foo = array_values($foo); print_r($foo); ?> or look my last post Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677710 Share on other sites More sharing options...
Vivid Lust Posted October 29, 2008 Author Share Posted October 29, 2008 Works, thanks loads! Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677723 Share on other sites More sharing options...
Vivid Lust Posted October 29, 2008 Author Share Posted October 29, 2008 The only problem now, is that when having 20 checkboxes, it only works for the first 15! I've add all the relevant stuff Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677822 Share on other sites More sharing options...
sasa Posted October 29, 2008 Share Posted October 29, 2008 are you change value of variable $checks to 20? Quote Link to comment https://forums.phpfreaks.com/topic/130575-loop-help/#findComment-677847 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.