Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Creating Dynamic Tables In Html Using Php

HTML html5 Dynamic Tables

  • Please log in to reply
10 replies to this topic

#1 Fenrir190

Fenrir190

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Programming Language:C, Java, C++
  • Learning:PHP, Python, JavaScript, Perl, Assembly, Bash

Posted 13 July 2012 - 01:23 PM

This is page.html
<html><body>
<table border ="1">
<?php
include 'Display.class.php';

$obj = new Display();
$obj -> display(3,3);
?>
</table>
</body></html>

this is Display.class.php
<?php
class Display
{
  public function display($rows, $cols)
  {
   for($c = 0; $c < $cols; $c++)
   {
    echo '<td>';
    for($r = 0; $r < $rows; $r++)
    {
	 echo '<tr> blank </tr>';
    } //ends rows loop
    echo '</td>';
   } //ends columns loop
  } //ends function display
} //ends class Display
?>

When I run the html file in chrome from the /var/www/html folder and from a regular folder off the server the html code printed in php isn't converted to html. Am I missing something?
  • 0

#2 Fenrir190

Fenrir190

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Programming Language:C, Java, C++
  • Learning:PHP, Python, JavaScript, Perl, Assembly, Bash

Posted 13 July 2012 - 02:28 PM

I've done a bit more research into the matter and it seems as though php is just not being executed at all. I used some other simple php code snipet and I got nothing when I ran the html file.
<html>
<head></head>
<body class="page_bg">
Hello, today is <?php echo date('l, F jS, Y'); ?>.
</body>
</html>

  • 0

#3 macallik

macallik

    CC Newcomer

  • Validating
  • PipPip
  • 10 posts
  • Location:CT
  • Programming Language:C, Java, C++, Objective-C, C#, PHP, (Visual) Basic, Python, JavaScript, Visual Basic .NET, VBScript
  • Learning:Python, PL/SQL

Posted 13 July 2012 - 04:15 PM

Rename page.html to page.php and that should work.
  • 1

#4 Fenrir190

Fenrir190

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Programming Language:C, Java, C++
  • Learning:PHP, Python, JavaScript, Perl, Assembly, Bash

Posted 13 July 2012 - 04:21 PM

Rename page.html to page.php and that should work.


I tried that with a different file and got the same thing. The task I'm charged with however is calling the function display from an html file. I read something about the htaccess file being an issue. Currently there isn't one. Could that be it?

*Update*
Upon further review I think I may have read what may have been asked of me. Putting the html code in a php file should still give the same benefits. Also I noticed that i had td and tr in the wrong places. Thanks for the help macallik
  • 1

#5 macallik

macallik

    CC Newcomer

  • Validating
  • PipPip
  • 10 posts
  • Location:CT
  • Programming Language:C, Java, C++, Objective-C, C#, PHP, (Visual) Basic, Python, JavaScript, Visual Basic .NET, VBScript
  • Learning:Python, PL/SQL

Posted 13 July 2012 - 07:22 PM

Your welcome. Is it up and running now?
  • 0

#6 Fenrir190

Fenrir190

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Programming Language:C, Java, C++
  • Learning:PHP, Python, JavaScript, Perl, Assembly, Bash

Posted 15 July 2012 - 01:51 PM

Your welcome. Is it up and running now?


Yeah it is. But now I'm dealing with the issue of submitting data that's input into the cells (I have a text box in each cell for user input) and then transferring them into an array in php. Finally I need to display the data to ensure that it worked. I hate to keep depending on this site for my answers but 3 hours of searching hasn't turned up anything good.
  • 0

#7 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 15 July 2012 - 06:52 PM

so, do you have the not working code at hand ?
  • 0

#8 Fenrir190

Fenrir190

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Programming Language:C, Java, C++
  • Learning:PHP, Python, JavaScript, Perl, Assembly, Bash

Posted 15 July 2012 - 06:58 PM

so, do you have the not working code at hand ?


Sure
New page.php
<html><body>
<h> Enter data into one of the cells below. </h>
<form action="Display.class.php" method="post">
<table border ="1">
<?php
include ("Display.class.php");

$obj = new Display();
$obj -> displayTable(3,3);
echo '<input type= \'submit\' value= \'Do Something\'>';
$obj -> check_values();
?>
</table>
</form>
</body></html>

