0

(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.

3
  • 1
    Come on. Writing that php generated tables should be 10 min work MAX. Probably less than time you took asking this Commented Nov 18, 2010 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. Commented Nov 18, 2010 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 Commented Nov 18, 2010 at 20:31

2 Answers 2

3

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.

5
  • 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. Commented Nov 18, 2010 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? Commented Nov 18, 2010 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. Commented Nov 19, 2010 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. Commented Nov 19, 2010 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. Commented Nov 19, 2010 at 15:27
1

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?

5
  • NO, page size will increase almost double Commented Nov 18, 2010 at 19:05
  • Beside what ajreal said, I think Google sees that the dumped data are inside a "display: none" DIV and ignores them. Commented Nov 18, 2010 at 19:09
  • May be true, but you can always hide that DIV using javascript Commented Nov 18, 2010 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. Commented Nov 18, 2010 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. Commented Nov 18, 2010 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.