SA-MP Forums Archive
PHP load up a chart + REP - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: PHP load up a chart + REP (/showthread.php?tid=607785)



Test - Amit1998 - 23.05.2016

-/-:-


Re: PHP load up a chart + REP - ilijap - 23.05.2016

1. Why you dont use mysqli?

2. Try like this:

PHP код:
$sql="SELECT * FROM $tbl_name ORDER BY ID DESC";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
function 
GetBannedCount()
{
return 
$count;
}
function 
GetBannedList()
{
while(
$row $result->fetch_assoc($result)) {
echo 
"<tr><td>" $row["TimeDate"]. "</td><td>" $row["PlayerName"]. " " $row["By"]. "</td><td>" $row["Reason"]. "</td></tr>";
}
}
mysql_close();
?> 



Re: PHP load up a chart + REP - Amit1998 - 23.05.2016

Testing


Re: PHP load up a chart + REP - ilijap - 23.05.2016

Quote:
Originally Posted by Amit1998
Посмотреть сообщение
How is you code different than mine? Couldn't find smth new in yours.
There is a little bit difference just try it its not a spam lol

EDIT: Try like this

PHP код:
$sql="SELECT * FROM $tbl_name ORDER BY ID DESC";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
function 
GetBannedCount()
{
return 
$count;
}
function 
GetBannedList()
{
while(
$row mysql_fetch_assoc($result)) {
echo 
"<tr><td>" $row["TimeDate"]. "</td><td>" $row["PlayerName"]. " " $row["By"]. "</td><td>" $row["Reason"]. "</td></tr>";
}
}
?> 
If it still dont work then remove your line for disableing (ok idk how to spell it :3) errors and warnings and you will see what is the problem


Re: PHP load up a chart + REP - Amit1998 - 23.05.2016

Testingg


Re: PHP load up a chart + REP - ilijap - 24.05.2016

Quote:
Originally Posted by Amit1998
Посмотреть сообщение
Nope. It doesn't work. And which line are you talking about? And btw, I can't find a difference between our codes so, mind telling me what you did there?
In fetch assoc

You must have error_reporting 0 in your php because its "always" giving some warns :P If you not have directly in your php file then maybe in include


Re: PHP load up a chart + REP - Vince - 24.05.2016

If the previous assignment of $count is a global (which should be avoided) then this:
PHP код:
function GetBannedCount()
{
return 
$count;

does absolutely nothing because that local variable $count is not the same as the global variable $count. To reference a global variable in a method the 'global' keyword is needed. Same with the other function where the local $result is not the same as the global $result.


Re: PHP load up a chart + REP - JasperM - 24.05.2016

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


Re: PHP load up a chart + REP - Amit1998 - 24.05.2016

Testing


Re: PHP load up a chart + REP - JasperM - 24.05.2016

Quote:
Originally Posted by Amit1998
Посмотреть сообщение
Wow. Thank you for your effort tho I tried using the exact codelines you used and it doesn't work aswell. >:
Obviously you will need to change the queries and defines, and make sure the folder structure is right.