drisate Posted March 4, 2008 Share Posted March 4, 2008 Hey guys i got a loop working like this 1 2 3 4 5 6 7 8 But i need him to work like this 1 3 5 7 2 4 6 8 How can i turn my function to do it? <?php function list_categ() { if ($_GET[cat_]==""){$_GET[cat_]=0;} $command = mysql_query("Select * FROM categories where parent_cat_id='$_GET[cat_]'") or die (mysql_error()); while ($data = mysql_fetch_array($command)) { $tablo[]=$data; } $nb=count($tablo); if ($nb!="0"){ for($i=0;$i<$nb;$i++){ if ($tablo[$i]['parent_cat_id']=="0"){$nbcol=4;}else{$nbcol=2;} if($i%$nbcol==0) print ('<table border="0" cellpadding="3" cellspacing="3" width="100%"><tr>'); $nombre = $nbthiscat; if ($tablo[$i]['cat_img']!=""){ print ('<td width="20%"> <table border="0" cellpadding="3" cellspacing="3" width="100%"> <tr> <td><center><a href="index.php?mod=6&cat_='.$tablo[$i]['cat_id'].'"><img border="0" src="admin/image/categ/thumbs/'.$tablo[$i]['cat_img'].'"></a></center></td> </tr> <tr> <td><center><a href="index.php?mod=6&cat_='.$tablo[$i]['cat_id'].'">'.$tablo[$i]['cat_name'].'</a></center></td> </tr> </table> </td>'); }else{ print ('<td width="20%"><a href="index.php?mod=6&cat_='.$tablo[$i]['cat_id'].'">'.$tablo[$i]['cat_name'].'</a></td>'); } if($i%$nbcol==($nbcol-1)) print ('</tr>'); } print ('</table>'); } } ?> Link to comment https://forums.phpfreaks.com/topic/94346-vertical-loop/ Share on other sites More sharing options...
discomatt Posted March 4, 2008 Share Posted March 4, 2008 Why not loop through all odds, then loop through all evens? Link to comment https://forums.phpfreaks.com/topic/94346-vertical-loop/#findComment-483170 Share on other sites More sharing options...
drisate Posted March 4, 2008 Author Share Posted March 4, 2008 No because the number of columns can change from the admin panel and the number of rows depens on the number of category to dispaly Link to comment https://forums.phpfreaks.com/topic/94346-vertical-loop/#findComment-483174 Share on other sites More sharing options...
discomatt Posted March 4, 2008 Share Posted March 4, 2008 Well, then you have to use a similar method. Find the number of cols/rows and have x loops, where x is the number of rows, and iterate by y, where y is the number of rows... though my assumption is one has to be variable. Same method, higher thinking. Link to comment https://forums.phpfreaks.com/topic/94346-vertical-loop/#findComment-483180 Share on other sites More sharing options...
drisate Posted March 4, 2008 Author Share Posted March 4, 2008 i know the HTML would look something like this <table border="0" cellpadding="3" cellspacing="3" style="border-collapse: collapse" bordercolor="#111111" width="100%"> <tr> <td width="25%">1</td> <td width="25%">4</td> <td width="25%">7</td> <td width="25%">10</td> </tr> <tr> <td width="25%">2</td> <td width="25%">5</td> <td width="25%">8</td> <td width="25%">11</td> </tr> <tr> <td width="25%">3</td> <td width="25%">6</td> <td width="25%">9</td> <td width="25%">12</td> </tr> </table> But i dont see how to loop it ... Link to comment https://forums.phpfreaks.com/topic/94346-vertical-loop/#findComment-483186 Share on other sites More sharing options...
Barand Posted March 4, 2008 Share Posted March 4, 2008 <?php $data = range(1,12); $numcols = 4; $numrows = ceil (count($data)/$numcols); echo '<table border="1">'; for ($r=0; $r<$numrows; $r++) { echo '<tr>'; for ($c=0; $c<$numcols; $c++) { $cell = isset($data[$r + $c*$numrows]) ? $data[$r + $c*$numrows] : ' '; echo '<td>', $cell, '</td>'; } echo '</tr>'; } echo '</table>'; ?> Link to comment https://forums.phpfreaks.com/topic/94346-vertical-loop/#findComment-483194 Share on other sites More sharing options...
drisate Posted March 4, 2008 Author Share Posted March 4, 2008 thanks thats brilliant! Link to comment https://forums.phpfreaks.com/topic/94346-vertical-loop/#findComment-483201 Share on other sites More sharing options...
drisate Posted March 4, 2008 Author Share Posted March 4, 2008 hmm i probably did it wroung because i am missing the last value <?php function list_categ() { if ($_GET[cat_]==""){$_GET[cat_]=0;} $command = mysql_query("Select * FROM categories where parent_cat_id='$_GET[cat_]'") or die (mysql_error()); while ($data = mysql_fetch_array($command)) { $tablo[]=$data; } if ($_GET[cat_]=="0" or $_GET[cat_]==""){$numcols=4;}else{$numcols=2;} $data = range(1,count($tablo)); $numrows = ceil (count($data)/$numcols); echo '<table width="100%">'; for ($r=0; $r<$numrows; $r++) { echo '<tr>'; for ($c=0; $c<$numcols; $c++) { $cell = isset($data[$r + $c*$numrows]) ? $data[$r + $c*$numrows] : ' '; if ($tablo[$cell]['cat_img']!=""){ print ('<td width="20%"> <table border="0" cellpadding="3" cellspacing="3" width="100%"> <tr> <td><center><a href="index.php?mod=6&cat_='.$tablo[$cell]['cat_id'].'"><img border="0" src="admin/image/categ/thumbs/'.$tablo[$cell]['cat_img'].'"></a></center></td> </tr> <tr> <td><center><a href="index.php?mod=6&cat_='.$tablo[$cell]['cat_id'].'">'.$tablo[$cell]['cat_name'].'</a></center></td> </tr> </table> </td>'); }else{ print ('<td width="20%"><a href="index.php?mod=6&cat_='.$tablo[$cell]['cat_id'].'">'.$tablo[$cell]['cat_name'].'</a></td>'); } } echo '</tr>'; } echo '</table>'; } ?> see http://www.trans-f-air.com/EZA/index.php There should be 3 categorys ... not 2. Link to comment https://forums.phpfreaks.com/topic/94346-vertical-loop/#findComment-483233 Share on other sites More sharing options...
drisate Posted March 4, 2008 Author Share Posted March 4, 2008 maby i should make a new topic... Link to comment https://forums.phpfreaks.com/topic/94346-vertical-loop/#findComment-483251 Share on other sites More sharing options...
Barand Posted March 4, 2008 Share Posted March 4, 2008 try changing $data = range(1,count($tablo)); to $data = range(0,count($tablo) - 1); Link to comment https://forums.phpfreaks.com/topic/94346-vertical-loop/#findComment-483252 Share on other sites More sharing options...
drisate Posted March 4, 2008 Author Share Posted March 4, 2008 yeah that worked :-) Thanks again bro Link to comment https://forums.phpfreaks.com/topic/94346-vertical-loop/#findComment-483256 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.