23.05.2016, 21:30
(
Последний раз редактировалось Amit1998; 03.06.2017 в 12:33.
)
-/-:-
$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();
?>
How is you code different than mine? Couldn't find smth new in yours.
|
$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>";
}
}
?>
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?
|
function GetBannedCount()
{
return $count;
}
<?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($dsn, DB_USER, DB_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);
?>
<?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);
}
}
?>
<?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>