maliary Posted June 2, 2007 Share Posted June 2, 2007 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>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/ Share on other sites More sharing options...
chigley Posted June 2, 2007 Share Posted June 2, 2007 <?php for ($i = 0; $i <= 4; $i++) { echo $array[$i]; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-266784 Share on other sites More sharing options...
maliary Posted June 2, 2007 Author Share Posted June 2, 2007 Thanks, But this doesn't print out the next line of array elements. It doesn't fix it. Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-266809 Share on other sites More sharing options...
chigley Posted June 2, 2007 Share Posted June 2, 2007 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-266815 Share on other sites More sharing options...
maliary Posted June 2, 2007 Author Share Posted June 2, 2007 Shouldn't the curly braces be closed? Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-266837 Share on other sites More sharing options...
chigley Posted June 2, 2007 Share Posted June 2, 2007 Yeah Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-266848 Share on other sites More sharing options...
maliary Posted June 3, 2007 Author Share Posted June 3, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-267484 Share on other sites More sharing options...
chigley Posted June 3, 2007 Share Posted June 3, 2007 <?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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-267488 Share on other sites More sharing options...
sasa Posted June 4, 2007 Share Posted June 4, 2007 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>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-267577 Share on other sites More sharing options...
maliary Posted June 5, 2007 Author Share Posted June 5, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-268182 Share on other sites More sharing options...
chigley Posted June 5, 2007 Share Posted June 5, 2007 Have you tried mine..? Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-268322 Share on other sites More sharing options...
maliary Posted June 5, 2007 Author Share Posted June 5, 2007 Yes I have. It prints out elements[0] to element [3] but it leaves out the rest. From element [4] to all the other elements ahead. Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-268384 Share on other sites More sharing options...
sasa Posted June 5, 2007 Share Posted June 5, 2007 can you send same value of field 'serial_value' for testing. and does your sql command return one or more then one row? Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-268676 Share on other sites More sharing options...
chocopi Posted June 5, 2007 Share Posted June 5, 2007 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 ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-268679 Share on other sites More sharing options...
maliary Posted June 6, 2007 Author Share Posted June 6, 2007 Sasa, This is a value of $row[serial_field] - &=carand=1-2=3=1&=laptan=4-14=3=2 hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-268999 Share on other sites More sharing options...
maliary Posted June 6, 2007 Author Share Posted June 6, 2007 Chocopi, This is what i get after the replacing the line. carand 1-2 3 1 laptan 4-14 3 2 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 Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-269001 Share on other sites More sharing options...
maliary Posted June 6, 2007 Author Share Posted June 6, 2007 Sasa, Sorry I forgot the last question - One row can be returned from the database. Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-269013 Share on other sites More sharing options...
sasa Posted June 6, 2007 Share Posted June 6, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-269019 Share on other sites More sharing options...
maliary Posted June 26, 2007 Author Share Posted June 26, 2007 Sasa, Thank you very much for this, it really helped. But i can't understand how it works.Could you kindly explain to me how this works - the for loops part only - the logic behind it. Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-282879 Share on other sites More sharing options...
sasa Posted June 26, 2007 Share Posted June 26, 2007 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>' Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-283148 Share on other sites More sharing options...
maliary Posted June 26, 2007 Author Share Posted June 26, 2007 Sasa, thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/53967-solved-loops-and-arrays/#findComment-283163 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.