ckuo Posted February 5, 2020 Posted February 5, 2020 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: ------------------------------------------------------------------------------------------------------<?phpinclude_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; ?> ------------------------------------------------------------------------------------------------------
wolstech Posted February 5, 2020 Posted February 5, 2020 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.
ckuo Posted February 5, 2020 Author Posted February 5, 2020 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 {//Variablesprivate $db_host;private $username;private $password;private $db_name;private $mysqli;private $data;//Functionspublic function __construct(){$this->db_host = 'localhost';$this->username = 'ckuo_username';$this->password = '123456';$this->db_name = 'ckuo_database';} //Connect DBpublic 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 DBpublic function query($sql){$data = $this->mysqli->query($sql);if(!$data) {return $this->mysqli->errno;} else {return $data;} } }?>
ckuo Posted February 7, 2020 Author Posted February 7, 2020 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 {//Variablesprivate $db_host;private $username;private $password;private $db_name;private $mysqli;private $data;//Functionspublic function __construct(){$this->db_host = 'localhost';$this->username = 'ckuo_username';$this->password = '123456';$this->db_name = 'ckuo_database';} //Connect DBpublic 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 DBpublic 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!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now