how to call a php function when we open the page - php

/*the function which fetches data from database */public function allClients()
{
try
{
$stmt = $this->conn->query("SELECT * FROM client");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $results;
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
This is the next page where the function is called.I have used a fetch button:'btn-signup' but I want to display this data in a table without using any button how do I do that.
$client2 = new client2();
if (isset($_POST['btn-signup']))
{
try
{
$results=$client2->allClients();
foreach(array ($results) as $row)
{
$id = $row['id'];
$name = $row['name'];
$email = $row['email'];
echo $id."\t\t";
echo $name."\t\t";
echo $email;
echo "\n\n";
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
The table in which i need to fetch the values.THANK you
<table id="data-table-command" class="table table-striped table-vmiddle">
<thead>
<tr>
<th data-column-id="id" data-type="numeric">ID</th>
<th data-column-id="sender">Sender</th>
<th data-column-id="received" data-order="desc">Received</th>
<th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
</tr>
</thead>
<tbody>
<tr>
<td>10238</td>
<td>eduardo#pingpong.com</td>
<td>14.10.2013</td>
</tr>
<tr>
<td>10243</td>
<td>eduardo#pingpong.com</td>
<td>19.10.2013</td>
</tr></tr>
</tbody>
</table>

<table id="data-table-command" class="table table-striped table-vmiddle">
<thead>
<tr>
<th data-column-id="id" data-type="numeric">ID</th>
<th data-column-id="sender">Sender</th>
<th data-column-id="received" data-order="desc">Received</th>
<th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
</tr>
</thead>
<tbody>
<?php
foreach( array($results) as $row )
{
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
You do the foreach inside your table printing the vars with the proper table markup

So why don't you built your table generic with PHP? As much as I can see you are not doing a AJAX-Request or something like that.
something like:
function table()
{
$client2 = new client2();
if (isset($_POST['btn-signup']))
{
try
{
$results=$client2->allClients();
foreach(array ($results) as $row)
{
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '</tr>';
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
/* Set this line where your static rows now are */
<table id="data-table-command" class="table table-striped table-vmiddle">
<thead>
<tr>
<th data-column-id="id" data-type="numeric">ID</th>
<th data-column-id="sender">Sender</th>
<th data-column-id="received" data-order="desc">Received</th>
<th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
</tr>
</thead>
<tbody>
<?php table(); ?>
</tbody>
</table>

change
echo $id."\t\t";
echo $name."\t\t";
echo $email;
echo "\n\n";
to
echo "<tr><td>$id</td><td>$name</td><td>$email</td></tr>;
And call the function before the close of the table.
And also, you have an extra < /tr> before closing the body tag
</tr></tbody>

The problem was with the function,this is the correct code.It's now working perfectly
public function allClients()//function
{
try
{
$stmt = $this->conn->query("SELECT * FROM client");
$stmt->execute();
$results = $stmt->setFetchMode(PDO::FETCH_ASSOC);
return $stmt;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
Function call
<table id="data-table-command" class="table table-striped table-vmiddle">
<thead>
<tr>
<th data-column-id="id" data-type="numeric">ID</th>
<th data-column-id="sender">Sender</th>
<th data-column-id="received" data-order="desc">Received</th>
<th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
</tr>
</thead>
<tbody>
<?php
$results=$client2->allClients();
foreach($results as $row){
echo "<tr><td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['whatphn']."</td>";
// echo "<td>".$row['email']."</td></tr>";
}
?>
</tbody>
</table>

Related

How to view pdf without downloading?

Good day everyone I am using a select command to view pdf from database it is working but it tries to download all pdf every time the page is refresh. I want it to be a normal list then download once click.
here is my code
<?php
function selectFishprice()
{
try {
$connection = connect();
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqlQuery = "SELECT price AS pdf_path, Date FROM upload WHERE id = 1";
$statement = $connection->query($sqlQuery);
$statement->setFetchMode(PDO::FETCH_ASSOC);
$Fishprices = $statement->fetchAll();
if (!empty($Fishprices)) {
return $Fishprices;
} else {
return NULL;
}
} catch (PDOException $exception) {
die("Error: " . $exception->getMessage());
}
}
$fishPrices = selectFishprice();
?>
<table id="users">
<thead>
<tr>
<th scope="col">Fish Market Price</th>
<th scope="col">As of</th>
</tr>
</thead>
<tbody>
<?php
if (!empty($fishPrices)) {
foreach ($fishPrices as $fishPrice) {
$date = strtotime($fishPrice['Date']);
?>
<tr class="table-primary">
<td scope="row"><img src="<?= $fishPrice['pdf_path'] ?>"/></td>
<td scope="row"><?= date('M j Y', $date)?></td>
<?php
}
} else {
?>
<tr class="table-primary">
<td class="text-center" colspan="4" scope="row">No records found.</td>
</tr>
<?php
}
?>
</tbody>
</table>
I read from the site to use "header" but I don't understand where do I put the "header"? Any help is appreciated thank you
Use anchor tag (i.e. <a>)
function selectFishprice()
{
try {
$connection = connect();
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $connection->query("SELECT price, Date FROM upload WHERE id = 1 ");
$statement->setFetchMode(PDO::FETCH_ASSOC);
$Fishprices = $statement->fetchAll();
if (!empty($Fishprices)) {
return $Fishprices;
} else {
return NULL;
}
} catch (PDOException $exception) {
die("Error: " . $exception->getMessage());
}
}
$fishPrices = selectFishprice();
<table id="users">
<thead>
<tr>
<th scope="col">Fish Market Price</th>
<th scope="col">As of</th>
</thead>
<tbody>
<?php
if(!empty($fishPrices)) {
foreach ($fishPrices as $fishPrice) {
$date = strtotime($fishPrice['Date']);
?>
<tr class="table-primary">
<td scope="row"><img src="" alt="PDF"/></td>
<td scope="row"><?= date('M j Y', $date)?></td>
<?php
}
} else {
?>
<tr class="table-primary">
<td class="text-center" colspan="4" scope="row">No records found.</td>
</tr>
<?php
}
?>
</tbody>
</table>

How to do this PHP loop in a table

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>

Display data in table in specific format

i want to display data in specific format that i have attached. Every object,Title and Coordinates added by instructor should be displayed in front of instructor in one row . This is what i want . this is how i am currently getting data
Code for both view and controller is here
<table id="example2" class="table table-bordered table-hover">
<thead>
<tr>
<th>#</th>
<th>Instructor</th>
<th>Title</th>
<th>Coordinates</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php $i=1; foreach ($res as $value) { ?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $value['first_name'] . " " . $value['last_name']; ?></td>
<td><?php echo $value['description']; ?></td>
<td><?php echo $value['coordinates']; ?></td>
<td>
<?php if($edit_delete['edit']) {?>
<i class="fa fa-eye"></i>
<?php } ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
public function index()
{
$this->user_model->check_permissions('Objects/index');
$data['edit_delete']=$this->user_model->checkEditDelete('Objects/index');
$rl_id= $this->rol_id;
$str = implode('-',$rl_id);
if($str==2)
{
$data['menu']=$this->load_model->menu_inst();
}else
{
$data['menu']=$this->load_model->menu();
}
$data['base_url'] = base_url();
$data['userInfo'] = $this->userInfo;
$rl_id= $this->rol_id;
$super = implode('-',$rl_id);
if($super==1)
{
$this->db->select('object.id, object.description, object.coordinates, admin.first_name, admin.last_name');
$this->db->from('object');
$this->db->join('admin', 'admin.id = object.instructor_id');
$this->db->where('admin.is_delete',0);
$this->db->where('admin.role_id',2);
$this->db->where('object.is_delete',0);
$data['res'] = $this->db->get()->result_array();
}
else
{
$in_id= $this->insret_id;
$insrtuctur = implode('-',$in_id);
$data['data'] = $this->db->where('is_delete',0)->where('instructor_id',$insrtuctur)->get('object')->result_array();
}
$data['page'] = "objects/objects";
$this->load->view('Template/main', $data);
}

how do i create html table like on the picture

i have a 3 table and im doing inner join in the output below
how can i achive something like this
so far i already have code for expanding the row. the real problem here is how do i get all the signatoryname for each tracknum. im using php and html
this is my code
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['tracknum'] ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5"><p><?php echo $r['signatoryname'] ?></p>
</td></tr>
<?php endwhile; ?>
</table>
for the table to expand
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function() {
$("td[colspan=5]").find("p").hide();
$("table").click(function(event) {
event.stopPropagation();
var $target = $(event.target);
if ( $target.closest("td").attr("colspan") > 1 ) {
$target.slideUp();
} else {
$target.closest("tr").next().find("p").slideToggle();
}
});
});
});//]]>
</script>
this is the output of the code
i need to group the data below by tracknum but i need the signatoryname
i want the html row to be expandable and list the signatoryname of that tracknum bellow it. thanks.
update: so far this is my code
UPDATE: below is the correct code:
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
// execute the stored procedure
$sql = 'CALL sp_trasactionsignatory()';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['tracknum'] ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5">
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
$_tempp1 = $r['tracknum'];
$stmt = $conn->prepare("CALL sp_gettransactsignatory(?)");
$stmt->bindParam(1, $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<p>" . $row['signatoryname'] . "</p>";
}
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
</td></tr>
<?php endwhile; ?>
</table>
Problem:
How do I get all the signatoryname for each tracknum?
Solution:
Your very first initial query would be like this,
(Since you didn't provide the table name, change the table name from the below queries)
$q = $connection->query("SELECT tracknum, doctitle, doctype, datefilled FROM tablename GROUP BY tracknum");
Then comes to your table,
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch_assoc()): ?>
<tr>
<td><?php echo $r['tracknum']; ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5">
<?php
$result_set = $connection->query("SELECT signatoryname FROM tablename WHERE tracknum = {$r['tracknum']}");
while ($row = $result_set->fetch_assoc()){
echo "<p>" . $row['signatoryname'] . "</p>";
}
?>
</td></tr>
<?php endwhile; ?>
</table>
Don't forget to change the tablename in both the queries.
Edited:
If you using PDO extensions to execute your queries then you can do something like this,
<?php
$_tempp1 = $r['tracknum'];
$stmt = $connection->prepare("SELECT signatoryname FROM tablename WHERE tracknum = :tracknum");
$stmt->bindParam(':tracknum', $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<p>" . $row['signatoryname'] . "</p>";
}
?>

Outputting more than one User Ban Data using PDO

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>";
}

Categories