PHP MySQL display data with table rows (clickable) - php

I have tables that are the publish date of a article and a columns article summary, and those are clickable and should go to a edit page when you click on this. The article should be filled in when you click on it, so you can easy edit the text. I have make the table rows linked but I think I need to take the ID from the tables so it knows what the edit page should be filled in with.
Here is my current code:
<?php
include 'index.php';
include 'login2.php';
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="alleartikelen.css">
</head>
<body>
<h2> Widget news admin </h2>
<!-- Echo username -->
<?php
$user = $_SESSION['user'];
echo "<p>You are logged in as <b>$user</b>.</p>";
?>
<a class="logout" href="index.php">Log out </a>
<hr>
<h1> All Articles </h1>
<?php
$sql = "SELECT art_date, art_head FROM article";
$result = $conn->query($sql); //it makes the query
?>
<style>
.table td {
font-family: Merriweather, serif;
font-size: 16px;
padding: 10px 5px;
overflow: hidden;
word-break: normal;
}
.table th:first-child,
.table td:first-child {
width: 14%;
height: 10%;
}
td,th{
font-family: "Calibri ";
}
tr:nth-child(even){
background-color: #ddd;
}
tr:hover {
background-color: #F6F6F6;
}
th{
color:white;
}
.aa {
text-decoration: none;
color: inherit;
}
</style>
<section>
<div class="container">
<table class="table">
<tr>
<th>Publication date</th>
<th>Article</th>
</tr>
<?php
$res=$result->fetch_all(MYSQL_ASSOC); //Splitting all rows in arrays
$keys = array_keys($res[0]); //Arrays getting attached to $keys
// Make the results as rows
echo "<tbody>";
foreach($res as $rows)
{
echo "<tr>";
foreach($rows as $date)
{
echo "<td><a href='bewerkartikel.php'class ='aa'>$date</a>";
echo "</td>";
}
echo "</tr>";
}
echo "</tr>";
}
?>
</section>
</body>
</html>

Add article id to your link ?art_id=some_id
<?php
include 'index.php';
include 'login2.php';
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="alleartikelen.css">
<style>
.table td {
font-family: Merriweather, serif;
font-size: 16px;
padding: 10px 5px;
overflow: hidden;
word-break: normal;
}
.table th:first-child,
.table td:first-child {
width: 14%;
height: 10%;
}
td,th {
font-family: "Calibri ";
}
tr:nth-child(even){
background-color: #ddd;
}
tr:hover {
background-color: #F6F6F6;
}
th {
color:white;
}
.aa {
text-decoration: none;
color: inherit;
}
</style>
</head>
<body>
<h2> Widget news admin </h2>
<!-- Echo username -->
<?php
$user = $_SESSION['user'];
echo "<p>You are logged in as <b>$user</b>.</p>";
?>
<a class="logout" href="index.php">Log out </a>
<hr>
<h1> All Articles </h1>
<?php
$sql = "SELECT art_date, art_head, art_id FROM article"; //get article id too
$result = $conn->query($sql); //it makes the query
?>
<section>
<div class="container">
<table class="table">
<tr>
<th>Publication date</th>
<th>Article</th>
</tr>
<?php
echo "<tbody>";
while($rows = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>". $rows['art_date'] . "</td>";
echo "<td><a href='bewerkartikel.php?art_id=" . $rows['id'] . "' class ='aa'> " . $rows['art_head'] . "</a>";
echo "</td>";
echo "</tr>";
}
echo "</tr>";
?>
</section>
</body>
</html>
Screenshot

You need to pass the id to next page so what you need to do is add a id to url
like below
"<td><a href='bewerkartikel.php?id=".$date['id']." ' class='aa' >$date</a></td>";
and get the id on bewerkartikel page using
$id = $_GET['id'];
and then you get the particular id to edit.

Related

How to filter and get data between two dates

