How to only display date, not time, in a table using PHP - php

This is what my table currently looks like:
This is the corresponding PHP code:
<?php
//include 'database.php';
$pdo2 = Database::connect();
$sql2 = "SELECT id,
full_name,
owner_name awardee,
owner awardee_email,
(select concat(user_name,' ',user_last_name) from users where email = who_created) awarder,
creation_time
FROM awards, award_types
where who_created = ? and awards_type=type_id";
$q2 = $pdo->prepare($sql2);
$q2->execute(array($_COOKIE["email"]));
$date = date('Y-m-d', strtotime($date));
foreach ($q2 as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['full_name'] . '</td>';
echo '<td>'. $row['awardee'] . '</td>';
echo '<td>'. $row['awardee_email'] . '</td>';
echo '<td>'. $row['awarder'] . '</td>';
echo '<td>'. $row['creation_time'] . '</td>';
echo '<td><a class="btn" href="DeleteAward.php?id='.$row['id'].'">Delete Award</a></td>';
echo '</tr>';
}
Database::disconnect();
?>
I want to only display the date, not time, under the "Date" column. I read from other posts that I need to use some sort of date(creation_time) or DATE_FORMAT(creation_time) command, but I tried both and they just make my "Date" column blank.
I suspect that I'll need to change the echo '<td>'. $row['creation_time'] . '</td>'; line as well, but I was not able to find any guidelines on how to do so. Any help would be greatly appreciated!

There's two ways to go about it:
The MySQL way:
SELECT DATE(creation_time) AS creation_time
Or the PHP way:
$creation_date = date('Y-m-d', strtotime($row['creation_time']));
Edit Here's the exact code for each way:
Mysql:
$sql2 = "SELECT id,
full_name,
owner_name awardee,
owner awardee_email,
(select concat(user_name,' ',user_last_name) from users where email = who_created) awarder,
DATE(creation_time) AS creation_time
FROM awards, award_types
where who_created = ? and awards_type=type_id";
And PHP version:
foreach ($q2 as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['full_name'] . '</td>';
echo '<td>'. $row['awardee'] . '</td>';
echo '<td>'. $row['awardee_email'] . '</td>';
echo '<td>'. $row['awarder'] . '</td>';
$creation_time = date('Y-m-d', strtotime($row['creation_time']));
echo '<td>'. $creation_time . '</td>';
echo '<td><a class="btn" href="DeleteAward.php?id='.$row['id'].'">Delete Award</a></td>';
echo '</tr>';
}

Related

html/php sql query where clause

This code works but the one belows shows an error
if(isset($_POST['month'])=='')
{
$sql = ('SELECT
substring(pin,5,2) as District,
count(arpn) as RPU,
sum(area) as AREA,
sum(marketvalue) as MV,
sum(assessedvalue) as AV
FROM 2017_oct_land
WHERE taxability= "T"
group by District ASC');
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['District'] . '</td>';
echo '<td class="RPU">'. $row['RPU'] . '</td>';
echo '<td class="AREA">'. $row['AREA'] . '</td>';
echo '<td class="MV">'. $row['MV'] . '</td>';
echo '<td class="AV">'. $row['AV'] . '</td>';
}
include 'total.php';
}
--with AND-- why do i get this "Warning: Invalid argument supplied for foreach() in C:\wamp64\www\reportview\pages\code2.php on line 20" with this code?
if(isset($_POST['month'])!=NULL)
{
$kind = $_POST['kind'];
echo "MONTH : ".$kind."<br/>";
$sql = ('SELECT
substring(pin,5,2) as District,
count(arpn) as RPU,
sum(area) as AREA,
sum(marketvalue) as MV,
sum(assessedvalue) as AV
FROM julcons
WHERE taxability="T" and actualuse like "' . $kind .'"');
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['District'] . '</td>';
echo '<td class="RPU">'. $row['RPU'] . '</td>';
echo '<td class="AREA">'. $row['AREA'] . '</td>';
echo '<td class="MV">'. $row['MV'] . '</td>';
echo '<td class="AV">'. $row['AV'] . '</td>';
}
include 'total.php';
}
Here you go ( untested ) but should be close
if(isset($_POST['kind'])) {
//why check month and then use kind? which is it..
//isset returns boolean, not null, null works because it's false but it's not correct IMO
$kind = $_POST['kind']; //what is this kind or month
echo "MONTH : ".$kind."<br/>";
$sql = 'SELECT
substring(pin,5,2) as District,
count(arpn) as RPU,
sum(area) as AREA,
sum(marketvalue) as MV,
sum(assessedvalue) as AV
FROM julcons
WHERE taxability="T" and actualuse like :kind';
$stmt = $pdo->prepare($sql);
$stmt->execute([':kind' => $kind]);
while( false !== ( $row = $stmt->fetch(PDO::FETCH_ASSOC))){
echo '<tr>';
echo '<td>'. $row['District'] . '</td>';
echo '<td class="RPU">'. $row['RPU'] . '</td>';
echo '<td class="AREA">'. $row['AREA'] . '</td>';
echo '<td class="MV">'. $row['MV'] . '</td>';
echo '<td class="AV">'. $row['AV'] . '</td>';
}
include 'total.php';
}
Sorry, but you had some really big mistakes in your code.

