Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print Array - Columned HTML 1

Status
Not open for further replies.

Extension

Programmer
Nov 3, 2004
311
CA
Hi,

I want to print an array which contains about 50 elements into an HTML table with 4 columns.

Obviously with my current code, I simply print everything on the same row.

I guess I would have to loop a string to define the record number and etc...

Any help or guidance would be appreciated...


Code:
<table cellpadding="0" cellspacing="0" border"0">
   <tr>
foreach ($Pages as $key=>$Page) {
    print "<td>$Page['Title']</td>";
}
   </tr>
</table>


[code]
$Pages = array(
    
    "50" => array (    
                "Title" => "some text 50",
                "Desc" => "some text",
                "Content" => "some text" ),
    "51" => array (    
                "Title" => "some text 51",
                "Desc" => "some text",
                "Content" => "some text" ),
    "52" => array (    
                "Title" => "some text 52",
                "Desc" => "some text",
                "Content" => "some text" ),
    "53" => array (    
                "Title" => "some text 53",
                "Desc" => "some text",
                "Content" => "some text" ),
                
);

 
Code:
<table cellpadding="0" cellspacing="0" border"0">
<?
foreach ($Pages as $key=>$Page):
    echo <<<EOF
<tr>
 <td>$key</td>
 <td>{$Page['Title']}</td>
 <td>{$Page['Desc']}</td>
 <td>{$Page['Content']}</td>
</tr>

EOF;
endforeach; ?>
</table>
 
jpadie;

I want to display four keys (records) by row instead of one key by row.

Something like this:

Code:
+----------+----------+----------+----------+
|Key1      |Key2      |Key3      |Key4      |
+----------+----------+----------+----------+
|Key5      |Key6      |Key7      |Key8      |
+----------+----------+----------+----------+


Thanks
 
Code:
<table cellpadding="0" cellspacing="0" border"0">
<?
$cnt = 1;
foreach ($Pages as $key=>$Page):
 switch ($cnt):
  case 1:
   echo "<tr><td>{$Page['Title']}</td>";
   $cnt++;
  break;
  case 4:
   echo "<td>{$Page['Title']}</td></tr>";
   $cnt = 1;
   break;
  default:
  echo "<td>{$Page['Title']}</td>";
  $cnt++;
 endswitch;
endforeach; 
?>
</table>
 
use this code instead. you need an additional check on whether the array has finished at an uneven point so that you can properly terminate the row.

Code:
<table cellpadding="0" cellspacing="0" border"0">
<?
$cnt = 1;
$tot_c = 0;
$tot = count($Pages);
foreach ($Pages as $key=>$Page):
 $tot_c++;
 switch ($cnt):
  case 1:
   echo "<tr><td>{$Page['Title']}</td>";
   $cnt++;
  break;
  case 4:
   echo "<td>{$Page['Title']}</td></tr>";
   $cnt = 1;
   break;
  case 2:
  echo "<td>{$Page['Title']}</td>";
  if ($tot_c === $tot) echo "<td>&nbsp;</td><td>&nbsp;</td></tr>";
 break;
  case 3:
  echo "<td>{$Page['Title']}</td>";
  if ($tot_c === $tot) echo "<td>&nbsp;</td></tr>";
  $cnt++;
  break;
 endswitch;
endforeach;
?>
</table>

i'd recommend using css though - cleaner:
Code:
<?
<style tye="text/css">
.cell {width:10em; padding:1px;}
.cells {width: 44em;}
</style>
echo "<div class=\"cells\">";
foreach ($Pages as $key=>$Page):
 echo "<span class=\"cell\">{$Page{'Title']}</span>";
endforeach;
echo "</div>";
 
Thanks again for your help jpadie

It's working even thought I will take some time to fully understand your code. I'm new to some functions.

I agree that the CSS solution is a lot more simple. I will check if it's also cross-browser friendly.
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top