**Hi, I want to make a one-page website that can filter the data between two dates. I am new to coding and can someone teach me how to do it? I have no idea how to do it, and below is my code. I want to do a web page that can be selecting data between two dates. please help me. I tried to use an example from a website but I still can not make it **
<!DOCTYPE html>
<html lang="en" >
<head>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<style>
#customers {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#customers td, #customers th {
border: 1px solid #ddd;
padding: 8px;
}
#customers tr:nth-child(even){background-color: #f2f2f2;}
#customers tr:nth-child(odd){background-color: #f2f2f2;}
#customers tr:hover {background-color: #ddd;}
#customers th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4CAF50;
color: white;
}
.block {
display: block;
width: 100%;
border: none;
background-color: #4CAF50;
color: white;
padding: 14px 28px;
font-size: 16px;
cursor: pointer;
text-align: center;
}
.block:hover {
background-color: #ddd;
color: black;
}
</style>
<meta charset="UTF-8">
<title>Feedback</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<form action = "#" method="POST">
<div class="w3-show-inline-block">
<div class="w3-bar">
<center>
<input type="submit" value="Download as PDF" name="logout" class="w3-btn w3-black">
</center>
</div>
</div>
</form>
<form action = "logout.php" method="POST">
<div class="w3-show-inline-block">
<div class="w3-bar">
<center>
<input type="submit" value="LogOut" name="logout" class="w3-btn w3-black">
</center>
</div>
</div>
</form>
<?php
session_start();
require 'config.php';
if (isset($_SESSION['login_user'])) {
$userLoggedIn = $_SESSION['login_user'];
$result = mysqli_query($con,"SELECT * FROM poll");
echo "<table border='1' id='customers'>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>feedback</th>
<th>feedback1</th>
<th>feedback2</th>
<th>Suggestions</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['feedback'] . "</td>";
echo "<td>" . $row['feedback1'] . "</td>";
echo "<td>" . $row['feedback2'] . "</td>";
echo "<td>" . $row['suggestions'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
else {
//header("Location: index.php");
}
?>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
</body>
</html>

How to create style of html elements while php codes are embedded in it?

I have this php file where I want to retrieve data from database and show in a tabular format.
<html>
<head>
<title>Admin</title>
<link rel="stylesheet" type="text/css" href="adminPanel.css"/>
</head>
<body>
<div class="header">
<h1>Admin</h1>
</div>
<div class="center">
<?php
include "db_connection.php";
$sql = "SELECT * FROM appointments;";
$result = mysqli_query($db,$sql);
If(mysqli_query($db,$sql) == TRUE){
?>
<table>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
<th>License</th>
<th>Engine</th>
<th>Appointment Date</th>
<th>Preferred Mechanic</th>
<?php
while($row = mysqli_fetch_assoc($result)){
?>
<td><?php echo $row['Name'];?></td></br>
<td><?php echo $row['Address'];?></td>
<td><?php echo $row['Phone'];?></td>
<td><?php echo $row['Car_license_No'];?></td>
<td><?php echo $row['Car_Engine_No'];?></td>
<td><?php echo $row['Date'];?></td>
<td><?php echo $row['Mechanic'];?></td>
<?php
}
?>
</table>
<?php
}
?>
</div>
<div class="footer">
<p id="lastMod">
<script language="Javascript">
document.write("Last modified on " + document.lastModified + " ");
</script>
</p>
</div>
</body>
</html>
But the problem is I can`t change the style of data showing in the table. The .footer, .center, .header these are showing up with the perfect style. But styles of html elements that are written within the php blocks are not working, such as , , .
Here is my style sheet
.header{
background-color: black;
color: DC143C;
height: 150px;
margin-bottom: 0px;
font-family: "Impact"
}
.center{
background-image: url(background.jpg);
height: 400px;
margin-top: 0px;
margin-bottom: 0px;
}
.footer{
background-color: black;
font-family: "Impact";
color: DC143C;
margin-top: 82px;
height: 95px;
}
th, td{
color: white;
}
#lastMod{
margin-top: 0px;
padding: 5px;
}
Searched a lot but havn`t found any relevant answer. Thanks in advance.
You should apply the classes to cells then you could style it.
<td class="name"><?php echo $row['Name'];?></td></br>
...
<td class="mechanic"><?php echo $row['Mechanic'];?></td>
<style>
td.name {
background-color: #0f0;
}
....
td.mechanic {
background-color: #f00;
}
</style>
you didn't added any class in your php you should add class in your table like
<table class="mytable">
and style it by pointing with parent such as
<style>
.mytable th { color:#fff; }
</style>

How to display the full content of a database on clicking on 'Read More' button

I have created a database named Magazine and it has a table social_media. The table has 5 columns namely ID, Headline, Author, Content Short, Content. The Content Short column contains the shorter version of the entire article(which will be displayed in the following code) and the Content column contains the entire article(which will be displayed on clicking the Read More button). I have created a homepage where all the articles from the social_media are displayed in the Content_Left div(NOTE: The Content Short column is displayed and not Content). At the end of each entry from the database there is a 'Read More' button. On clicking on that read more button I want the user to be redirected to a new page 'article.php' where the Headline, Author and Content of the corresponding article will be displayed(The Longer version of the content and not the Content Short). How do I do it? I have created the following webpage:
<?php require_once('connections/connection.php'); ?>
<?php
session_start();
$stmt = $con->query("SELECT * FROM social_media");
?>
<!doctype html>
<html>
<head>
<link href="CSS/Layout.css" rel="stylesheet" type="text/css" />
<link href="CSS/menu.css" rel="stylesheet" type="text/css" />
<style type="text/css">
</style>
<meta charset="utf-8">
<title>Home</title>
</head>
<body style="background-color:#E0DDDD">
<div id="Container">
<div id="Header"></div>
<div id="NavBar">
<nav>
<ul>
<li>Home</li>
<li>Social Media</li>
<li>Tech</li>
<li>Tips & Tricks</li>
</ul>
</nav>
</div>
<div id="Content">
<div id="Content_Left">
<h1><center>Social Media</center></h1>
<table>
<?php
while($records=$stmt->fetch_assoc()) {
$_SESSION["ID"] = $records['ID'];
echo "<tr>";
echo "<th><h3>".$records['Headline']."</h3></th>";
echo "</tr>";
echo "<tr>";
echo "<td>By <strong>".$records['Author']."</strong></td>";
echo "</tr>";
echo "<tr>";
echo "<td>".$records['Content_short']." Read More...</td>";
echo "</tr>";
}
?>
</table>
</div>
<div id="Content_Center">
<h1>Header Here </h1>
<p>Text Goes Here!</p>
</div>
<div id="Content_Right">
<h1>Header Here </h1>
<p>Text Goes Here!</p>
</div>
</div>
<div id="Footer">
<center>Your Copyright Message</center>
</div>
</div>
</body>
</html>
And here is the CSS file(Layout.css):
body{
margin:0;
padding: 0;
}
#Container {
width: 980px;
height:auto;
margin-left:auto;
margin-right:auto;
margin-top:20px;
margin-bottom:21px;
}
#Header {
height:120px;
background-image:url(../Assets/Untitled-1.png);
background-repeat:no-repeat;
margin-bottom:21px;
}
#NavBar {
height:60px;
background-color:#000000;
}
#Content {
background-color:#FFFFFF;
margin-top: 20px;
padding: 5px;
overflow:hidden;
}
#Content_Left {
height: auto;
width: 210px;
padding:5px;
float:left;
background-color: lightblue
}
#Content_Center {
height: auto;
padding:5px;
width: 500px;
float:left;
margin-left: 10px;
margin-right: 10px;
background-color: lightblue
}
#Content_Right {
height: auto;
padding:5px;
width: 210px;
float: right;
background-color: lightblue
}
#Container #Content h1 {
font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;
font-style: normal;
font-variant: normal;
font-weight: bolder;
text-shadow: 0px 0px;
}
#Footer {
padding-top: 10px;
height: 100px;
}
And here is the CSS file(menu.css):
nav ul {
margin:0;
padding:0;
}
nav ul li {
list-style-type:none;
display:block;
width:150px;
height:60px;
float:left;
text-align:center;
line-height:55px;
font-family:Baskerville, "Palatino Linotype", Palatino, "Century Schoolbook L", "Times New Roman", serif;
font-size:17px;
}
nav ul li a {
text-decoration:none;
color:#FFF;
}
nav ul li:hover {
background-color:#BBB5B5;
}
nav ul li:hover a {
display:block;
color:Black;
}
I want to display that article in the new page but with the same background theme. Or is there any other way to display the entire article? I hope this much detail is enough. In case any more information is needed please ask.
EDIT: As per the suggestions of the fellow members I have edited the code. My article.php file now looks like this:
<?php require_once('connections/connection.php');?>
<?php
$articleId = $_GET['articleId'];
// you need to fetch only one record this time for showing only the article that wanted to be read. so use `where` condition
$stmt = $con->query("SELECT * FROM social_media WHERE id = $articleId ");
?>
<!doctype html>
<html>
<head>
<link href="CSS/Layout.css" rel="stylesheet" type="text/css" />
<link href="CSS/menu.css" rel="stylesheet" type="text/css" />
<style type="text/css">
</style>
<meta charset="utf-8">
<title>Home</title>
</head>
<body style="background-color:#E0DDDD">
<div id="Container">
<div id="Header"></div>
<div id="NavBar">
<nav>
<ul>
<li>Home</li>
<li>Social Media</li>
<li>Tech</li>
<li>Tips & Tricks</li>
</ul>
</nav>
</div>
<div id="Content">
<div id="Content_Full">
<table>
<?php
while($records=$stmt->fetch_assoc()) {
echo "<tr>";
echo "<th><h1>".$records['Headline']."</h1></th>";
echo "</tr>";
echo "<tr>";
echo "<td>By <strong>".$records['Author']."</strong></td>";
echo "</tr>";
echo "<tr>";
echo "<td>".$records['Content']."</td>";
echo "</tr>";
}
?>
</table>
</div>
</div>
<div id="Footer">
<center>Your Copyright Message</center>
</div>
</div>
</body>
</html>
However, I am getting the following error:
Fatal error: Call to a member function fetch_assoc() on a non-object
How do I resolve this problem?
Change few thing
in php file
echo "<td>".$records['Content_short']." <a href='article.php'>Read More...</a></td>";
to
echo "<td>".$records['Content_short']." <a href='article.php?articleId= " . $records['ID']. "'>Read More...</a></td>";
then in your article.php
<?php
if(isset($_GET['articleId'])){
$articleId = $_GET['articleId'];
}else{
echo "Invalid access"; die;
}
// you need to fetch only one record this time for showing only the article that wanted to be read. so use `where` condition
$stmt = $con->query("SELECT * FROM social_media WHERE id = $articleId ");
Follow same html code of your article listing page on article.php
Change your below line
echo "<td>".$records['Content_short']." <a href='article.php'>Read More...</a></td>";
To:
echo "<td>".$records['Content_short']." Read More...</td>";
Now you've to get details by ID and display data accordingly.

