Jump to content

Using variables to create and name html tables


msaz87

Recommended Posts

Hi all, I'm new to PHP and was hoping to do the following:

 

I wanted to set up a page with a few different variables. One of the variables would specify the number of identical HTML tables are shown on the page -- and number them sequentially.

 

In other words, it would be something like this:

<?
$number_of_tables = 4;

$table = "<table id="xx">xxx</table";

echo $tables
?>

 

So the $number_of_tables would specify how many of $table are shown and the table's ID would be numbered according to how many there are... IE: if there's 8 tables, it would go <table id="1">, <table id="2"> ... <table id="8">

 

Hopefully that makes sense.. and if anyone can help or point me in the right direction, I'd really appreciate it! Thanks so much!

Without further information, this is the best I can do:

 

<?php
$number_of_tables = 4;

$tables = '';

for ($e = 1; $e <= $number_of_tables; $e++) {
     $tables .= '<table id="' . $e . '"></table>';
}

echo $tables;

 

That's exactly what I need! Thanks!

 

The only additional question I was going to ask is how I can configure variable text for these tables...

 

What I'd like is to have a bunch of variables that are configured to present when the number table ID exists... so:

<?
$1 = "show for table ID 1";
$2 = "show for table ID 2";
?>

 

But I'm not quite sure how to make that happen here:

     $tables .= '<table id="' . $e . '"></table>';

 

I assume it would be echo $variable and tied to the $e -- I just don't know how.

 

Thanks again!

Where do you want the to appear? Inside the table?

 

<?php
$number_of_tables = 4;

$content = array(
     'content for table 1',
     'content for table 2'
);

$tables = '';

for ($e = 1; $e <= $number_of_tables; $e++) {
     $tables .= '<table id="' . $e . '">' . $content[$e] . '</table>';
}

echo $tables;

Where do you want the to appear? Inside the table?

 

<?php
$number_of_tables = 4;

$content = array(
     'content for table 1',
     'content for table 2'
);

$tables = '';

for ($e = 1; $e <= $number_of_tables; $e++) {
     $tables .= '<table id="' . $e . '">' . $content[$e] . '</table>';
}

echo $tables;

 

Yes, I want the content to be in the tables, but it'll be a little more complicated than that, because the table will be a little more complicated -- there will be four parts to each table. So, in other words, it'll be like:

 

<table>
<tr>
<td>
content1a
</td>
<td>
content1b
</td>
</tr>
<tr>
<td>
content1c
</td>
<td>
content1d
</td>
</tr>
</table>

 

Hopefully that makes sense... thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.