Wednesday, May 14, 2014

Converting an HTML page into a php page...

I got a list of names and bio data about 40 items long.  I started adding the information to a new page, html page, because that was the simplest way to deal with it.  Create a new html page, add a table tag, massage the data to fit into it.  Simple.

After about 1/2 hr, I remembered that I used to be a pretty good PHP programmer.  This is a task that PHP can handle very easily.

The question really was this.  Where is the best place for this data to live?  On the html page or in a database?  The answer?, almost always in a DB.

Why put the list data in a DB table when I could just put it directly into the page?

  • 1 - More flexible.  I can build the *list in many places, since data is in a DB table
  • 2 - Less code.  I can build a simple loop and use far less html code to output the list.
  • 3 - Sorting options.  I can reorder the list a couple different ways from the DB.
  • 4 - More extensible.  I can add things to it.

The table code in the html page spanned 230 lines. It was at this point that I said "you are a dummy, stop doing this and put the data in a table and convert this htmp page to a php page and use a simple for loop to output the data.

Create the table in the DB

Using MySQL workbench, I created a simple table called teachers, with these columns


Populate the table - I did this in MySQL workbench - edit table mode.




Create a function in my DAO.php file to pull the data from the new table


Connect the html page, which is now a php page to the DAO.php





Call the function from the page and dump the result - for testing purposes.






The result.


































Code to loop the result set from 240 lines to 15, including white space














The final result - output of php page.



No comments:

Post a Comment