create tables and cells through php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
techjosh
Forum Newbie
Posts: 7
Joined: Mon May 18, 2009 11:55 pm

create tables and cells through php

Post by techjosh »

Hello. I'm a beginner so please keep answers simple. What I want to do is create a table based on information read from a text file off the server. This has zero to do with databases: it's just simple creations of table cells. I've seen people do it in javascript but it's hard to find someone who knows how to do it in php and without going crazy with databases. I just want to know the simple pieces of code to use, or where to look. Thanks in advance for any help.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: create tables and cells through php

Post by susrisha »

creating a table in php is same as creating a table in html
in html

Code: Select all

 <table><tr><!--Table row--><td>Cell 1 data</td><td>Cell2 data</td></tr></table> 
Where as in php you can write it straight away as echo

Code: Select all

 
echo "<table><tr><td>Cell1data</td><td>Cell2data</td></tr></table>";
 
techjosh
Forum Newbie
Posts: 7
Joined: Mon May 18, 2009 11:55 pm

Re: create tables and cells through php

Post by techjosh »

I mean actually creating the table cells based on dynamic data... not an initial setup. So the php web page will read a txt file from the server and might find there's 10 values to put into a table that day, so it creates 10 tables cells. Tomorrow it might have 15.
andycain
Forum Newbie
Posts: 19
Joined: Thu Jul 31, 2008 5:21 pm

Re: create tables and cells through php

Post by andycain »

Do you want multiple columns? How does the data need to display?

To achieve this you need to use logic loops until all the data from the text file has been echoed.
techjosh
Forum Newbie
Posts: 7
Joined: Mon May 18, 2009 11:55 pm

Re: create tables and cells through php

Post by techjosh »

I'm either going to do 2 columns per row or 3 columns per row, then keep adding rows till the end of file is reached. I'll definitely use if/else statements. I just want to know specifically what syntax code to use, like maybe there's some code "createcell()". From there, I'll do my own research on how to use it.

Example for the display:

5 pieces of data:
A B C
D E

10 pieces of data:
A B C
D E F
G H I
J
andycain
Forum Newbie
Posts: 19
Joined: Thu Jul 31, 2008 5:21 pm

Re: create tables and cells through php

Post by andycain »

There's no function like that.

Look up foreach loops...

http://uk2.php.net/manual/en/control-st ... oreach.php

Best thing to do is load all the values from the text file into an array and then use the foreach loop to ittirate throuhg each one.

You simply echo a <td> and </td> tag in the loop so each time it goes throuhg it will create a new cell.

Make sense?
techjosh
Forum Newbie
Posts: 7
Joined: Mon May 18, 2009 11:55 pm

Re: create tables and cells through php

Post by techjosh »

Gotchya, that makes sense.

Thank you both for your help. I'm rdy to start getting this thing going.
Acetylene
Forum Newbie
Posts: 4
Joined: Mon May 18, 2009 12:14 pm

Re: create tables and cells through php

Post by Acetylene »

Check out this PEAR module:

http://pear.php.net/package/HTML_Table/

It's maybe more than you need, but it's worth at least looking at it to see how they did it. It can give you ideas.
Post Reply