Hy!
I want to list all row of my table, but it is not works :(
My function.php:
function getCharacterAdminJails($account_id) {
global $connection;
$stmt = $connection->pdo->prepare("SELECT * FROM adminjails WHERE jailed_accountID = :account_id");
$stmt->bindParam(":account_id", $account_id);
$stmt->execute();
$adminjail_data = $stmt->fetch(PDO::FETCH_ASSOC);
return $adminjail_data;
}
My example.php, where I list my rows:
<table class="table table-hover">
<tr>
<th>Admin neve:</th>
<th>Indok:</th>
<th>Perc:</th>
<th>Időpont:</th>
</tr>
<tr>
<th><?=$adminjail_data["jailed_admin"];?></th>
<th><?=$adminjail_data["jailed_reason"];?></th>
<th><?=$adminjail_data["jailed_ido"];?></th>
<th><?=$adminjail_data["jailed_idopont"];?></th>
</tr>
</table>
How can I list all rows of my table?
I would suggest you change this line of code in your php function from
$adminjail_data = $stmt->fetch(PDO::FETCH_ASSOC);
To this:
$adminjail_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
As fatchAll will return an array of all the results from your query and then in your html page do this:
<table class="table table-hover">
<tr>
<th>Admin neve:</th>
<th>Indok:</th>
<th>Perc:</th>
<th>Időpont:</th>
</tr>
<?php foreach($adminjail_data as $row) :?>
<tr>
<th><?=$row["jailed_admin"];?></th>
<th><?=$row["jailed_reason"];?></th>
<th><?=$row["jailed_ido"];?></th>
<th><?=$row["jailed_idopont"];?></th>
</tr>
<?php endforeach; ?>
</table>
This is from PHP.net documentation about fetchAll:
PDOStatement::fetchAll — Returns an array containing all of the result set rows
You can refer to the full php documentation about PDOs fetchAll here: https://secure.php.net/manual/en/pdostatement.fetchall.php
Try the following code to list all the rows by iterating through the $adminjail_data array:
<table class="table table-hover">
<tr>
<th>Admin neve:</th>
<th>Indok:</th>
<th>Perc:</th>
<th>Időpont:</th>
</tr>
<?php foreach($adminjail_data as $row) :?>
<tr>
<th><?=$row["jailed_admin"];?></th>
<th><?=$row["jailed_reason"];?></th>
<th><?=$row["jailed_ido"];?></th>
<th><?=$row["jailed_idopont"];?></th>
</tr>
<?php endforeach; ?>
</table>
Related
after having found the data in a table the function displays the information without missing, with a td tag only the first word which displays the others after the space are not displayed.
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Service </th>
<th>Prix</th>
<th >Quantite</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$reqq = "SELECT * FROM service WHERE client = '$clientId' AND dossier = '$dossier' ";
$results = $connect->query($reqq);
?>
<?php if($results->num_rows > 0){ ?>
<?php
foreach($results as $data){ ?>
<?php echo $data['nom']; ?>
<tr>
<td> <?php print $data['nom']; ?>
<td><?= $data['prix']?></td>
<td><?= $data['quantite']?></td>
<td>Supprimer</td>
</tr>
</tbody>
</table>
the result is
Inside the table the last field have Franchise Procedure the table only display the first word which is Franchise
Help me find the solution please.
Try with:
<td> <?php echo htmlspecialchars($data['nom']); ?> </td>
Often, text stored in databases can contains reserved characters in HTML like "<" or "&" which should be replaced by html entities
That's what the PHP function htmlspecialchars was invented for.
i have an sql table with three columns which have comma separated values, i am trying to print it inside an html table, my code looks like below:
<table class="table custom-table m-0">
<thead>
<tr>
<th>Description</th>
<th>Make</th>
<th>UOM</th>
</tr>
</thead>
<tbody>
<?php
$one=explode(',', $row['description']);
$two=explode(',', $row['make']);
$three=explode(',', $row['uom']);
foreach($one as $ones) {
?>
<tr>
<td>
<?php echo $ones?>
</td>
<td></td>
<td></td>
</tr>
<?php }?>
</tbody>
</table>
here am only able to get the values of first column, can anyone please tell me how to get values from all the three columns, thanks in advance
Use a counter - assuming exact same number of entries per row
http://sandbox.onlinephpfunctions.com/code/555be47daf3bc3e99d496585f702bfc9dfae4e4e
<?
$one=explode(',', $row['description']);
$two=explode(',', $row['make']);
$three=explode(',', $row['uom']);
$i=0;
?>
<table class="table custom-table m-0">
<thead>
<tr>
<th>Description</th>
<th>Make</th>
<th>UOM</th>
</tr>
</thead>
<tbody>
<?php
foreach($one as $ones) {
?>
<tr>
<td><?php echo $ones; ?></td>
<td><?php echo $two[$i]?></td>
<td><?php echo $three[$i]?></td>
</tr>
<?php $i++;}?>
</tbody>
</table>
I couldn't loop through score record. It keeps replacing the score of new user but does not display the record of previous user.
Here is the code.
users.php
public function show_per($matricnum)
{
$query=$this->conn->query("select * from percentage where matricnum='$matricnum'");
$row=$query->fetch_array(MYSQLI_ASSOC);
if($query->num_rows>0)
{
$this->data[]=$row;
}
return $this->data;
}
viewre.php
<div class="container">
<center><h2>Student Result Record</h2>
<?php
$i=1;
foreach($result->data as $view)
{?>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Matric Number</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $view['id']; ?></td>
<td><?php echo $view['matricnum'];?></td>
<td><?php echo $view['per'];?></td>
</tr>
</tbody>
<?php $i++; }?>
</table>
</div>
Try:
public function show_per($matricnum)
{
$query=$this->conn->query("select * from percentage where matricnum='$matricnum'");
if($query->num_rows>0)
{
while ($row=$query->fetch_array(MYSQLI_ASSOC))
$this->data[]=$row;
}
return $this->data;
}
You call fetch_array() method which returns only one row. You need a loop to go through all rows, then when it takes all rows it will return NULL and the while loop will stop.
And BTW, you can use fetch_assoc() instead fetch_array(MYSQLI_ASSOC).
in my viewv-view-fields--myView.tpl.php
i have styled how i want the output to look like
if(isset($fields['title']) && isset($fields['file'])) {
$category = $fields['field_category']->content;
$file_link = $fields['file']->raw;
$title = $fields['title']->content;
if($category == "2014"){
print '<tr><td>'.$title.' </td> </tr>';
} else if($category == "2013") {
print '<tr><td>'.$title.' </td> </tr>';
}
i can if i want output only one category white my ifs
but now to the trouble if i want to output category 2014 in one table and category 2013 i another.
in my views-view--myView.tpl.php i can only do print rows and it picks both, i tried to put the links into a array but it creates a new array of every link so im kinda stuck here
<table class="table table-hover">
<thead>
<tr><th>2014</th></tr>
</thead>
<tbody>
<?php if ($rows):
print $rows;
?>
<?php endif; ?>
</tbody>
</table>
<table class="table table-hover">
<thead>
<tr><th>2013</th></tr>
</thead>
<tbody>
<?php if ($rows):
print $rows;
?>
<?php endif; ?>
</tbody>
</table>
anyone how got a suggestion?
I'm using PDO and I'm trying to print the results on a table, but the values don't appear. I need to use the <td> tags.
Here is my complete code with a help of francisco:
<?php
require_once('config.php');
$stmt = $connect->prepare("SELECT id, username, email FROM user");
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
?>
<table id="table">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>email</th>
</tr>
</thead>
<tbody>
<?php
foreach ($result as $row):
?>
<tr>
<td><?= $row['id'];?></td>
<td><?= $row['username'];?></td>
<td><?= $row['email'];?></td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
You need to call fetchAll() outside of loop OR call fetch() in each iteration like this:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
// do sth
If you want foreach, just call fetchAll() and do foreach loop over result.