Delete comment from database php

I am trying to delete comments from my comment system but the code I have is not working. I want to be able to delete each comment from my table including the parent comments. At the moment my code does not get the parameter of the comment from the delete button link.
function.php:
<?php
function getComments($row) {
echo "<li class='comment'>";
echo "<div class='aut'>" . $row['author'] . "</div>";
echo "<div class='comment-body'>" . $row['comment'] . "</div>";
echo "<div class='timestamp'>" . $row['created_at'] . "</div>";
echo "<a href='#comment_form' class='reply' id='" . $row['id'] . "'>Edit Song</a>";
echo "<span> </span>";
echo "<a href='delete.php?id='" . $row['id'] . "'>Delete Song</a>";
$q = "SELECT * FROM threaded_comments WHERE parent_id = ".$row['id']."";
$r = mysql_query($q);
if(mysql_num_rows($r)>0)
{
echo "<ul>";
while($row = mysql_fetch_assoc($r)) {
getComments($row);
}
echo "</ul>";
}
echo "</li>";
}
?>
delete.php:
<?php
include 'includes/config.php';
$deleteid = $_GET['id'];
mysql_query("DELETE FROM threaded_comments WHERE id='$deleteid'");
echo "Song has been deleted!";
header ('Location: index.php');
?>
I have added the index.php and post_comment page page. Also the delete function still doesn't work when I made the changes.
index.php:
<?php
include("config.php");
include("functions.php");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Multi-Lyric Collaboration System</title>
<script type='text/javascript' src='jquery.pack.js'></script>
<script type='text/javascript'>
$(function(){
$("a.reply").click(function() {
var id = $(this).attr("id");
$("#parent_id").attr("value", id);
$("#name").focus();
});
});
</script>
<style type='text/css'>
html, body, div, h1, h2, h3, h4, h5, h6, ul, ol, dl, li, dt, dd, p, blockquote,
pre, form, fieldset, table, th, td { margin: 0; padding: 0; }
body {
font-size: 14px;
line-height:1.3em;
background: url('music.jpg');
height: 100%;
background-size:100% ;
background-position: static;
background-color: purple;
min-height: 100%;
background-repeat: no-repeat;
}
label{
color: white;
font-size: 2em;
}
a, a:visited {
outline:none;
color:#7d5f1e;
}
.clear {
clear:both;
}
#wrapper {
width:480px;
margin:0px auto;
padding:15px 0px;
}
.comment {
padding:5px;
border:2px solid black;
border-width: 1px;
margin-top:15px;
list-style:none;
background-color: white;
opacity: 0.96;
-moz-border-radius:20px;
-webkit-border-radius:20px;
overflow-y: scroll;
}
.aut {
font-weight:bold;
}
.timestamp {
font-size:85%;
float:right;
}
#comment_form {
margin-top:15px;
}
#comment_form input {
font-size:1.2em;
margin:0 0 10px;
padding:3px;
display:block;
width:100%;
}
#comment_body {
display:block;
width:100%;
height:150px;
}
#submit_button {
text-align:center;
clear:both;
}
header{
color: white;
text-align: center;
}
</style>
</head>
<body>
<div id='wrapper'>
<header><font size="20">MultiLyric Collaborator</font></header>
<br>
<ul>
<?php
$q = "SELECT * FROM threaded_comments WHERE parent_id = 0";
$r = mysql_query($q);
while($row = mysql_fetch_assoc($r)):
getComments($row);
endwhile;
?>
</ul>
<form id="comment_form" action="post_comment.php" method='post'>
<label for="name">Name:</label>
<input type="text" name="name" id='name'/>
<label for="comment_body">Enter Song:</label>
<textarea name="comment_body" id='comment_body'></textarea>
<input type='hidden' name='parent_id' id='parent_id' value='0'/>
<div id='submit_button'>
<input type="submit" value="Add comment"/>
</div>
</form>
</div>
</body>
</html>
post_comment.php:
<?php
include("config.php");
$author = mysql_real_escape_string($_POST['name']);
$comment_body = mysql_real_escape_string($_POST['comment_body']);
$parent_id = mysql_real_escape_string($_POST['parent_id']);
$q = "INSERT INTO threaded_comments (author, comment, parent_id) VALUES ('$author', '$comment_body', $parent_id)";
$r = mysql_query($q) or die(mysql_error());
if(mysql_affected_rows()==1) {
header("location:index.php");
}
else {
echo "Comment cannot be posted. Please try again.";
}
?>
From the "SELECT" statement in getComments(), it looks like what you need to do is:
mysql_query("DELETE FROM threaded_comments WHERE parent_id ='$deleteid'");
It is 'parent_id' not 'id'.

