Jump to content

[Solved] Creating dynamic tables in HTML and PHP


ckuo

Recommended Posts

Hi, I am trying to create dynamic tables that have the same structure and the content changes according to the info in a database. 

I have a loop that repeat the HTML table code and the PHP DB reading function. I am using the foreach loop syntax to break from the HTML code and tested on my local drive it works but on the server it does not show any table. Can you help me check of the syntax is correct on Heliohost? 

I also tested the same code in another server and it worked fine. Not sure where the problem is. 

 

Below the code: 

 

------------------------------------------------------------------------------------------------------

<?php
include_once 'database/Database.php'; //this handles the Database
 
session_start();
$_SESSION['DEBUGGING'] = 'OFF';
 
// Init the DB.
$db = new Database;
$db->connect();
 
$sql = "SELECT stock_code
        FROM (SELECT  MAX(stock_id) as stock_id, stock_div.stock_code, MAX(exdiv_date) as exdiv_date
        FROM stock_div
        GROUP BY stock_code
        ORDER BY `exdiv_date` DESC) abc
 
        ";
 
$stockcode_array = $db->query($sql)->fetch_all();

 

 

foreach ($stockcode_array as $key => $stockcode) : ?>
 
<?php
  $stock_code = $stockcode[0];
 
  $sql = "SELECT *
          FROM stock_div
          WHERE stock_code=$stock_code
          ORDER BY exdiv_date DESC ,stock_id DESC
          ";
 
  $stock_info = $db->query($sql)->fetch_assoc();
 
 
.....some code here....
 
 
?> //End of the PHP dynamic data
 
//HTML table start....
 
<div class="block">
  <div id="<?php echo $stock_code;?>" class="table" onclick="location.href='stock_details.php?stock_code=<?php echo $stock_code;?>';" style="cursor: pointer;">
      <div class="row">
        <label for="stockCode">Stock Code</label>
        <input disabled="true" name="stockCode" value="<?php echo $stock_code;?>"></input>
      </div>
      <div class="row">
        <label for="stockName">Stock Name</label>
        <input disabled="true" name="stockName" value="<?php echo $stock_info['stock_name'];?>"></input>
      </div>
      <div class="row">
        <label for="exDiv">Ex-Div Date</label>
        <input disabled="true" name="exDiv" value="<?php echo $stock_info['exdiv_date'];?>"></input>
      </div>
      <div class="row">
        <label for="yield">Yield</label>
        <input disabled="true" name="yield" class="yield" value="<?php echo sprintf("%.1f%%", $stock_info['stock_yield']);?>"></input>
      </div>
      <div class="row">
        <label for="prob3">P(<3d)</label>
        <input disabled="true" name="prob3" value="<?php echo sprintf("%.0f%%", ($p3/$dataSize)*100);?>"></input>
      </div>
      <div class="row">
        <label for="prob5">P(<5d)</label>
        <input disabled="true" name="prob5" value="<?php echo sprintf("%.0f%%", ($p5/$dataSize)*100);?>"></input>
      </div>
      <div class="row">
        <label for="prob10">P(<10d)</label>
        <input disabled="true" name="prob10" value="<?php echo sprintf("%.0f%%", ($p10/$dataSize)*100);?>"></input>
      </div>
      <div class="row">
        <label for="prob20">P(<20d)</label>
        <input disabled="true" name="prob20" value="<?php echo sprintf("%.0f%%", ($p20/$dataSize)*100);?>"></input>
      </div>
      <div class="row">
        <label for="prob30">P(<30d)</label>
        <input id="prob30" disabled="true" name="prob30" value="<?php echo sprintf("%.0f%%", ($p30/$dataSize)*100);?>"></input>
      </div>
      <div class="row">
        <label for="size">Data Size</label>
        <input disabled="true" name="size" value="<?php echo $dataSize;?>"></input>
      </div>
  </div>
</div>
 
