dflow Posted October 16, 2011 Share Posted October 16, 2011 i have a list of check boxes im ordering it according to Amenity name ASC im floating 2 divs (columns)side by side how can i get the list to echo it according to ABC : like this::? A D B E C F <?php echo '<div style="height:800px;" />'; foreach($amens as $amen) { echo '<div style="float:left;padding-left: 2px;width:200px;"><input onclick="changedata(this)" type="checkbox" id="mm'.$amen['Amen_ID'].'" value="'.$amen['Amenity'].'" '.checkAmenity($id, $amen['Amenity']).' /><label style="padding-left:3px;" for="mm'.$amen['Amen_ID'].'">'.$amen['Amenity'].'</label></div>'; } echo '</div>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/249219-how-to-order-lists-correctly-in-a-loop/ Share on other sites More sharing options...
silkfire Posted October 16, 2011 Share Posted October 16, 2011 Do you want 50% of the values to be in the left DIV and 50% in the right DIV? Quote Link to comment https://forums.phpfreaks.com/topic/249219-how-to-order-lists-correctly-in-a-loop/#findComment-1279777 Share on other sites More sharing options...
mikesta707 Posted October 16, 2011 Share Posted October 16, 2011 assuming its already sorted, you can simply halve the count of the array, and have two loops to go through them for each div. like $arr = array(however your array is made and sorted); $midPoint = ceil(count($arr)/2);//ceil to make a int echo "<div id="left"> for ($i = 0; $i < $midPoint; $i++){ echo $arr[$i]; } echo "</div>"; echo "<div id="right"> for ($i = $midpoint; $i < count($arr); $i++){ echo $arr[$i]; } echo "</div>"; obviously you would need to change the output in my example to match what you desire. Quote Link to comment https://forums.phpfreaks.com/topic/249219-how-to-order-lists-correctly-in-a-loop/#findComment-1279778 Share on other sites More sharing options...
dflow Posted October 16, 2011 Author Share Posted October 16, 2011 assuming its already sorted, you can simply halve the count of the array, and have two loops to go through them for each div. like $arr = array(however your array is made and sorted); $midPoint = ceil(count($arr)/2);//ceil to make a int echo "<div id="left"> for ($i = 0; $i < $midPoint; $i++){ echo $arr[$i]; } echo "</div>"; echo "<div id="right"> for ($i = $midpoint; $i < count($arr); $i++){ echo $arr[$i]; } echo "</div>"; obviously you would need to change the output in my example to match what you desire. thanks i'll try it out Quote Link to comment https://forums.phpfreaks.com/topic/249219-how-to-order-lists-correctly-in-a-loop/#findComment-1279828 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.