0

I am new to html and php here. I have set up a game where a php file takes the scores and names and puts into mysql db. The php file also selects the top ten entries in the db table and stores them in an array.

What I simply cannot workout (my research leads me to more and more questions I cant answer) is how to actually get the php array displayed on a separate html page that loads my game. I just need conceptual guidance really - how does the html page request the php array and which file will arrange the ouput as a table?

Edit:

Will the answer in this SO question work for me? If I get my HTML page to include the php file, how can I display the table? Would I use echo in the html page under php tags? Or would I not have access to the php array variable that way?

Displaying Php echo message in an HTML page

Community
  • 1
  • 1
Serge
  • 634
  • 4
  • 9
  • You can't simply display an array as it is. You'll have to display the *items* in the array. To get the items, you can use a loop. Use a loop to get the items from an array (`for`, `foreach`, `while` etc.), and output the values in an HTML table using ``, `` etc. – Amal Murali Mar 06 '14 at 18:25

3 Answers3

0

You need to split your "app" (or game) into a clean Model View Controller style (which sounds alot like what you are talking about)

You have the Model - which is the part that deals with the php data (getting it , formatting it , etc) I think thill will be a standalone php file

You have the controller , which in your cases , gets that data , to be used in the front end latter. This would normally be a JS / Ajax file

And View , your html Game . So What happens is your play your game , you send a JS/Ajax request to get the data you need , and you receive it and display it like nothing happened. Viola :)

  • Interesting. Would this work if i keptthe game page as html? Could i go html page --> js page --> php? – Serge Mar 07 '14 at 03:00
  • Yes , that's exactly the point of it . You no longer need to complicate your pages and have them do more than they should - Keeping it simple and easy is both better for developing / maintaining and for performance – Nadi Hassan Hassan Mar 07 '14 at 15:28
  • So it looks like I have to encode the php array as json and then use ajax to display the elements of the array. Any tips? Not sure how to link the ajax file to html yet either – Serge Mar 07 '14 at 22:40
  • AJAX links the html to the PHP - You can use XMLHTTP Requests as an easy way to do it – Nadi Hassan Hassan Mar 09 '14 at 00:41
0

If you want to use PHP to pull the data, you will need to use a .php page rather than an .html page (the latter can be done, but will create a lot of unnecessary demand on web server resources and introduce potential security issues). In that .php page, you'll need a statement setting an array equal to the results of your MySQL query. Then you can create a loop with echo statements, cycling through those results, generating each row of your table on the fly and inserting each item of the array into its own table cell. No JavaScript is needed for this unless you have some client-side processing to do.

This may be helpful, if you need more specifics: http://www.homeandlearn.co.uk/php/php13p2.html

Edit:

So to address your other question, "Will the answer in this SO question work for me?": Yes, the one by R R on that page will work for you. You can configure your web server to parse .html pages as .php, and then just write the .html page as if it was a .php page. You can use a for loop, as shown in rroberts' answer on this page, to generate each row of the table dynamically.

  • The problem is that i dont think i can change the game page to .php from .html because the game engine makes it .html im considering the avenue of making the html page be able to read php – Serge Mar 07 '14 at 02:59
  • You will certainly take a performance hit by having all .html pages processed as PHP, but the severity will hinge on what portion of your web server resources will be consumed by your game (and multiple instances of your game, if applicable). That being said, it's a relatively simple matter to configure this on Apache or IIS. – Criminally Inane Mar 07 '14 at 03:23
  • The site will literally be 1 html page and 1 php for the time being so i assume it wont be a big issue – Serge Mar 07 '14 at 03:30
  • Fair enough. Just saying it pays to keep scalability in mind for any future expansion. Please see my edit above for more on your second question. Also, please forgive my multiple edits. Just getting acclimated to the (mini-)Markdown and etiquette here. – Criminally Inane Mar 07 '14 at 19:57
0

Hopefully, a simple example will help. Save a .php file, instead of an html file. Content may appear similar to:

<html>
<body>
<table>
<?php
foreach ($array as $value) {
      echo '<tr><td>';
      echo $value;
      echo '</td></tr>';
}
?>
</table>
</body>
</html>

I haven't initiated the array for you, just shown you how to iterate through to display it in a table. Also inside the php tags, you should be able to populate the array from your database - I suggest looking at PDO for that.

http://ca1.php.net/PDO

rroberts
  • 74
  • 3