//HTML table end....

 

 
 
 
<?php endforeach; ?>
 
 
------------------------------------------------------------------------------------------------------

 

 

 

Link to comment
Share on other sites

What version of PHP is this running on? Do they match?

 

Tommy and Johnny default to PHP 7.x nowadays. if you're developing on PHP 5.x, you need to change the PHP version in cpanel.

 

Also, are you sure the database settings are correct? That code will not work if the database connection fails. Your code lacks error checking for database errors, so unless it exists inside the Database class, your code would simply die quietly if the connection fails.

Link to comment
Share on other sites

What version of PHP is this running on? Do they match?

 

Tommy and Johnny default to PHP 7.x nowadays. if you're developing on PHP 5.x, you need to change the PHP version in cpanel.

 

Also, are you sure the database settings are correct? That code will not work if the database connection fails. Your code lacks error checking for database errors, so unless it exists inside the Database class, your code would simply die quietly if the connection fails.

I am running on PHP 7.3.6 and I was thinking maybe the DB connection was the problem. I tried testing the Database class in a test.php file and it works fine. I attach the Database.php code below: 

 

<?php

//This class only deals with connecting and running the queries in DB.

class Database {

//Variables

private $db_host;

private $username;

private $password;

private $db_name;

private $mysqli;

private $data;

//Functions

public function __construct(){

$this->db_host = 'localhost';

$this->username = 'ckuo_username';

$this->password = '123456';

$this->db_name = 'ckuo_database';

}

 

//Connect DB

public function connect (){

$this->mysqli = new mysqli($this->db_host, $this->username, $this->password, $this->db_name);

if ($this->mysqli->connect_errno){

echo "Connection error: ". $mysqli->connect_errno;

exit;

}

// echo 'Connection succesful!';

}

 

//Run the query in DB

public function query($sql){

$data = $this->mysqli->query($sql);

if(!$data) {

return $this->mysqli->errno;

} else {

return $data;

}

 

}

 

}

?>

Link to comment
Share on other sites

 

What version of PHP is this running on? Do they match?

 

Tommy and Johnny default to PHP 7.x nowadays. if you're developing on PHP 5.x, you need to change the PHP version in cpanel.

 

Also, are you sure the database settings are correct? That code will not work if the database connection fails. Your code lacks error checking for database errors, so unless it exists inside the Database class, your code would simply die quietly if the connection fails.

I am running on PHP 7.3.6 and I was thinking maybe the DB connection was the problem. I tried testing the Database class in a test.php file and it works fine. I attach the Database.php code below: 

 

<?php

//This class only deals with connecting and running the queries in DB.

class Database {

//Variables

private $db_host;

private $username;

private $password;

private $db_name;

private $mysqli;

private $data;

//Functions

public function __construct(){

$this->db_host = 'localhost';

$this->username = 'ckuo_username';

$this->password = '123456';

$this->db_name = 'ckuo_database';

}

 

//Connect DB

public function connect (){

$this->mysqli = new mysqli($this->db_host, $this->username, $this->password, $this->db_name);

if ($this->mysqli->connect_errno){

echo "Connection error: ". $mysqli->connect_errno;

exit;

}

// echo 'Connection succesful!';

}

 

//Run the query in DB

public function query($sql){

$data = $this->mysqli->query($sql);

if(!$data) {

return $this->mysqli->errno;

} else {

return $data;

}

 

}

 

}

?>

 

 

What version of PHP is this running on? Do they match?

 

Tommy and Johnny default to PHP 7.x nowadays. if you're developing on PHP 5.x, you need to change the PHP version in cpanel.

 

Also, are you sure the database settings are correct? That code will not work if the database connection fails. Your code lacks error checking for database errors, so unless it exists inside the Database class, your code would simply die quietly if the connection fails.

Hi, so I changed the PHP version to 7.3 on cpanel and it works as a charm. Thanks!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...