Jump to content
Sign in to follow this  
Twango

5 Tds Then Tr?

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?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

The counter variable in the original function isn't defined as global, it's always undefined in the function.

Share this post


Link to post
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>';

I have absoloutely no Idea what you mean. can't get it to work. please help. :3

Share this post


Link to post
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;?>

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...