(1) On server side I have a piece of PHP code that takes data from DB and converts them into a sort of Javascript array/matrix, let's just simply call it the PHP_OUTPUT

(2) On client side I have a piece of Javascript code that takes the PHP_OUTPUT and renders it as an HTML table.

Unfortunately Google does not read Javascript, so if I want Google to see the contents of the HTML table, I must write down in page the real HTML of the table. So now I need a NEW piece of PHP code to create by itself the SAME HTML table created by the Javascript code.

One way is obviously to rewrite in PHP all the Javascript code that renders the HTML table. Code rewriting is a sport that I don't like much.

Another way (I don't know if possible) is in some way to:

a) have (1) to pass the PHP_OUTPUT to (2),

b) then (2) creates the HTML table and sends (in some way) back the HTML source of the table to server,

c) finally the NEW piece of PHP code on server just writes down the source with a simple echo.

It's convoluted, but this would reduce the amount of code to be written to almost zero.

But is there a simple way to do this kind of stuff?


Hope you understand what I wrote, plz comments to ask for explanations.

Thanks for any answer.

share|improve this question

1  
Come on. Writing that php generated tables should be 10 min work MAX. Probably less than time you took asking this – Zlatev Nov 18 '10 at 19:02
@Zlatev: if you want I give you the code to write. Just kidding. I did not explain everything on the question because I did not want it to become too confused. Writing a PHP to dump a simple table is not an issue, the problem is that the HTML TABLE rendered by Javacript is very complicated, contains classes on each column to distinguish rendering between strings/int/dates etc, moreover the PHP function interface takes input parameter in a special format that I would like to preserve: it lets you change headers and define column types. – Marco Demaio Nov 18 '10 at 19:15
This isn't super helpful since it's all alpha-level code, but this is pretty neat developer.yahoo.com/yui/theater/video.php?v=glass-node – Peter Bailey Nov 18 '10 at 20:31
feedback

2 Answers

up vote 1 down vote accepted

Could you not simply have the PHP also dump the DB data into a hidden div of some sort? If it is simply for indexing purposes, it does not necessarily have to be in a human-readable table format, right?

share|improve this answer
NO, page size will increase almost double – ajreal Nov 18 '10 at 19:05
Beside what ajreal said, I think Google sees that the dumped data are inside a "display: none" DIV and ignores them. – Marco Demaio Nov 18 '10 at 19:09
May be true, but you can always hide that DIV using javascript – Zlatev Nov 18 '10 at 19:14
@ajreal: Well, to not double the size, simply dump the data into some un-formatted div, and have the Javascript format that data. Of course, why he wouldn't just format it from the start using PHP, I don't know. – Jeff B Nov 18 '10 at 19:17
I came up with a solution, I convert PHP_OUTPUT that is in Json Javascript into a Json obejct by using PHP json_decode, then I create the HTML tabel simply running a couple of ofr each over the Json object. I accept your answer because so far it was the best suggestion I received. Thanks. – Marco Demaio Nov 18 '10 at 21:24
feedback

If you are able to create the HTML on the server side, then why do you need JavaScript at all? I would say the simplest way, would be to use your PHP code that creates the HTML that you need and get rid of the JavaScript all together.

share|improve this answer
Javascript dynamically rendered table ar very useful, they can sort rows very fast on client side without recurring each time to a server call. Anyway this is not the point of the question. Now on the server the PHP does NOT create the HTML, but a piece of Javascript code to be interpreted by Javascript on client side. – Marco Demaio Nov 18 '10 at 19:17
@Marco Demaio you can render an HTML table in PHP on the server and still sort it in JavaScript without needing to return to the server. It sounds like you're saying that you've got PHP that generates JavaScript that Generates HTML... why not cut out the middle man? – Matthew J Morrison Nov 18 '10 at 20:47
thanks for your reply. It would be nice if you could explain me more. I know I could do waht you say, but how would you then sort the table using JS? You would need a JS object to go 1st through all the table rows/cells, store in memory all the data found in cells, and then sort these data in memory, and finally render the table out in HTML. My PHP code creates already this type of JS object with all the rows/cells already in its memory, in this way this JS object does not need to go 1st through the table to store in its memory all the rows/cells data. – Marco Demaio Nov 19 '10 at 11:50
@Marco Demaio: check this out tablesorter.com, If I were tasked with your project, I would try this rather than trying to roll my own solution. – Matthew J Morrison Nov 19 '10 at 13:39
I already have a JS object that sort the table I'm not rolling my own solution! The JS object that I use wants table data to be already in JS form (array or arrays) and not in HTML as tablesorter. tablesorter is more general purpose, but with mid/big tables will take more time to load becasue it has always to convert HTML table in an internal set of data (I looked up in the code, I think it uses the function buildCache for this). Anyway thanks for the hint, it's always nice to see different code solutions. – Marco Demaio Nov 19 '10 at 15:27
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.