Unable to print picture after request

What´s is wrong on this sentence?
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['u_id'] . '</td>';
echo '<td>'. $row['u_role'] . '</td>';
echo '<td>'. $row['u_name'] . '</td>';
echo '<td>'. $row['u_passw'] . '</td>';
echo '<td>'. $row['u_init'] . '</td>';
echo '<td>'. $row['c_name'] . '</td>';
echo '<td>'. $row['u_mail'] . '</td>';
echo '<td>'.'<img href="'. $row['u_pic'] . '" width=45 height=45></img>'.'</td>';`
}
I´m able to print all fields after query but for last one is not possible, I got on db saved the url for the picture with this format
http://localhost/Pics/st.gif dbfield as varchar
If I inspect the component in the browser I can see it was properly selected by mysql but there is not print on the screen.
Any comment? Thanks in advance
For images you'll have to use:
<img src="url">
echo '<td><img src="'. $row['u_pic'] . '" width=45 height=45></img></td>';
It should be in following way,
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['u_id'] . '</td>';
echo '<td>'. $row['u_role'] . '</td>';
echo '<td>'. $row['u_name'] . '</td>';
echo '<td>'. $row['u_passw'] . '</td>';
echo '<td>'. $row['u_init'] . '</td>';
echo '<td>'. $row['c_name'] . '</td>';
echo '<td>'. $row['u_mail'] . '</td>';
echo '<td>'.'<img src="'. $row['u_pic'] . '" width=45 height=45></img>'.'</td>';
}

Echoing results as file hyperlinks

I am creating a table by echoing results out as the following:
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM kayitlar ORDER BY id DESC';
foreach ($pdo->query($sql) as $row)
{
echo '<tr>';
echo '<td>'. $row['model'] . '</td>';
echo '<td>'. $row['problem'] . '</td>';
echo '<td>'. $row['work'] . '</td>';
echo '<td>'. $row['result'] . '</td>';
echo '<td>'. $row['keywords'] . '</td>';
echo '<td>'. $row['addedby'] . '</td>';
echo '<td>'. $row['date_time'] . '</td>';
echo '<td>'. $row['document'] . '</td>';
}
I allowed users to add documents and the file name is being recorded into documents after string operations. I want to display respective documents as hyperlinks. If I was using mysql_fetch array I would use
<td> view </td>
but I am not good at PDO and getting synthax error everytime.
here is my erroneous code:
echo '<td>'. view file.'</td>';
simply try echo '<td>view file</td>';
Your echo statement mixes inline html with echo. You should use inline html or echo a string, but not both at the same time
echo '<td>view file</td>';
or
<td>view file</td>
echo '<td>view file</td>';

Make the content of td table linkable

Please can someone review this code and tell me how to get the content of <td> table clickable?
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM products ORDER BY product_id DESC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['cat_id'] . '</td>';
echo '<td>'. $row['brand_id'] . '</td>';
echo '<td>'. $row['product_title'] . '</td>';
echo '<td>'. $row['product_img1'] . '</td>';
echo '</tr>';
}
Database::disconnect();
?>
What I want is to make by example $row['brand_id'] a link that redirect to the brand details.
Your TD has to have a hyperlink in it.
echo '<td>'. $row['brand_id']. '</td>';
with URL?brand_id=... being the URL of the page with the brand details, where you pass your brand_id into.

Dynamically Apend data in html table from mysql using php

I have mysql table with following columns :
no, name, oname, quantity
I have n number of records in the tables ,I want to fetch these records in an html table that dynamically increases'decreases its row length according to the number of rows in mysql database. I am trying following but its not working.can anyone help me out here
<?php
include './connection.php';
$query = "select * from orderdetails";
$result = mysql_query($query);
echo '<table border="1" style="width:600px" align=center >';
echo '<tr bgcolor="lavendar">';
echo '<td width="15%">Order No.</td>';
echo '<td>Name</td>';
echo '<td>Order</td>';
echo '<td>Quantity</td>';
echo '</tr>';
echo '</table>';
while( $row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' row['no'] '</td>';
echo '<td>' row['name'] '</td>';
echo '<td>' row['oname'] '</td>';
echo '<td>' row['quantity'] '</td>';
echo '</tr>';
}
echo '</table>';
?>
Where you are printing out your values, there are a few errors.
In this specific code block:
while( $row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' row['no'] '</td>';
echo '<td>' row['name'] '</td>';
echo '<td>' row['oname'] '</td>';
echo '<td>' row['quantity'] '</td>';
echo '</tr>';
}
First off all, you would want to concatinate your strings, PHP uses . for that.
For example, to concatinate "World!" to "Hello, ", to print out Hello, World!, you would use the following:
echo "Hello, " . "World!";
You are also missing a variable sign ($) in your code, when printing out the rows.
In your case, you're using echo '<td>' row['no'] '</td>';,
where it should have been echo '<td>' . $row['no'] . '</td>';
To clarify, your code should look like this:
while($row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' . $row['no'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['oname'] . '</td>';
echo '<td>' . $row['quantity'] . '</td>';
echo '</tr>';
}
Relevant PHP documentation:
PHP String Concatination
Echo documentation
There's a typo-
row['no']
to
$row['no']
Similarly other variables too.

Categories