I am trying to display the current users bans. They do display, but it only outputs 1 detail, when I'm trying to fetchAll data for that current user.
Here is my code
function getPlayerBans($username, $conn) {
$getPlayerBans = $conn->prepare("SELECT id, player, bannedby, comment, expires, time FROM `bans` WHERE player = :Player");
$getPlayerBans->bindValue(":Player", $username);
$getPlayerBans->execute();
$rows = $getPlayerBans->fetchAll(\PDO::FETCH_ASSOC);
foreach($rows as $row) {
$id1 = $row['bannedby'];
$id2 = $row['comment'];
$id3 = $row['expires'];
}
if($getPlayerBans->rowCount() == 0)
{
echo('No Results Available');
}else{
echo "<table id=\"gradient-style\" summary=\"Meeting Results\">
<thead>
<tr>
<th scope=\"col\"><b>Banned By</b></th>
<th scope=\"col\"><b>Comments</b></th>
<th scope=\"col\"><b>Expires</b></th>
<th scope=\"col\"><b>Actions</b></th>
</tr>
</thead>
<tbody>
<tr>
<th>$id1</th>
<th>$id2</th>
<th>$id3</th>
<th><img width=\"32px\" height=\"32px\" title=\"Delete\" src=\"/../cp_mod/images/trash_can.png\"></th>
</tr>
</tbody>
</table>";
}
}
You have to put the foreach loop around the code that prints each row of the table.
$rows = $getPlayerBans->fetchAll(\PDO::FETCH_ASSOC);
if (count($rows) == 0) {
echo('No Results Available');
} else {
echo "<table id=\"gradient-style\" summary=\"Meeting Results\">
<thead>
<tr>
<th scope=\"col\"><b>Banned By</b></th>
<th scope=\"col\"><b>Comments</b></th>
<th scope=\"col\"><b>Expires</b></th>
<th scope=\"col\"><b>Actions</b></th>
</tr>
</thead>
<tbody>";
foreach ($rows as $row) {
$id1 = $row['bannedby'];
$id2 = $row['comment'];
$id3 = $row['expires'];
echo "<tr>
<th>$id1</th>
<th>$id2</th>
<th>$id3</th>
<th><img width=\"32px\" height=\"32px\" title=\"Delete\" src=\"/../cp_mod/images/trash_can.png\"></th>
</tr>";
}
echo "</tbody>
</table>";
}
Related
I have no idea why this is not working. Maybe I'm lack of PHP experience but I tried to follow as much as I can from the one which works, but it doesn't work.
What I wish to perform is that getting the ID from the list of page after the user presses the button and pass the variable to the bootstrap modal page. I manage to figure out on displaying the table of the ID.
But when it comes to date filtering, it has failed.
<?php
require_once 'dbconfig.php';
if (isset($_REQUEST['id'],$_POST["From"], $_POST["to"])) {
$result = '';
$id = intval($_REQUEST['id'],$_POST["From"], $_POST["to"]);
$query = "SELECT * FROM history WHERE ID=:id and date BETWEEN '".$_POST["From"]."' AND '".$_POST["to"]."'";
//
$stmt = $DBcon->prepare( $query );
$stmt->execute(array(':id'=>$id));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
$result .='
<table class="table table-bordered">
<tr>
<th width="10%">Order Number</th>
<th width="35%">Customer Number</th>
<th width="40%">Purchased Item</th>
<th width="10%">Purchased Date</th>
</tr>';
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$result .='
<tr>
<td>'.$row["ID"].'</td>
<td>'.$row["location"].'</td>
<td>'.$row["currentStatus"].'</td>
<td>'.$row["date"].'</td>
</tr>';
}
$result .='</table>';
echo $result;
}?>
But it is working this way:
<?php
require_once 'dbconfig.php';
if (isset($_REQUEST['id'])) {
$id = intval($_REQUEST['id']);
$query = "SELECT * FROM history WHERE ID=:id";
$stmt = $DBcon->prepare( $query );
$stmt->execute(array(':id'=>$id));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
}
?>
<div id="purchase_order">
<table class="table table-bordered">
<tr>
<th width="10%">ID</th>
<th width="20%">Location</th>
<th width="20%">Status</th>
<th width="10%">Date</th>
</tr>
<?php
// while($row= mysqli_fetch_array($sql))
// {
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td><?php echo $row["ID"]; ?></td>
<td><?php echo $row["location"]; ?></td>
<td><?php echo $row["currentStatus"]; ?></td>
<td><?php echo $row["date"]; ?></td>
</tr>
<?php
}
?>
I believe the problem is with your use of date in your SQL.
<?php
require_once 'dbconfig.php';
// enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (isset($_REQUEST['id'], $_POST['From'], $_POST['to'])) {
$result = '';
// intval() only has 2 arguments
$id = intval($_REQUEST['id']);
$from = $_POST['From'];
$to = $_POST['to'];
// more likely the problem: date is a reserved word in MySQL -- so wrap it in backquotes
// also, use placeholders for From and To
$query = "SELECT * FROM history WHERE ID = ? AND `date` BETWEEN ? AND ?";
$stmt = $DBcon->prepare($query);
$stmt->execute([$id, $from, $to])); // simplified
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// unnecessary and dangerous
//extract($row);
$result .= '
<table class="table table-bordered">
<tr>
<th width="10%">Order Number</th>
<th width="35%">Customer Number</th>
<th width="40%">Purchased Item</th>
<th width="10%">Purchased Date</th>
</tr>';
// you already fetched the row - no need to do it again
if ($row) {
$result .= '
<tr>
<td>'.$row["ID"].'</td>
<td>'.$row["location"].'</td>
<td>'.$row["currentStatus"].'</td>
<td>'.$row["date"].'</td>
</tr>';
}
$result .= '</table>';
echo $result;
}
?>
Useful links:
PDO and MySQL 'between'
Is “Date” a keyword in mysql?
Converting to boolean
I use simple-php-dom to get some html from other website.
Then I use foreach to find 3 things : business name, address and phone.
Finally I want to print those 3 info in my table using php loop.
Question : How to write the loop inside the table ?
Here's my PHP code to get the 3 info from other site :
<?php
//Get Biz Name
foreach($html->find('a[class=biz-name js-analytics-click]') as $biz_name_root){
foreach ($biz_name_root->find('span') as $biz_name) {
echo $biz_name->plaintext . "<br>";
}
}
//Get Address
foreach($html->find('address') as $address){
echo $address . '<br>';
}
// Get Phone
foreach($html->find('span[class=biz-phone]') as $phone){
echo $phone . '<br>';
}
?>
Here's the table that I want to store those 3 information to :
I want to store the $biz_name->plaintext to <td>biz_name</td>, the $address to
<td>address</td> and $phone to <td>phone</td>field.
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">No</th>
<th scope="col">Biz Name</th>
<th scope="col">Address</th>
<th scope="col">Phone</th>
</tr>
</thead>
<tbody>
<?php
for ($i=1; $i < count($html->find('address'))+2 ; $i++) { ?>
<tr>
<th scope="row"><?php echo $i ?></th>
<td>biz_name</td>
<td>address</td>
<td>phone</td>
</tr>
<?php }
?>
</tbody>
</table>
You should store all found info in arrays to iterate through them:
<?php
//Get Biz Name
$bisNames = array();
foreach($html->find('a[class=biz-name js-analytics-click]') as $biz_name_root){
foreach ($biz_name_root->find('span') as $biz_name) {
$bizNames[] = $biz_name->plaintext;
}
}
//Get Address
$adresses = array();
foreach($html->find('address') as $address){
$adresses[] = $address;
}
// Get Phone
$phones = array();
foreach($html->find('span[class=biz-phone]') as $phone){
$phones[] = $phone;
}
?>
And then:
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">No</th>
<th scope="col">Biz Name</th>
<th scope="col">Address</th>
<th scope="col">Phone</th>
</tr>
</thead>
<tbody>
<?php
for ($i=1; $i < count($html->find('address'))+2 ; $i++) { ?>
<tr>
<th scope="row"><?php echo $i ?></th>
<td><?php echo $bizNames[$i]; ?></td>
<td><?php echo $adresses[$i]; ?></td>
<td><?php echo $phones[$i]; ?></td>
</tr>
<?php }
?>
</tbody>
</table>
I got this errors in the view file (and so many more about the indexes id,username, name, lastname, password, type, status, date):
https://i.gyazo.com/d8bda30b1fafce47ed2125d590c5b4e4.png
I gotta show ONLY the rows that contain "simple" users ("type = 1") which are stored inside the table "usuarios"
Table "usuarios".
"usuarios" includes:(id,username,name,lastname,password,type,status,date).
When i enter the system as an ADMIN the program must show a table with all the "simple" users stored in the table "usuarios".
I need something like this:
https://i.gyazo.com/36f11b368a339964f1b734cea0177734.png
This is my code:
My view file ("user_view"):
<table class="table table-hover" align="center" border="1" cellspacing="0" cellpadding="0" width="700" id="tabla_busqueda">
<thead>
<th>id</th>
<th>User</th>
<th>Name</th>
<th>Lastname</th>
<th>Password</th>
<th>Type</th>
<th>Status</th>
<th>Date</th>
</thead>
<tbody>
<?php
if (count($records) > 0 && $records != false)
{
foreach($records as $record) {
echo "<tr>
<td>".$records['id']."</td>
<td>".$records['username']."</td>
<td>".$records['name']."</td>
<td>".$records['lastname']."</td>
<td>".$records['password']."</td>
<td>".$records['type']."</td>
<td>".$records['status']."</td>
<td>".$records['date']."</td>
</tr>";
}
}
?>
</tbody>
</body>
</html>
My model function:
public function getINFOUSER(){
$query = $this->db->get_where('usuarios',array('type'=>1));
if ($query->num_rows() > 0 ) {
$result = $query->result_array();
}
return $result;
}
Do not know what to do now :S
It is record, not records inside foreach:
foreach($records as $record) {
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['username']."</td>
<td>".$record['name']."</td>
<td>".$record['lastname']."</td>
<td>".$record['password']."</td>
<td>".$record['type']."</td>
<td>".$record['status']."</td>
<td>".$record['date']."</td>
</tr>";
}
Hope this will work.
There is some error in your code. you have written wrong variable name. I just corrected it.
<?php
if (count($records) > 0 && $records != false)
{
foreach($records as $record) {
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['username']."</td>
<td>".$record['name']."</td>
<td>".$record['lastname']."</td>
<td>".$record['password']."</td>
<td>".$record['type']."</td>
<td>".$record['status']."</td>
<td>".$record['date']."</td>
</tr>";
}
}
?>
I am beginner in php...
How to create a bids option from the table:
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Current Location</th>
<th>Country</th>
<th>State</th>
<th>City</th>
<th>Preferred Treatment Location</th>
<th>Country</th>
<th>State</th>
<th>City</th>
<th>Treatment & Surgery</th>
<th>Enter date</th>
<th>Medical Condition of Patient</th>
<th>BIDS</th>
</tr>
<?php
//Create Connection with MySQL Database
$con = mysqli_connect('localhost','root','root123');
//Select Database
if(!mysqli_select_db($con,'medical'))
{
echo "Database Not Selected";
}
//Select Query
$sql = "SELECT * FROM user_detail";
//Execute the SQL query
$records = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($records))
{
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['fname']."</td>";
echo "<td>".$row['lname']."</td>";
echo "<td>".$row['clocation']."</td>";
echo "<td>".$row['country']."</td>";
echo "<td>".$row['state']."</td>";
echo "<td>".$row['city']."</td>";
echo "<td>".$row['treatment']."</td>";
echo "<td>".$row['country1']."</td>";
echo "<td>".$row['state1']."</td>";
echo "<td>".$row['city1']."</td>";
echo "<td>".$row['surgery']."</td>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['medicond']."</td>";
}
?>
</table>
Can you please help...
I am trying to display my table from my MySQL database. It doesn't seem to work, I guess it's most likely something simple. If possible I would like to have the table look like just a normal table with no frills; just a border and to be able to fit in a normal sized page. This is what I have so far:
<html>
<head>
<title>Profile Database </title>
</head>
<body>
<?
$connection = "connect.php";
require $connection;
mysql_select_db("bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ))
{
echo <tr>
<td>{$row\['id'\]}</td>
<td>{$row\['fname'\]}</td>
<td>{$row\['lname'\]}</td>
<td>{$row\['email'\]}</td>
<td>{$row\['dob'\]}</td>
<td>{$row\['age'\]}</td>
<td>{$row\['city'\]}</td>
<td>{$row\['state'\]}</td>
<td>{$row\['zip'\]}</td>
<td>{$row\['offence'\]}</td>
<td>{$row\['notes'\]}</td>
</tr>\n;
}
?>
</tbody>
</table>
<?php
mysql_close($connection);
?>
</body>
</html>
Give this a go:
<table>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
<?php
require "connect.php";
mysql_select_db("bank");
$result = mysql_query("SELECT * FROM profiles");
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo '<tr>';
foreach($row as $column) {
echo '<td>', $column, '</td>';
}
echo '</tr>';
}
?>
</table>
WHILE($row = mysql_fetch_array($result))
should be either
WHILE($row = mysql_fetch_array($result, MYSQL_ASSOC))
OR
WHILE($row = mysql_fetch_assoc($result))
Also the mysql_* functions have been deprecated and relying on them is HIGHLY discouraged.
Kindly check the below discussions :-
Deprecated MySql Functions
How to successfully rewrite old mysql-php code with deprecated mysql_* functions?
http://php.net/manual/en/migration55.deprecated.php
Use either MySQLi or PDO.
<center><table border="1">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
<?
$connection = "connect.php";
require $connection;
mysql_select_db("bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
WHILE($row = mysql_fetch_array($result))
{
$id = $row ['id'];
$firstname = $row ['fname'];
$lastname = $row ['lname'];
$email = $row ['email'];
$dob = $row ['dob'];
$age = $row ['age'];
$city = $row ['city'];
$state = $row ['state'];
$zip = $row ['zip'];
$offence = $row ['offence'];
$nots = $row ['notes'];
echo '<tr>'; // change here
echo '<td>'.$id.'</td>';
echo '<td>'.$firstname.'</td>';
echo '<td>'.$lastname.'</td>';
echo '<td>'.$email.'</td>';
echo '<td>'.$dob.'</td>';
echo '<td>'.$age.'</td>';
echo '<td>'.$city.'</td>';
echo '<td>'.$state.'</td>';
echo '<td>'.$zip.'</td>';
echo '<td>'.$offence.'</td>';
echo '<td>'.$notes.'</td>';
echo '</tr>'; // change here
}
mysql_close();
?>
</table></center>
Try this code:
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";
$dbconnect = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$select=mysql_select_db("bank",$dbconnect) or die("Could not select bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
?>
<center><table border="1">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th></tr>
<?php
while($row = mysql_fetch_array($result))
{
$id = $row ['id'];
$firstname = $row ['fname'];
$lastname = $row ['lname'];
$email = $row ['email'];
$dob = $row ['dob'];
$age = $row ['age'];
$city = $row ['city'];
$state = $row ['state'];
$zip = $row ['zip'];
$offence = $row ['offence'];
$nots = $row ['notes'];
echo '<tr>';
echo '<td>'.$id.'</td>';
echo '<td>'.$firstname.'</td>';
echo '<td>'.$lastname.'</td>';
echo '<td>'.$email.'</td>';
echo '<td>'.$dob.'</td>';
echo '<td>'.$age.'</td>';
echo '<td>'.$city.'</td>';
echo '<td>'.$state.'</td>';
echo '<td>'.$zip.'</td>';
echo '<td>'.$offence.'</td>';
echo '<td>'.$notes.'</td>';
echo '</tr>';
}
?>
</table></center>
<?php
mysql_close();
?>
Corrected Code:
<html>
<head>
<title>Profile Database </title>
</head>
<body>
<?php
$connection = "connect.php";
require $connection;
mysql_select_db("bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ))
{
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['dob'];?></td>
<td><?php echo $row['age'];?></td>
<td><?php echo $row['city'];?></td>
<td><?php echo $row['state'];?></td>
<td><?php echo $row['zip'];?></td>
<td><?php echo $row['offence'];?></td>
<td><?php echo $row['notes'];?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
mysql_close($connection);
?>
</body>
</html>