Horizontal Results from MySQL using PHP

Below is the code I am using to populate a menu with check boxes. I need to adjust the code so that it will display 5 results horizontally then <br> and then the next 5 will display horizontally under the first 5 and so on. The code I am using works perfectly but it displays all results vertically..
/////////////////////////////////////////////////////////////////////////////////////;



  function chk_box_qry($id,$sel,$ncr_id) {
     // create check box list from database;


	//query for options

	$qry= my_query('

			SELECT 
		  		op.id			AS id,
				op.num			AS num,
				op.ois_id		AS ois_id,
				ncr_ref.ncr_id		AS ncr_id
				
				
		FROM	op 	AS op
		LEFT JOIN ncr_ref AS ncr_ref ON ncr_ref.op_id=op.id
		WHERE op.cdr_id = '.$id.'
		ORDER by op.num;
		
		

		');





      //Puts results into a checkbox format, (id as the recorded value);



while($row = mysql_fetch_assoc($qry)) { 

                if ($row['ncr_id'] == $ncr_id) { 
 
                        $ch='checked'; 
 
                } else { 
 
                        $ch=''; 
 
                } 
 
        $chkbox .=  'OP&nbsp;'. $row['num'] . '&nbsp;<input type="hidden" name="ncr_ref:id:' . $row['id'] . '" value="'.$row['ncr_id'].'">
		
		<input type="hidden" name="ncr_ref:op_id:' . $row['id'] . '">
		
		<input type="checkbox" '.$ch.'  name="ncr_ref:op_id:' . $row['id'] . '"  value="' . $row['id'] . '">
		
		<input type="hidden" name="ncr_ref:ncr_id:'.$row['id'].'" value="' . $ncr_id . '"><br>' ; 
		     
        }


	return $chkbox;
  }

Open in new window

brad0525Asked:
Who is Participating?
 
titzu2001Connect With a Mentor Commented:
Hi,
I modified your function and should work.

/////////////////////////////////////////////////////////////////////////////////////;



  function chk_box_qry($id,$sel,$ncr_id) {
     // create check box list from database;


	//query for options

	$qry= my_query('

			SELECT 
		  		op.id			AS id,
				op.num			AS num,
				op.ois_id		AS ois_id,
				ncr_ref.ncr_id		AS ncr_id
				
				
		FROM	op 	AS op
		LEFT JOIN ncr_ref AS ncr_ref ON ncr_ref.op_id=op.id
		WHERE op.cdr_id = '.$id.'
		ORDER by op.num;
		
		

		');





      //Puts results into a checkbox format, (id as the recorded value);


$rowIndex = 0;
	
while($row = mysql_fetch_assoc($qry)) { 

                if ($row['ncr_id'] == $ncr_id) { 
 
                        $ch='checked'; 
 
                } else { 
 
                        $ch=''; 
 
                } 
 
        $chkbox .=  'OP&nbsp;'. $row['num'] . '&nbsp;<input type="hidden" name="ncr_ref:id:' . $row['id'] . '" value="'.$row['ncr_id'].'">
		
		<input type="hidden" name="ncr_ref:op_id:' . $row['id'] . '">
		
		<input type="checkbox" '.$ch.'  name="ncr_ref:op_id:' . $row['id'] . '"  value="' . $row['id'] . '">
		
		<input type="hidden" name="ncr_ref:ncr_id:'.$row['id'].'" value="' . $ncr_id . '">' ; 
        
		$rowIndex++;     
        if ($rowIndex==4) {
        	$chkbox .='<br>';
        	$rowIndex = 0;
        }
        
        
        }
        

		
			
	return $chkbox;
  }

Open in new window

0
 
brad0525Author Commented:
I am displaying the results on another page using the code below..
<tr>
			<td colspan=20 style="text-align:left; height: 150;">
			<div style="overflow:auto; height:100%; text-align:left;">
				'.chk_box_qry($row['cdr_id'],$row['ois_id'], $row['ncr_id'], $row['ncr_ref_id']).'
				</div>
			</td>
		</tr>

Open in new window

0
 
john-formbyCommented:
Hi,

Please have a look at the following simple example showing how to split checkboxes over several lines.

Hope this helps,

John
<?php
$i = 0;
$x = 0;
while($i <= 16) {
	$i++;
	$x++;
	echo '<input type="checkbox" name="cbox[]" />';
	if($x == 5) {
		echo '<br />';
		$x = 0;
	}
}
?>

Open in new window

0
All Courses

From novice to tech pro — start learning today.