Jump to content

Recommended Posts

 

Hi,

 

I need to create a loop that counts upto 5 array elements before printing the next row.

$row['serial_value'];

  is a string made up of & and =  characters and words. The function preg_split removes the characters to leave the array elements which are used in the for loop to print out 5 characters for every row then print another row for the next 5 and so forth.

 

I need help on how to set it to print out only 5 array elements per row! It can only print out all elements in one row creating a crazy line that dosen't make any sense.

 

 

$dbtable = 'table_care'; 
    $depts = $db->Execute("SELECT * FROM $dbtable WHERE batch_nr='92'");                 
    while($row= $depts->FetchRow()){
	           $h = $row['serial_value'];
			      
         $keywords = preg_split ("/[\=,\&]+/", "$h");
	            
{ 
   for ($i=1;$i<count($keywords);$i++)
   {
  $cache.='<td> $keywords[$i]               </td>';  
  }
  }

Link to comment
https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/
Share on other sites

Sorry, completely misread your request.

 

<?php
$dbtable = 'table_care'; 
    $depts = $db->Execute("SELECT * FROM $dbtable WHERE batch_nr='92'");                 
    while($row= $depts->FetchRow()){
	           $h = $row['serial_value'];
			      
         $keywords = preg_split ("/[\=,\&]+/", "$h");
	            	   
  $cache.="<tr>\n <td>\n  {$keywords[0]\n </td>\n <td>\n  {$keywords[1]\n </td>\n <td>\n  {$keywords[2]\n </td>\n <td>\n  {$keywords[3]\n </td>\n <td>\n  {$keywords[0]\n </td>\n</tr>";  

  }

?>

 

Hi, Chigley

 

This solution only prints out the array elements [0] to [3],But leaves out the rest while printing out many n's. It needs to print out all the elements in the array while printing out only 4 per row. So if i have 8 elements then I get 2 rows. 

 

Actually I need to get only four elements to get displayed per row.

 

<?php
$dbtable = 'table_care'; 
    $depts = $db->Execute("SELECT * FROM $dbtable WHERE batch_nr='92'");                 
    while($row= $depts->FetchRow()){
	           $h = $row['serial_value'];
			      
         $keywords = preg_split ("/[\=,\&]+/", "$h");
	            	   
  $cache.="<tr>\n <td>\n  {$keywords[0]}\n </td>\n <td>\n  {$keywords[1]}\n </td>\n <td>\n  {$keywords[2]}\n </td>\n <td>\n  {$keywords[3]}\n </td>\n</tr>\n";  

  }

?>

try

<?php
$dbtable = 'table_care'; 
$depts = $db->Execute("SELECT * FROM $dbtable WHERE batch_nr='92'");                 
while($row= $depts->FetchRow()){
$h = $row['serial_value'];
$keywords = preg_split ("/[\=,\&]+/", "$h");
for ($i = 1; $i < count($keywords); $i += 5) {
	$cache .= '<tr>';
	for ($j = 0; $j < 5; $j++) {
		if ($i + $j < count($keywords)) {
			$cache.='<td> $keywords[$i + $j]               </td>';
		} else $cache .= '<td> </td>';
	}
	$cache .= '</tr>';
}
}
?>

 

Sasa,

 

It gives this as the out put :

carand              1-2              3              1              laptan              4-14              3              2   

 

But I need to output like this:

 

carand              1-2              3              1 

laptan              4-14              3              2   

If you want it to do a new line either use <br /> or close the <tr> and open a new one.

 

So for <br /> replace:

 

$cache.='<td> $keywords[$i + $j]               </td>';

 

with

 

$cache.='<td> $keywords[$i + $j]               <br/></td>';

 

Hope it helps ;D

 

~ Chocopi

 

Chocopi,

 

This is what i get after the replacing the line.

 

carand             

1-2             

3             

1             

laptan             

4-14             

3             

 

It breaks after each element,but it should be after every four elements.

like this.

 

pepto              1-2              3              1

lagran              4-14              3              2

 

 

 

 

 

are you star and end <table> tags

I try

<?php
$cache = '<table border="3">';
//$dbtable = 'table_care'; 
//$depts = $db->Execute("SELECT * FROM $dbtable WHERE batch_nr='92'");                 
//while($row= $depts->FetchRow()){
//$h = $row['serial_value'];
$h = '&=carand=1-2=3=1&=laptan=4-14=3=2';
$keywords = preg_split ("/[\=,\&]+/", "$h");
for ($i = 1; $i < count($keywords); $i += 4) {
	$cache .= '<tr>';
	for ($j = 0; $j < 4; $j++) {
		if ($i + $j < count($keywords)) {
			$cache.='<td> '.$keywords[$i + $j].'               </td>'."\n";
		} else $cache .= '<td> </td>'."\n";
	}
	$cache .= '</tr>';
}
//}
echo $cache, '</table>';
?>

and output is OK

  • 3 weeks later...

1st keywords (keywords[0]) is empty string ('') start for 2nd keyword (indeks = 1) => for($i = 1;

loop all keywords => $i < count($keywords);

i wont 4 in row increment 4 => $i += 4)

star row => $cache .= '<tr>';

i wont in this row keywords with index $i + 0, $i +1, $i +2 and $i +3

change $j for 0 to 3 => for ($j = 0; $j < 4; $j++)

check if this keyword exist if ($i + $j < count($keywords))

if exist echo ceill with keyword in it => $cache.='<td> '.$keywords[$i + $j].'               </td>'."\n";

if not echo empty ceill => } else $cache .= '<td> </td>'."\n"

end of row => $cache .= '</tr>';

when i loop all keywords end table => '</table>'

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.