Jump to content

how to order lists correctly in a loop ?


dflow

Recommended Posts

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>';
             	?>

Link to comment
https://forums.phpfreaks.com/topic/249219-how-to-order-lists-correctly-in-a-loop/
Share on other sites

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.