New Display.class.php
<?php
class Display
{
  private $tbl_cells= array();
 
  public function __construct()
  {} //ends default constructor.
 
  public function displayTable($rows, $cols)
  {
   for($c = 0; $c < $cols; $c++)
   {
    echo '<tr>';
    for($r = 0; $r < $rows; $r++)
    {
	 echo "<td> <input type='text' name='tbl_cells[]' /> </td>";
    } //ends rows loop
    echo '</tr>';
   } //ends columns loop
  } //ends function display
 
  protected function check_values()
  {
   if(isset($_POST['submit']))
   {
    foreach($_POST['tbl_cells'] as $values)
    {
	 echo "printing value... ";
	 echo "The value is $value <br />";
    }
   }
  } //ends function check_values
} //ends class Display
?>

There was no reason why I changed display() to displayTable(). Since my last post I've not been able to display anything from the array. Either no data is there or I'm not displaying it right. Thanks for any help you can provide.

PS thanks macallik for help get the first problem solved.
  • 0

#9 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 15 July 2012 - 08:00 PM

well, i think check_values should go outside the table,separatedly, in another <?php ?>

then, about : Display.class.php
just in case, why don't you rename it to something without 2 dots, like CDisplay.php

And straightly call new page.php again in the form submit, since newpage.php calls Cdisplay.php for drawing


and what about putting some test into the html so at least you could see that an be sure the files is executing properly,
are you entering the site via localhost: or is it hosted ?
  • 0

#10 Fenrir190

Fenrir190

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Programming Language:C, Java, C++
  • Learning:PHP, Python, JavaScript, Perl, Assembly, Bash

Posted 16 July 2012 - 06:04 AM

well, i think check_values should go outside the table,separatedly, in another <?php ?>

then, about : Display.class.php
just in case, why don't you rename it to something without 2 dots, like CDisplay.php

And straightly call new page.php again in the form submit, since newpage.php calls Cdisplay.php for drawing


and what about putting some test into the html so at least you could see that an be sure the files is executing properly,
are you entering the site via localhost: or is it hosted ?

Thanks for that. I'll get on it and post the results.

Also this is being done on my personal computer without aid of a server. It will be put on a server in the future though.
  • 0

#11 Fenrir190

Fenrir190

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Programming Language:C, Java, C++
  • Learning:PHP, Python, JavaScript, Perl, Assembly, Bash

Posted 16 July 2012 - 01:49 PM

I basically started over and got my ideas as to what needed to be done in order. For the most part I got it done.

index.php == page.php
<html>
<body>
<form action="index.php" method="post">
<table border="1" cellspacing="4">
<?php
  include("Utility.php");
  displayTable(2,2);

  if(isset($_POST['submit']))
  {
   foreach($_POST['tbl_cell'] as $cell)
   {
    echo "Processing...<br />";
    echo "Cell contains ".$cell."<br />";
   }
  }

  else
   echo "<h> Enter data into the cells below. </h>";
?>
</table>
<input type='submit' name='submit' value='Process'/>
</form>
</body>
</html>

Utility.php == DisplayClass.php
<?php
$tbl_cell = array();

function displayTable($rows, $cols)
{
  for($c = 0; $c < $cols; $c++)
  {
   echo '<tr>';
   for($r = 0; $r < $rows; $r++)
   {
    $id = (($rows * $c) + $r);

    echo "<td> <input type='text' id='cell".$id."' name='tbl_cell[".$id."]'/> </td> ";
   } //ends rows loop
   echo '</tr>';
  } //ends columns loop
} //ends function display

function displayResults()
{
  echo "<h> Entered displayResults </h>";
  if(empty($tbl_cell))
  {
   echo "<p> No data found! </p>";
  } //ends if

  else
  {
   foreach($tbl_cell as $cell)
   {
    echo "Processing...<br/>";
    echo "<p>Cell contains ".$cell."</p><br />";
   }
  } //ends else
} //ends displayResults()
?>

My only remaining issue is that tbl_cell appears to be empty. I can only see results when I use the $_POST array, which makes sense given post data isn't being sent to my utility file. This could be solved in javascript since page redirects are a simple thing in that language. But php not so much. I'll run this though by the person who wants this code and see if that's something they want to do.
  • 0





Also tagged with one or more of these keywords: HTML, html5, Dynamic Tables

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download