jesushax Posted March 28, 2008 Share Posted March 28, 2008 hi could someone tell me how i can create a table and when the number of </td> = 3 or the number of records = 3 then write a <tr> to make them go on to the next row. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/98286-records-in-a-table-when-td-3-new-row/ Share on other sites More sharing options...
Cep Posted March 28, 2008 Share Posted March 28, 2008 I would build a string to do this and then concatenate each cell in a loop. Quote Link to comment https://forums.phpfreaks.com/topic/98286-records-in-a-table-when-td-3-new-row/#findComment-502932 Share on other sites More sharing options...
jesushax Posted March 28, 2008 Author Share Posted March 28, 2008 how do i do this? Quote Link to comment https://forums.phpfreaks.com/topic/98286-records-in-a-table-when-td-3-new-row/#findComment-502936 Share on other sites More sharing options...
Cep Posted March 28, 2008 Share Posted March 28, 2008 Its fairly simple, I won't give it all away though <?php $tbl = "<table><tr>"; $mydata = array('A', 'B', 'C'); foreach ($mydata as $data) { $tbl .= "<td>{$data}</td>"; } $tbl .= "</tr></table>"; echo $tbl; ?> You don't have to use an array, this could be a row call to a database and a completly different loop but you get the idea? Quote Link to comment https://forums.phpfreaks.com/topic/98286-records-in-a-table-when-td-3-new-row/#findComment-502940 Share on other sites More sharing options...
ansarka Posted March 28, 2008 Share Posted March 28, 2008 Hope this will help you ;) ;) <?php $data=""; $data .="<tr><td><table><tr>"; for($i=1;$i<=10;$i++) { $data .="<td> $i </td>"; if($i%3 ==0) { $data .="</tr><tr>"; } } $data .="</table></td></tr>"; echo $data; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98286-records-in-a-table-when-td-3-new-row/#findComment-502942 Share on other sites More sharing options...
jesushax Posted March 28, 2008 Author Share Posted March 28, 2008 bah im confused, cant work either of the above out, the second one gives numbers from 1-10 in teh right way but i dont know where to put my records and the first one doesnt count the records just puts them in a td.... im tryign to figure it out here, bt not so much luck lol Quote Link to comment https://forums.phpfreaks.com/topic/98286-records-in-a-table-when-td-3-new-row/#findComment-502949 Share on other sites More sharing options...
ansarka Posted March 28, 2008 Share Posted March 28, 2008 :o :o :o :o ;) ;)Look below code <?php $res="select * from table"; $res=mysql_query($res); $data=""; $data .="<tr><td><table><tr>"; while($row=mysql_fetch_array($res)) { $data .="<td>". $row['id']." </td>"; $data .="<td>". $row['name']." </td>"; $data .="<td>". $row['phone']." </td>"; if($i%3 ==0) { $data .="</tr><tr>"; } } $data .="</table></td></tr>"; echo $data; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98286-records-in-a-table-when-td-3-new-row/#findComment-502952 Share on other sites More sharing options...
jesushax Posted March 28, 2008 Author Share Posted March 28, 2008 i see isee, gettign somewhere now but look the below im trying to get the PortTitle to display 3 times then move down to the next row and prtin it 3 more times but obviously the next record <?php include($_SERVER['DOCUMENT_ROOT'] . '/includes/connection.php'); $res = mysql_query("SELECT * FROM tblPortfolio WHERE PortType='Web' ORDER BY PortDateAdded DESC") or die(mysql_error()); $data=""; $data .="<tr><td><table><tr>"; while($row=mysql_fetch_array($res)) { $data .="<td>". $row['PortTitle']." </td>"; if($i%3 ==0) { $data .="</tr><tr>"; } } $data .="</table></td></tr>"; echo $data; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98286-records-in-a-table-when-td-3-new-row/#findComment-502963 Share on other sites More sharing options...
redarrow Posted March 28, 2008 Share Posted March 28, 2008 Look at this example please cheers. <?php $test1=array("php"=>"title1","mysql"=>"tittle2","html"=>"title3"); foreach($test1 as $key => $val){ $data1=$key; $data2=$val; $data="<table><tr><td>$data1</td><tr>$data2</tr></tr></table><br><br>"; echo $data; } ?> another example showing the table off same info twice. <?php $test1=array("php"=>"title1","mysql"=>"tittle2","html"=>"title3"); foreach($test1 as $key => $val){ $data1=$key; $data2=$val; for($i=0; $i<2; $i++){ $data="<table><tr><td>$data1</td><tr>$data2</tr></tr></table><br><br>"; echo $data; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/98286-records-in-a-table-when-td-3-new-row/#findComment-502974 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.