PHP load up a chart + REP
#8

A few things to note:

1. MySQL is deprecated in PHP 5 and completely abandoned in PHP 7 (in which it throws Fatal Errors at you). As specified above use MySQLi if you want something simple and easy to learn if you already know MySQL_*, or use PDO if you want something a little bit more advanced with support for Object-Oriented Programming.

2. Use classes for easier access accross pages and make one config.php file which you require_once on every front-end page. In this config, configure an autoloader which automatically loads the required classes when needed. In this config you can also store database credentials and such, making all other files a lot cleaner.

3. I would do something like this:
config.php
PHP код:
<?php
    
// Database credentials
    
define("DB_HOST""localhost");
    
define("DB_USER""username");
    
define("DB_PASS""password");
    
define("DB_DATABASE""database_name");
    
// Connecting to database
    
$dsn "mysql:dbname=" DB_DATABASE ";host=" DB_HOST;
    try {
        
$db_con = new PDO($dsnDB_USERDB_PASS);
    } catch (
PDOException $e) {
        echo 
"Connection failed: " $e->getMessage();
    }
    
// Configuring autoloader
    // Example: if a new instance of User is created, it automatically requires /assets/classes/User.class.php
    // If your classes are in a different folder, change the include line
    
spl_autoload_register(function($class) {
        include 
"assets/classes/" $class ".class.php";
    });
    
$user = new User($db_con);
?>
User.class.php
PHP код:
<?php
    
class User {
        private 
$db;
        
/**
         * Creating a new user instance
         * @param PDO $db_con PDO database connection
         */
        
function __construct($db_con) {
            
$this->db $db_con;
        }
        
/**
         * Gets the amount of bans
         * @return int Amount of bans
         */
        
public function getBannedCount() {
            
$stmt $this->db->prepare("SELECT COUNT(*) FROM bans");
            
$stmt->execute();
            return (int) 
$stmt->fetchColumn();
        }
        
/**
         * Gets all the bans
         * @return array Array with all bans, contains TimeData, PlayerName, By and Reason
         */
        
public function getBannedList() {
            
$stmt $this->db->prepare("SELECT TimeDate, PlayerName, By, Reason FROM bans");
            
$stmt->execute();
            return 
$stmt->fetchAll(PDO::FETCH_ASSOC);
        }
    }
?>
banlist.php
PHP код:
<?php
    
require_once("assets/config.php");
    
$amount $user->getBannedCount();
    
$bans $user->getBannedList();
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        
        <title>Ban list</title>
    </head>
    <body>
        <p>There are currently <?= $amount?> players banned.</p>
        <table>
            <thead>
                <tr>
                    <th>Time and date of ban</th>
                    <th>Playername</th>
                    <th>Admin name</th>
                    <th>Reason</th>
                </tr>
            </thead>
            <tbody>
                <?php
                    
foreach($bans as $ban) {
                        echo 
"<tr>";
                            echo 
"<td>" $ban["TimeDate"] . "</td>";
                            echo 
"<td>" $ban["PlayerName"] . "</td>";
                            echo 
"<td>" $ban["By"] . "</td>";
                            echo 
"<td>" $ban["Reason"] . "</td>";
                        echo 
"</tr>";
                    }
                
?>
            </tbody>
        </table>
    </body>
</html>
With such a folder structure:
Root
|-- assets
| |-- classes
| | `-- User.class.php
| `-- config.php
`-- banlist.php
Reply


Messages In This Thread
Test - by Amit1998 - 23.05.2016, 21:30
Re: PHP load up a chart + REP - by ilijap - 23.05.2016, 21:47
Re: PHP load up a chart + REP - by Amit1998 - 23.05.2016, 21:52
Re: PHP load up a chart + REP - by ilijap - 23.05.2016, 21:58
Re: PHP load up a chart + REP - by Amit1998 - 23.05.2016, 22:27
Re: PHP load up a chart + REP - by ilijap - 24.05.2016, 00:00
Re: PHP load up a chart + REP - by Vince - 24.05.2016, 05:28
Re: PHP load up a chart + REP - by JasperM - 24.05.2016, 08:31
Re: PHP load up a chart + REP - by Amit1998 - 24.05.2016, 09:10
Re: PHP load up a chart + REP - by JasperM - 24.05.2016, 10:08

Forum Jump:


Users browsing this thread: 2 Guest(s)