Error message while trying to make a shopping cart

The following code is not my own. I'm trying to learn how to make a shopping cart but i'm stuck on this part of the tutorial. Please verify the code and tell me what i'm doing wrong.
This is products.php:
if(isset($_GET['action']) && $_GET['action']=="add"){
$id=intval($_GET['id']);
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]['quantity']++;
}else{
$query_s=mysql_query($sql_s);
$row_s=mysql_fetch_array($query_s);
$_SESSION['cart'][$row_s'id_product']]=array(
"quantity" => 1,
"price" => $row_s['price']
);
}
}
?>
<h1>Product List</h1>
<?php
echo print_r($_SESSION['cart']);
?>
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Action</th>
</tr>
<?php
$sql = "SELECT * FROM products ORDER BY name ASC";
$query = mysql_query($sql);
while($row=mysql_fetch_array($query))
{
?>
<tr>
<td><?php echo $row['name'] ?> </td>
<td><?php echo $row['description'] ?> </td>
<td><?php echo $row['price'] ?>$</td>
<td>Add to cart</td>
</tr>
<?php
}
?>
</table>
This is index.php:
<?php
session_start();
include("connection.php");
if(isset($_GET['page']))
{
$pages=array("products","cart");
if(in_array($_GET['page'], $pages)) {
$_page=$_GET['page'];
}else{
$_page="products";
}
}
else {
$_page="products";
}
?>
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Shopping cart</title>
</head>
<body>
<div id="container">
<div id="main">
<?php require($_page.".php"); ?>
</div> <!--end of main-->
<div id="sidebar">
</div> <!--end of sidebar-->
</div> <!--end of container-->
</body>
</html>
This is the css file style.css:
a {
color: #48577D;
text-decoration:none;
}
a:hover{
text-decoration:underline;
}
body {
font-family: Verdana;
font-size: 12px;
color: #444;
}
h1, h2 { margin-bottom: 15px;}
h1 { font-size:18px;}
h2 {font-size: 16px;}
#container {
width:700px;
margin: 150px auto;
background-color:#eee;
padding:15px;
overflow:hidden;
}
#main {
width: 490px;
float: left;
}
#main table {
width: 480px;
}
#main table th {
padding: 10px;
background-color: #48577D;
color: #fff;
text-align: left;
}
#main table td {
padding: 5px;
}
#sidebar {
width: 200px;
float: left;
}
This is the connection.php:
<?php
$con=mysql_connect("localhost","root","");
$database=mysql_select_db('tutorials');
?>
This is cart.php:
This is your cart
It's not incrementing the values properly. Thanks.
There is a syntax error here:
$_SESSION['cart'][$row_s'id_product']]
I think it's supposed to be:
$_SESSION['cart'][$row_s['id_product']]
You will easily catch these errors if you turn on error reporting. There is no reason to not have it turned on in development.

Categories