Jump to content

5 Tds Then Tr?


Twango
 Share

Recommended Posts

Hi, I want to have something that basically generates 5 items in a table, then closes the TR and re-opens.Here's what I have:

$cxnms = 1;function addtd($inc){if ($cxnms == 1){echo "<tr>";$cxnms++;}if ($cxnms > 1 && $cxnms < 5){$cxnms++;}echo "<td><b class='lt'>" . $inc['name'] . "</b><br/><div class='bgi'><img src=" . $inc['tnloc'] . " width='90px' height='90px'/></div><br/><p><a href=game.php?gid=" . $inc['gid'] . ">" . $inc['sdesc'] . "</a></p></td>";if ($cxnms == 5){echo "</tr>";$cxnms = 1;}

It just puts it all on one line.Help?

Link to comment
Share on other sites

this is typically done using the modulos operater (%)

$nums = array("0","1","2","3","4","5","6","7","8","9");$table = '';$breakPoint = 5; $table .= '<table><tr>';for($i = 0, $l = count($nums); $i < $l; $i++){  $table .= '<td>' . $nums[$i] . '</td>';  if($i % $breakPoint === 0){	$table .= '</tr><tr>';  };};$table .= '</tr></table>';

Edited by thescientist
Link to comment
Share on other sites

  • 1 month later...
this is typically done using the modulos operater (%)
$nums = array("0","1","2","3","4","5","6","7","8","9");$table = '';$breakPoint = 5; $table .= '<table><tr>';for($i = 0, $l = count($nums); $i < $l; $i++){  $table .= '<td>' . $nums[$i] . '</td>';  if($i % $breakPoint === 0){	$table .= '</tr><tr>';  };};$table .= '</tr></table>';

I have absoloutely no Idea what you mean. can't get it to work. please help. :3
Link to comment
Share on other sites

I took thescientist's code and rearranged it a bit and added comments.

<?php// this nums array just replaces your $inc, where we don't know,// what you stored in it, so we can't play with it ;-)$nums = array("0","1","2","3","4","5","6","7","8","9");// html is your output// in which the whole table is stored$html = '';// the breakpoint int let's you decide, after how much elements (<td>)// you want a new <tr>$breakPoint = 5;$table .= '<table><tr>'; //start your markup// this is a foreach loop// you'll do as follows: for every entry in $nums (10 in our example) you// execute this wrapped code// $key is the currently processed Key of your array entry// $value is the value stored in $nums[$key]foreach($nums as $key => $value){  $html .= '<td>'.$value.'</td>'; // generate the table data  // for explanations of the modulo % operator take a look at google or something  // like that. Basically it divides and outputs the rest  if(($key+1) % $breakPoint === 0) $html .= '</tr><tr>';};$html .= '</tr></table>'; // end your markup// output your markupecho $html;?>

Link to comment
Share on other sites

I have absoloutely no Idea what you mean. can't get it to work. please help. :3
which parts? it does no good to keep helping you if you don't tell us what the problem is, what you're not getting, etc. did you try to run the code to see what the output was?
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...