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>
Related
/*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>
in my programe, user login and can upload images. user may view all his images and delete it.
The problem is i only can display 1 image. If the user have more that 1 image how to display it.
display.php
<?php
session_start();
require_once 'class.user.php';
$user_home = new USER();
$reg_user = new USER();
$stmt = $user_home->runQuery("SELECT * FROM bike_tbl WHERE userID=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$userID=$row['userID'];
echo $userID;
if(isset($_POST['btn-update']))
{
$status = $_POST['status'];
$remarks = $_POST['remarks'];
if($reg_user->updateBike($status,$remarks,$userID))
{
echo "good";
}
else {
echo "bad";
}
}
?>
<form action="" method="post">
<table class="table table-bordered">
<thead>
<tr>
<th>Image</th>
<th>File Name</th>
<th>Serial Number</th>
<th>Status</th>
<th>Remarks</th>
</tr>
</thead>
<?php while ($row[image])?>
<tbody>
<tr>
<td><img src="uploads/<?php echo $row['file'] ?>" height="100" width="100"></td>
<td><?php echo $row['file'] ?></td>
<th><?php echo $row['serialNumber'] ?></th>
<td><select name="status">
<option value="In use">In use</option>
<option value=Deleted>Deleted</option>
</select>
</td>
<td><input type="text" name="remarks" /></td>
</tr>
</tbody>
</table>
<button class="btn btn-large btn-primary" type="submit" name="btn-update">Update</button>
</form>
class.user.php
public function updateBike($status,$remarks,$userID)
{
try
{
// echo $status;
// echo$remarks;
// echo$userID;
// echo "UPDATE bike_tbl SET status=$status,remarks=$remarks WHERE userID=$userID:";
$stmt = $this->conn->prepare("UPDATE bike_tbl SET status=:status,remarks=:remarks WHERE userID=:userID");
$stmt->bindparam(":status",$status);
$stmt->bindparam(":remarks",$remarks);
$stmt->bindparam(":userID",$userID);
$stmt->execute();
return $stmt;
}
catch(PDOException $ex)
{
echo $ex->getMessage();
}
}
I have a HTML table and the data is from the database. I've then added a second table.
The table is shown by clicking the row. The row is expanded and shows the second table.
What is wrong with my code? It gives data from database but it doesn't loop. For example, I'm expecting 3 rows, but it only output one row.
The query is correct.
This is the code for the second table,
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Date filled</th>
<th>Date signed</th>
</tr>
</thead>
<tbody>
<tr>
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
$_tempp1 = $row1['tracknum'];
$stmt = $conn->prepare("CALL sp_gettransactsignatory(?)");
$stmt->bindParam(1, $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ ?>
<tr>
<td><?php echo $row['signatoryname'] ?></td>
<td><?php echo $row['datefilled'] ?></td>
<td><?php echo $row['datesigned'] ?></td>
</tr>
<?php
}
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
</tr>
</tbody>
</table>
</td>
</tr>
Changes are commented :-
<tr>
<td colspan="5">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Date filled</th>
<th>Date signed</th>
</tr>
</thead>
<tbody>
<!-- remove <tr> -->
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
$_tempp1 = $row1['tracknum'];
$stmt = $conn->prepare("CALL sp_gettransactsignatory(?)");
$stmt->bindParam(1, $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ ?>
<tr>
<td><?php echo $row['signatoryname']; ?></td><!-- ; missed -->
<td><?php echo $row['datefilled']; ?></td><!-- ; missed -->
<td><?php echo $row['datesigned']; ?></td><!-- ; missed -->
</tr>
<?php}} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}?>
<!-- remove </tr> -->
</tbody>
</table>
</td>
</tr>
I have the code to do the search. But the search results are not in the same table. All search results appear in a different table. How do I make them appear in one table?
screenshots :
<?php if(isset($_POST['submit']))
{
if(empty($_POST['word'])){
echo "<center>Title do not match. Please insert the correct title.</center>";}
else {
if(isset($_POST['word'])&& !empty($_POST['word']))
{
require 'config.php';
$word = $_POST['word'];
$query="SELECT * FROM data WHERE word LIKE '%" . $word . "%'";
$sql = $conn->query($query); ?>
<?php if(!$sql)
{
echo "<center>No Record</center>";
}
?>
<table align="center" border="0" >
<tr>
<td width="900">
<?php while($row = $sql->fetch_assoc()){
$dataID = $row['dataID'];
?>
<?php if($sql ==true){?>
<table class="table table-bordered table-hover table-striped">
<tr>
<td width="258" align="center" class="style5">TITLE</td>
<td width="170" align="center" class="style5">MENTION</td>
</tr>
<tr>
<td style="text-transform:uppercase" align="center"> <span style="text-transform:uppercase"><?php echo $row{'word'};?></span> </td>
<td style="text-transform:uppercase" align="center"><span style="text-transform:uppercase"><?php echo $row{'mention'};?></span></td>
</tr>
<?php }}} ?>re
Where do we start?
1.) Try avoiding styling in your html. It isn't forbidden, but your global style is better of when used with CSS.
http://www.w3schools.com/css/
For exampe: Imagine this rule:
<td style="text-transform:uppercase" align="center"> <span style="text-transform:uppercase"><?php echo $row{'word']; ?></span> </td>
that could also be:
<td>
<a href="admindisplay.php?id=<?php echo $row['dataID']; ?>">
<?php echo $row['word'];?>
</a>
</td>
2.) Try indenting. Use an IDE (Notepad with some extra highlighting features and nice indenting. By structuring and neatly placing the brackets "[] {} and ()" you make code more readable and make it easier to DEBUG your own code.
3.) When using SQL, your input should ALWAYS be escaped. Maybe I'm wrong and you have a DB class that does this for you. Although, you should ALWAYS be aware of this: This could prevent SQL Injection and might one day safe your life (or your job).
Escaping: Making your query safe and let it do only thing you want it to do.
SQL INJECTION: Adding malicous characters and code to INPUT so your QUERY does other things than you want.
4.) Try structering what you are doing or the goal you're trying to reach.
<?php
Class DoSearch()
{
protected $search_string;
public function __construct()
{
if(!$this->verify())
return $this->__html('<center>Title do not match. Please insert the correct title.</center>');
$this->search()
}
public function verify()
{
if(isset($_POST['word']))
return false;
if(empty($_POST['word']))
return false;
global $conn;
$this->search_string = $conn->escape($_POST['word']);
return true;
}
public function search()
{
global $conn;
$query = "SELECT * FROM data WHERE word LIKE '%" . $this->search_string . "%'";
$sql = $conn->query($query);
if(!$sql)
return $this->__html('<center>No Record</center>');
$table = array();
$i = 0;
$tableHtml = '
<table class="table table-bordered table-hover table-striped">
<tr>
<th width="258" align="center" class="style5">TITLE</th>
<th width="170" align="center" class="style5">MENTION</th>
</tr>';
while($row = $sql->fetch_assoc())
{
$tableHtml .= '
<tr>
<td style="text-transform:uppercase" align="center"> <span style="text-transform:uppercase">'.$row['word'].'</span> </td>
<td style="text-transform:uppercase" align="center"><span style="text-transform:uppercase">'. $row['mention'].'</span></td>
</tr>
';
}
$tableHtml .= '
</table>
';
return $this->__html($tableHtml);
}
public function __html($msg)
{
echo $msg;
}
}
require_once 'config.php';
$search = new DoSearch();
Move your table code outside of your loop, and only create a new row each time you loop. You were creating a new table each time you loop.
<table class="table table-bordered table-hover table-striped">
<tr>
<td width="258" align="center" class="style5">TITLE</td>
<td width="170" align="center" class="style5">MENTION</td>
</tr>
<?php while($row = $sql->fetch_assoc()){
$dataID = $row['dataID'];
?>
<?php if($sql ==true){?>
<tr>
<td style="text-transform:uppercase" align="center"> <span style="text-transform:uppercase"><?php echo $row{'word'};?></span> </td>
<td style="text-transform:uppercase" align="center"><span style="text-transform:uppercase"><?php echo $row{'mention'};?></span></td>
</tr>
<?php }}} ?>
Don't create a table each time a loop run. Make it simple.
Updated Code
<?php
if(isset($_POST['submit']))
{
if(empty($_POST['word'])) {
echo "<center>Title do not match. Please insert the correct title.</center>";}
else
{
if(isset($_POST['word']) && !empty($_POST['word']))
{
require 'config.php';
$word = $_POST['word'];
$query="SELECT * FROM data WHERE word LIKE '%" . $word . "%'";
$sql = $conn->query($query); ?>
<?php if(!$sql)
{
echo "<center>No Record</center>";
}
?>
<table class="table table-bordered table-hover table-striped">
<tr>
<td width="258" align="center" class="style5">TITLE</td>
<td width="170" align="center" class="style5">MENTION</td>
</tr>
<?php
if($sql ==true)
{
while($row = $sql->fetch_assoc())
{
$dataID = $row['dataID'];
?>
<tr>
<td style="text-transform:uppercase" align="center">
<a href="admindisplay.php?id=<?php echo $row{'dataID'}?>">
<span style="text-transform:uppercase"><?php echo $row{'word'};?></span>
</a>
</td>
<td style="text-transform:uppercase" align="center">
<span style="text-transform:uppercase"><?php echo $row{'mention'};?></span>
</td>
</tr>
<?}}
else
{?>
<tr>
<td colspan=2>Results Not Found.</td>
</tr>
<?}?>
</table>
<?php
}
}
}
?>
If the result is to be issued only in a (unformated) table, simply use this function:
function show_in_table($arr)
{
$count = count($arr);
$output = "";
if($count>0)
{
reset($arr);
$num = count(current($arr));
$output.= '<table cellpadding="0" cellspacing="0">'."\n";
$output.="<tr>\n";
foreach(current($arr) as $key => $value)
{
$output.="<th>";
$output.=$key." ";
$output.="</th>\n";
}
$output.="</tr>\n";
while ($curr_row = current($arr))
{
$output.="<tr>\n";
$col = 1;
while (false !== ($curr_field = current($curr_row)))
{
$output.="<td>";
$output.=$curr_field." ";
$output.="</td>\n";
next($curr_row);
$col++;
}
while($col <= $num)
{
$output.="<td> </td>\n";
$col++;
}
$output.="</tr>\n";
next($arr);
}
$output.="</table>\n";
}
return $output;
}
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>";
}
?>