ballhogjoni Posted October 7, 2008 Share Posted October 7, 2008 I dont know the logic i shoudl use for this. Say I have queried the db and I have 4 results, I use mysql_fetch_array to put the results into an array and then I want to run a foreach to show the results. The tricky question is that I don't want to show the results in a normal table result IE <table> <tr><td>result 1</td></tr> <tr><td>result 2</td></tr> <tr><td>result 3</td></tr> <tr><td>result 4</td></tr> </table> which will put each result into its own row and column. What I want to do is make the table look somthing like this: <table> <tr> <td>result 1</td><td>result 2</td> </tr> <tr> <td>result 3</td><td>result 4</td> </tr> </table> How would I go about doing that? I hope this made sense. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/127416-foreach-loop-question/ Share on other sites More sharing options...
tmallen Posted October 7, 2008 Share Posted October 7, 2008 Easy. Assume an array of 10 elements: <?php // $data = 10-item array $total = count($data) $html = "<table>"; for($i = 1; $i++; $i <= $total) { // Calculate $total ahead of time, otherwise it checks each loop // Also, note that we start at 1 instead of 0 to make things easier, // otherwise the first item is even (0) if($i % 2 = 1) { // If odd $newHtml = "<tr><td>{$data[$i]}</td>"; } else { // it's even $newHtml = "<td>{$data[$i]}</td></tr>"; } $html .= $newHtml; } echo $html; Quote Link to comment https://forums.phpfreaks.com/topic/127416-foreach-loop-question/#findComment-659081 Share on other sites More sharing options...
KevinM1 Posted October 7, 2008 Share Posted October 7, 2008 Easy. Assume an array of 10 elements: <?php // $data = 10-item array $total = count($data) $html = "<table>"; for($i = 0; $i++; $i < $total) { // Calculate $total ahead of time, otherwise it checks each loop if($i % 2 = 1) { // If odd $newHtml = "<tr><td>{$data[$i]}</td>"; } else { // it's even $newHtml = "<td>{$data[$i]}</td></tr>"; } $html .= $newHtml; } echo $html; Your for-loop is backwards. It should be: for($i = 0; $i < $total; $i++) Quote Link to comment https://forums.phpfreaks.com/topic/127416-foreach-loop-question/#findComment-659087 Share on other sites More sharing options...
tmallen Posted October 7, 2008 Share Posted October 7, 2008 Oops, didn't actually run the code. Barring that, it should work fine. Also edited the original because $i should start at one, not zero. Quote Link to comment https://forums.phpfreaks.com/topic/127416-foreach-loop-question/#findComment-659089 Share on other sites More sharing options...
ballhogjoni Posted October 7, 2008 Author Share Posted October 7, 2008 cool thanks! Quote Link to comment https://forums.phpfreaks.com/topic/127416-foreach-loop-question/#findComment-659113 Share on other sites More sharing options...
ballhogjoni Posted October 7, 2008 Author Share Posted October 7, 2008 Ok a new one for ya. What if I want it to be three columns and then a new row: <table> <tr> <td>result 1</td><td>result 2</td><td>result 3</td> </tr> <tr> <td>result 4</td><td>result 5</td><td>result 6</td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/127416-foreach-loop-question/#findComment-659138 Share on other sites More sharing options...
tmallen Posted October 7, 2008 Share Posted October 7, 2008 <?php for($i = 1; $i <= $total; $i++) { // I'm not sure if switch accepts expressions. You may need to assign the switch variable beforehand switch($i % 3) { case 1: break; // = 1/3 case 2: break; // = 2/3 case 0: break; // = 3/3 default: break; } } To do that, you'll need to use this for loop instead. Just fill the cases in with the necessary HTML Quote Link to comment https://forums.phpfreaks.com/topic/127416-foreach-loop-question/#findComment-659149 Share on other sites More sharing options...
tmallen Posted October 7, 2008 Share Posted October 7, 2008 By the way, if you're trying to make a product grid or something similar, you're better off just using floats and adjusting the containing element's width so that only 2, 3, etc. fit per row. That's also the preferred approach because the number per row has very little to do with the nature of the content itself. For that reason, you want to keep this out of your code and use CSS instead. If you still want to enforce a number of elements per row, only do one check ($i % $numberPerRow == 0) and add a class to that item. From your CSS, use "clear: left" and that item will always reset the row. Quote Link to comment https://forums.phpfreaks.com/topic/127416-foreach-loop-question/#findComment-659151 Share on other sites More sharing options...
ballhogjoni Posted October 7, 2008 Author Share Posted October 7, 2008 "make a product grid" is essentially what I am trying to do. I want 3 items per row. thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/127416-foreach-loop-question/#findComment-659232 Share on other sites More sharing options...
ballhogjoni Posted October 7, 2008 Author Share Posted October 7, 2008 Ok i am having major problems here. I can't seem to get this correct. I have 5 items to be placed into a table. Basically the table can have as many rows as needed but there can only be 3 columns in each row. I have 5 items so there should be 1 row with 3 items and 1 row with 2 items. I need to start the $i at 0 because $featured_golf is an array with integer indexes starting at 0. any help would be great! $total = count($featured_golf); for($i=0;$i<$total;$i++) { if($i % 3 == 1){ echo $i % 3;echo '<br>'; $newHtml = '<tr> <td style="padding-bottom:10px;"> <table border="1" cellspacing="0" cellpadding="0" style="font-size:12px;font-family: Arial, Helvetica, sans-serif;padding:8px;"> <tr> <td><a style="color:#000000;font-weight:bold;" href="'.$base_href.$featured_golf[$i]['short_name'].'"><u>'.$featured_golf[$i]['name'].' Golf Club</u></a>:<br /><span style="font-size:11px;line-height:150%;">'.$featured_golf[$i]['featured_description'].' <a href="'.$base_href.$featured_golf[$i]['short_name'].'">Click Here</a></span> </td> <td style="padding-left:10px;"><a href="'.$base_href.$featured_golf[$i]['short_name'].'"><img src="'.$featured_golf[$i]['image']['href'].'" border="0"/></a></td> </tr> </table> </td> <td style="padding-right:10px;"></td>'; }elseif($i % 3 == 2){ $newHtml = '<td style="padding-bottom:10px;"> <table border="1" cellspacing="0" cellpadding="0" style="font-size:12px;font-family: Arial, Helvetica, sans-serif;padding:8px;"> <tr> <td><a style="color:#000000;font-weight:bold;" href="'.$base_href.$featured_golf[$i]['short_name'].'"><u>'.$featured_golf[$i]['name'].' Golf Club</u></a>:<br /><span style="font-size:11px;line-height:150%;">'.$featured_golf[$i]['featured_description'].' <a href="'.$base_href.$featured_golf[$i]['short_name'].'">Click Here</a></span> </td> <td style="padding-left:10px;"><a href="'.$base_href.'Golf-Courses/'.$featured_golf[$i]['short_name'].'"><img src="'.$featured_golf[$i]['image']['href'].'" border="0"/></a></td> </tr> </table> </td> <td style="padding-right:10px;"></td>'; }elseif($i % 3 == 0){ $newHtml = '<td style="padding-bottom:10px;"> <table border="1" cellspacing="0" cellpadding="0" style="font-size:12px;font-family: Arial, Helvetica, sans-serif;padding:8px;"> <tr> <td><a style="color:#000000;font-weight:bold;" href="'.$base_href.$featured_golf[$i]['short_name'].'"><u>'.$featured_golf[$i]['name'].' Golf Club</u></a>:<br /><span style="font-size:11px;line-height:150%;">'.$featured_golf[$i]['featured_description'].' <a href="'.$base_href.$featured_golf[$i]['short_name'].'">Click Here</a></span> </td> <td style="padding-left:10px;"><a href="'.$base_href.$featured_golf[$i]['short_name'].'"><img src="'.$featured_golf[$i]['image']['href'].'" border="0"/></a></td> </tr> </table> </td> </tr>'; } $featured_golf_string .= $newHtml; } Quote Link to comment https://forums.phpfreaks.com/topic/127416-foreach-loop-question/#findComment-659340 Share on other sites More sharing options...
ballhogjoni Posted October 7, 2008 Author Share Posted October 7, 2008 nevermind I figured it out...Finally Quote Link to comment https://forums.phpfreaks.com/topic/127416-foreach-loop-question/#findComment-659352 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.