phpretard Posted August 2, 2008 Share Posted August 2, 2008 I have a feild called counties in my DB with the following values: HARDEE, HILLSBOROUGH, JACKSON, JEFFERSON, LAKE, LEE, LEON, LIBERTY, MADISON I just want them to show up with a checkbox for each one. I keep getting 1 EXTRA CHECKBOX Any help today? <? $result = mysql_query("SELECT * FROM members WHERE State='$LicState' ORDER by Counties"); while($row = mysql_fetch_array($result)) { $Counties=$row['Counties']; $County2 = explode(',', $Counties); foreach ($County2 as $County1) { $size = count($County1); for ($i=0; $i<$size; ++$i){ echo "<div style='width:24%; float:left; font-size:13px;'><input type=checkbox name=Counties[] Value='$County1'>$County1</div>"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/117846-solved-extra-checkbox/ Share on other sites More sharing options...
LemonInflux Posted August 2, 2008 Share Posted August 2, 2008 ...for($i=0; $i<$size; $i++) The ++ comes after, not before. ---------------- Now playing: Linkin Park - By_Myslf (Josh Abraham & Mike Shinoda) via FoxyTunes Quote Link to comment https://forums.phpfreaks.com/topic/117846-solved-extra-checkbox/#findComment-606133 Share on other sites More sharing options...
phpretard Posted August 2, 2008 Author Share Posted August 2, 2008 That doesn't solve the problem but thank you for pointing that out Quote Link to comment https://forums.phpfreaks.com/topic/117846-solved-extra-checkbox/#findComment-606138 Share on other sites More sharing options...
LemonInflux Posted August 2, 2008 Share Posted August 2, 2008 No I mean, that should be the error. If you put it before, then you're counting on the wrong side. You should be counting after the loop. Does that not solve it? ---------------- Now playing: Linkin Park - 1stp Klosr (The Humble Brothers ft. Jonathan Davis) via FoxyTunes Quote Link to comment https://forums.phpfreaks.com/topic/117846-solved-extra-checkbox/#findComment-606140 Share on other sites More sharing options...
phpretard Posted August 2, 2008 Author Share Posted August 2, 2008 When I echo $size it shows ten number ones. I only have nine entries. It unfortunately did not solve the problem Quote Link to comment https://forums.phpfreaks.com/topic/117846-solved-extra-checkbox/#findComment-606145 Share on other sites More sharing options...
LemonInflux Posted August 2, 2008 Share Posted August 2, 2008 If your string ends in a comma, then you'll have a blank entry on the end. use print_r to study the array. I have a feeling the last entry might be blank. In which case, just unset() the last value ---------------- Now playing: Linkin Park - 1stp Klosr (The Humble Brothers ft. Jonathan Davis) via FoxyTunes Quote Link to comment https://forums.phpfreaks.com/topic/117846-solved-extra-checkbox/#findComment-606146 Share on other sites More sharing options...
phpretard Posted August 2, 2008 Author Share Posted August 2, 2008 What you see at the top of the page is cut directly from the DB. No comma. The extra checkbox is actually showing up first??? Quote Link to comment https://forums.phpfreaks.com/topic/117846-solved-extra-checkbox/#findComment-606155 Share on other sites More sharing options...
LemonInflux Posted August 2, 2008 Share Posted August 2, 2008 use print_r, and post what's in the variable ---------------- Now playing: Linkin Park & Jay-Z - Jigga What/Faint via FoxyTunes Quote Link to comment https://forums.phpfreaks.com/topic/117846-solved-extra-checkbox/#findComment-606160 Share on other sites More sharing options...
kenrbnsn Posted August 2, 2008 Share Posted August 2, 2008 You have too many loops. Try: <?php $result = mysql_query("SELECT * FROM members WHERE State='$LicState' ORDER by Counties"); while($row = mysql_fetch_array($result)) { $Counties=explode(',',$row['Counties']); foreach ($Counties as $County1) echo "<div style='width:24%; float:left; font-size:13px;'><input type='checkbox' name='Counties[]' Value='$County1'>$County1</div>"; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/117846-solved-extra-checkbox/#findComment-606173 Share on other sites More sharing options...
phpretard Posted August 2, 2008 Author Share Posted August 2, 2008 That is truly fantastic !! you shortened the code...but the print_r Shows 2 Array ( [0] => ) one with NO VALUE (the empty checkbox of course) and then the rest of the array with all of the needed values. How do I get rid of Array ( [0] => )? Quote Link to comment https://forums.phpfreaks.com/topic/117846-solved-extra-checkbox/#findComment-606179 Share on other sites More sharing options...
LemonInflux Posted August 2, 2008 Share Posted August 2, 2008 unset($county1[0]);? ---------------- Now playing: Linkin Park & Jay-Z - Numb/Encore via FoxyTunes Quote Link to comment https://forums.phpfreaks.com/topic/117846-solved-extra-checkbox/#findComment-606202 Share on other sites More sharing options...
phpretard Posted August 2, 2008 Author Share Posted August 2, 2008 TEXT FEILD IN DB EXACTLY: "HARDEE, HILLSBOROUGH, JACKSON, JEFFERSON, LAKE, LEE, LEON, LIBERTY, MADISON" Where is the empty array comming from or better yet how do I get rid of it? I have read into ---$size = intval(count($County1) - 1);--- but am not sure where or how to implement such a beast. print_r yeilds: Array ( [0] => ) Array ( [0] => HARDEE [1] => HILLSBOROUGH [2] => JACKSON [3] => JEFFERSON [4] => LAKE [5] => LEE [6] => LEON [7] => LIBERTY [8] => MADISON ) //END Code Again: $result = mysql_query("SELECT * FROM members WHERE State='$LicState' ORDER by Counties"); while($row = mysql_fetch_array($result)) { $Counties=explode(',',$row['Counties']); foreach ($Counties as $County1) echo "<div style='width:24%; float:left; font-size:13px;'><input type='checkbox' name='Counties[]' Value='$County1'>$County1</div>"; } Quote Link to comment https://forums.phpfreaks.com/topic/117846-solved-extra-checkbox/#findComment-606212 Share on other sites More sharing options...
LemonInflux Posted August 2, 2008 Share Posted August 2, 2008 TEXT FEILD IN DB EXACTLY: "HARDEE, HILLSBOROUGH, JACKSON, JEFFERSON, LAKE, LEE, LEON, LIBERTY, MADISON" Where is the empty array comming from or better yet how do I get rid of it? I have read into ---$size = intval(count($County1) - 1);--- but am not sure where or how to implement such a beast. print_r yeilds: Array ( [0] => ) Array ( [0] => HARDEE [1] => HILLSBOROUGH [2] => JACKSON [3] => JEFFERSON [4] => LAKE [5] => LEE [6] => LEON [7] => LIBERTY [8] => MADISON ) //END Code Again: $result = mysql_query("SELECT * FROM members WHERE State='$LicState' ORDER by Counties"); while($row = mysql_fetch_array($result)) { $Counties=explode(',',$row['Counties']); foreach ($Counties as $County1) echo "<div style='width:24%; float:left; font-size:13px;'><input type='checkbox' name='Counties[]' Value='$County1'>$County1</div>"; } Post the code you're using including the print_r() you're using. ---------------- Now playing: Dance Gavin Dance - It's Safe to Say You Dig the Backseat via FoxyTunes Quote Link to comment https://forums.phpfreaks.com/topic/117846-solved-extra-checkbox/#findComment-606214 Share on other sites More sharing options...
phpretard Posted August 2, 2008 Author Share Posted August 2, 2008 PHP CODE: $result = mysql_query("SELECT * FROM members WHERE State='$LicState' ORDER by Counties"); while($row = mysql_fetch_array($result)) { $Counties=explode(', ' , $row['Counties']); foreach ($Counties as $County1) echo "<div style='width:24%; float:left; font-size:13px;'><input type='checkbox' name='Counties[]' Value='$County1'>$County1</div>"; print_r($Counties); } HTML OUTPUT (MINUS THE STYLE INFO): <div><input type='checkbox' name='Counties[]' Value=''></div> <<<<<<<<<<<<<< HE WONT GO AWAY <div><input type='checkbox' name='Counties[]' Value='HARDEE'>HARDEE</div> <div><input type='checkbox' name='Counties[]' Value='HILLSBOROUGH'>HILLSBOROUGH</div> <div><input type='checkbox' name='Counties[]' Value='JACKSON'>JACKSON</div> <div><input type='checkbox' name='Counties[]' Value='JEFFERSON'>JEFFERSON</div> <div><input type='checkbox' name='Counties[]' Value='LAKE'>LAKE</div> <div><input type='checkbox' name='Counties[]' Value='LEE'>LEE</div> <div><input type='checkbox' name='Counties[]' Value='LEON'>LEON</div> <div><input type='checkbox' name='Counties[]' Value='LIBERTY'>LIBERTY</div> <div><input type='checkbox' name='Counties[]' Value='MADISON'>MADISON</div> Quote Link to comment https://forums.phpfreaks.com/topic/117846-solved-extra-checkbox/#findComment-606220 Share on other sites More sharing options...
phpretard Posted August 2, 2008 Author Share Posted August 2, 2008 I took it out of the while loop and it works great. Thank you for all your help on this one!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/117846-solved-extra-checkbox/#findComment-606243 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.