I have a total 4 book of abc. I showing that author 1 book in book detail page. As well as i want to display remaining 3 books of author in below other books by this writer section. But in this section showing 1 book of that author. Please help me what can i do?
Book detail code:
<?php
if(isset($_GET['pid']) && !empty($_GET['pid'])){
$id = $_GET['pid'];
$result = $conn->query("SELECT * FROM bookrecord WHERE id ='$id' and status='a'");
if($result->num_rows > 0){
$imgData = $result->fetch_assoc();
extract($imgData);
?>
<div class="preview-pic tab-content">
<div class="tab-pane active" id="pic-1">
<img src="admin_pannel/book_images/<?php echo $imgData['file']; ?>" class="img-responsive" alt=""/>
</div>
</div>
<?php
}
}
?>
OTHER BOOKS BY THIS WRITER code:
<?php
if(isset($_GET['auth_name']) && !empty($_GET['auth_name'])){
$auth_name = $_GET['auth_name'];
$result = $conn->query("SELECT * FROM bookrecord WHERE author_name ='$auth_name' and status='a'");
if($result->num_rows > 0){
$row = $result->fetch_assoc();
extract($row);
?>
<div class="col-md-2">
<img src="admin_pannel/book_images/<?php echo $row['file']; ?>" class="img-responsive center-block">
<h4 class="text-center"><?php echo $row['book_title']; ?></h4>
<h5 class="text-center"><?php echo $row['author_name']; ?></h5>
</div>
<?php
}
}
?>
This is my (book-detail.php) page and i come from this link:
<a class="btnpoem" href="book-detail.php?pid=<?php echo htmlentities($row['id']);?>&auth_name=<?php echo htmlentities($row['author_name']);?>">Download</a>
You need to apply while() in your second code:-
<?php
if(isset($_GET['auth_name']) && !empty($_GET['auth_name'])){
$auth_name = $_GET['auth_name'];
$result = $conn->query("SELECT * FROM bookrecord WHERE author_name ='$auth_name' and status='a'");
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
extract($row);
?>
<div class="col-md-2">
<img src="admin_pannel/book_images/<?php echo $row['file']; ?>" class="img-responsive center-block">
<h4 class="text-center"><?php echo $row['book_title']; ?></h4>
<h5 class="text-center"><?php echo $row['author_name']; ?></h5>
</div>
<?php
}
}
}
?>
Note:- Your queries are Wide-Open for SQL-INJECTION.
So try to use prepared statements.
Reference:-
PHP: mysqli::prepare - Manual
PHP: PDO::prepare - Manual
Just change your code from this code and in this code you have to change your sql query a lil bit to get rows except first one because you have got first row(Image), and instead of while loop you can also use foreach
<?php
if(isset($_GET['auth_name']) && !empty($_GET['auth_name'])){
$auth_name = $_GET['auth_name'];
$result = $conn->query("SELECT * FROM bookrecord WHERE author_name ='$auth_name' and status='a'" and WHERE ID NOT IN (SELECT MIN(ID) FROM your_table GROUP BY USER_ID));
if($result->num_rows > 0){
foreach ($result as $key){
?>
<div class="col-md-2">
<img src="admin_pannel/book_images/<?php echo $key['file']; ?>" class="img-responsive center-block">
<h4 class="text-center"><?php echo $key['book_title']; ?></h4>
<h5 class="text-center"><?php echo $key['author_name']; ?></h5>
</div>
<?php
}
}
}
?>`
Related
So i want to place this code below
$keyword="";
if (isset($_POST['searchpakaian'])) {
$keyword = $_POST['searchpakaian'];
}
$query = mysqli_query($conn,"SELECT * FROM tbl_pakaian WHERE nama_pakaian LIKE '%".$keyword."%' ORDER BY id ASC limit 0,6");
$hitung_data = mysqli_num_rows($query);
if ($hitung_data > 0) {
while ($row = mysqli_fetch_array($query)) {
?>
<div class="col">
<div class="card h-100">
<div class="card-img-wrap ">
<img src="img/Galeri/pakaian/<?php echo $row['gambar_pakaian'] ?>" class="card-img-top" alt="...">
</div>
<div class="card-body">
<h5 class="card-title"><?php echo $row['nama_pakaian'] ?></h5>
<p class="card-text "><?php echo $row['keterangan_pakaian'] ?></p>
</div>
</div>
</div>
<?php } } else { ?>
<center><h4>Tidak Ada Data</h4></center>
<?php } ?>
On if conditional statement
if($req == 'something'){
}
Here is my full code
<?php
include '../koneksi.php';
$req = $_REQUEST['req'];
$keyword="";
if (isset($_POST['searchpakaian'])) {
$keyword = $_POST['searchpakaian'];
}
$query = mysqli_query($conn,"SELECT * FROM tbl_pakaian WHERE nama_pakaian LIKE '%".$keyword."%' ORDER BY id ASC limit 0,6");
$hitung_data = mysqli_num_rows($query);
if ($hitung_data > 0) {
while ($row = mysqli_fetch_array($query)) {
?>
<div class="col">
<div class="card h-100">
<div class="card-img-wrap ">
<img src="img/Galeri/pakaian/<?php echo $row['gambar_pakaian'] ?>" class="card-img-top" alt="...">
</div>
<div class="card-body">
<h5 class="card-title"><?php echo $row['nama_pakaian'] ?></h5>
<p class="card-text "><?php echo $row['keterangan_pakaian'] ?></p>
</div>
</div>
</div>
<?php } } else { ?>
<center><h4>Tidak Ada Data</h4></center>
<?php } ?>
If i just copy the code to the if conditional statement code, it just say 'unexpected end of file'
What should i do?
The syntax error are vey simple to find just confirm that when you copy inside the if, dont repeat php keys as and the echo finish it with ;
<?php echo('hello world'); ?>
OR the same echo but simple deleting the php tag by = simbol (only work with echo)
<?= 'hello world' ?>
I'm creating a site that lists custom userbars in a forum, and am using both PHP and SQL to achieve this easily.
My question is:
If a field is listed 'active' for the group, how do I go about adding a class specifically for that, to enable a glow around the Bootstrap card?
Here's my current code (forgive me if it's terrible)
<div class="cards">
<?php
include_once("assets/php/db.php");
$sql = "SELECT ubFilename, ubGroupName, ubGroupOwner, ubOwnerLink, isOfficial, isActiveGroup FROM UBSUserbars ORDER BY ubGroupName";
$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
while( $record = mysqli_fetch_assoc($resultset) ) {
?>
<div class="col-sm-4 col-card">
<img src="i/OGUsers/<?php echo $record['ubFilename']; ?>">
<hr>
<h4 class="group"><?php echo $record['ubGroupName']; ?></h4>
<span>Owner: <a class="owner" href="<?php echo $record['ubOwnerLink']; ?>"><?php echo $record['ubGroupOwner']; ?></a></span>
</div>
<?php } ?>
</div>
You can use a ternary operator like this:
<div class="cards">
<?php
include_once("assets/php/db.php");
$sql = "SELECT ubFilename, ubGroupName, ubGroupOwner, ubOwnerLink, isOfficial, isActiveGroup FROM UBSUserbars ORDER BY ubGroupName";
$resultset = mysqli_query($conn, $sql);
while( $record = mysqli_fetch_assoc($resultset) ) {
?>
<div class="col-sm-4 col-card <?php $record['isActiveGroup'] === 'yes' ? 'ADD-CLASS-HERE' : ''; ?>">
<img src="i/OGUsers/<?php echo $record['ubFilename']; ?>">
<hr>
<h4 class="group"><?php echo $record['ubGroupName']; ?></h4>
<span>Owner: <a class="owner" href="<?php echo $record['ubOwnerLink']; ?>"><?php echo $record['ubGroupOwner']; ?></a></span>
</div>
<?php } ?>
</div>
You can change the value of 'yes' to the value of the active group that is saved on your database.
I have the following script for showing posts and liking them, but if I like one post it likes all the posts on the page, I can't think of another way to do it, can anyone give me some advice?
<?php
if ($sort == 1){
$result = $conn->query("SELECT * FROM posts ORDER BY date DESC LIMIT 4 ");
}
elseif($sort == 2)
{
$result = $conn->query("SELECT * FROM posts WHERE date > NOW() - INTERVAL 24 HOUR ORDER BY likes DESC");
}
elseif($sort == 3)
{
$result = $conn->query("SELECT * FROM posts ORDER BY likes DESC");
}
if ($result->num_rows > 0) :
while($row = mysqli_fetch_assoc($result)) : ?>
<div class="card mb-4">
<img class="card-img-top" src="<?php echo $row['image1'] ?>" alt="Card image cap">
<div class="card-body">
<h2 class="card-title"><?php print title; ?></h2>
<p class="card-text"><?php print text; ?></p>
Read More →
</div>
<div class="card-footer text-muted">
Posted on <?php print $row['date'] ?> by
<?php print $row['author']; ?>
<?php
$id=$row['id'];
if($_POST['like']) {
$update = "UPDATE posts set `likes` = `likes`+1 where `id` ='$id'";
if ($conn->query($update) === TRUE) {
} else {
echo "Error updating record: " . $conn->error;
}
} ?>
<form action="" method="POST">
<button type = "submit" value = "like" name='like'style="font-size:24px"><?php echo $row['likes']; ?><i class="fa fa-thumbs-o-up"></i>
</form>
</div>
</div>
<?php endwhile; endif; ?>
Your while loop contains the update query so your code should be change like this.
in order to get the id to like you just need to use a hidden field to post that id like in this code
<?php
if($_POST['like']) {
$id=$POST['id'];
$update = "UPDATE posts set `likes` = `likes`+1 where `id` ='$id'";
if ($conn->query($update) === TRUE) {
} else {
echo "Error updating record: " . $conn->error;
}
} ?>
<?php
if ($sort == 1){
$result = $conn->query("SELECT * FROM posts ORDER BY date DESC LIMIT 4 ");
}
elseif($sort == 2)
{
$result = $conn->query("SELECT * FROM posts WHERE date > NOW() - INTERVAL 24 HOUR ORDER BY likes DESC");
}
elseif($sort == 3)
{
$result = $conn->query("SELECT * FROM posts ORDER BY likes DESC");
}
if ($result->num_rows > 0) :
while($row = mysqli_fetch_assoc($result)) : ?>
<div class="card mb-4">
<img class="card-img-top" src="<?php echo $row['image1'] ?>" alt="Card image cap">
<div class="card-body">
<h2 class="card-title"><?php print title; ?></h2>
<p class="card-text"><?php print text; ?></p>
Read More →
</div>
<div class="card-footer text-muted">
Posted on <?php print $row['date'] ?> by
<?php print $row['author']; ?>
<form action="" method="POST">
<input name="id" type="hidden" value="<?php echo $row['id']; ?>">
<button type = "submit" value = "like" name='like'style="font-size:24px"><?php echo $row['likes']; ?><i class="fa fa-thumbs-o-up"></i>
</form>
</div>
</div>
<?php endwhile; endif; ?>
I want to give a little style for my comment section, here is the code without any css, but i want to give it some style
<div id="comments">
<?php
$sql = "SELECT * FROM comments ORDER BY id LIMIT 2";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<p>";
echo $row['author'];
echo "<br>";
echo $row['message'];
echo "<br>";
echo $row['time'];
echo "</p>";
}
} else {
echo "there are no comments!";
}
?>
</div>
<button>More comments</button>
and down here is my html section of which i want to appear while handling my php comments where USER, COMMENT and TIME are are stored in my database, here is the html, how can i echo the above variables into the below html tags ?
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5>USER</h5>
</div>
<div class="media-body response-text-right">
<p>COMMENT</p>
<ul>
<li>TIME</li>
<li>Reply</li>
</ul>
</div>
</div>
You can do like this:
<div id="comments">
<?php
$sql = "SELECT * FROM comments ORDER BY id LIMIT 2";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) { ?>
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5><?php echo $row['author']; ?></h5>
</div>
<div class="media-body response-text-right">
<p><?php echo $row['message']; ?></p>
<ul>
<li><?php echo $row['time']; ?> </li>
<li>Reply</li>
</ul>
</div>
</div>
<?php }
} else {
echo "there are no comments!";
}
?>
</div>
hope it will help you.
I'm about to display comments on a page, everything works fine but I want to display the comments from the user first. So, how do I display the user comments from the user_id first? I'm using a while loop to display the comments.
I would be very grateful if anyone could help me. :)
<?php
$sql = "SELECT * FROM `comments` WHERE `post_id`=$pid AND `active`=1 ORDER BY ups-downs DESC";
$result = $mysqli->query($sql);
$count = $result->num_rows;
if($count == 1){
$counttext = "1 Comment";
}else{
$counttext = $count ." Comments";
}
?>
<div class="media-area media-area-small">
<h3 class="text-center"><?php echo $counttext; ?></h3>
<?php
while($row = $result->fetch_assoc()){
$uid = (int)$row["user_id"];
$user = get_user_info($mysqli,$uid);
?>
<div class="media">
<a class="pull-left" href="#">
<div class="avatar">
<img class="media-object" src="<?php echo $user["picture"]; ?>" alt="...">
</div>
</a>
<div class="media-body">
<table width="100%">
<tr valign="middle">
<td align="left"><h4 class="media-heading text-left"><?php echo fb_clear_name($mysqli, $user["id"]); ?></h4></td>
<td align="right"><h6 class="pull-right text-muted"><?php echo $row["ups"] - $row["downs"]; ?> bits</h6></td>
</tr>
</table>
<p><?php echo htmlentities($row["content"]); ?></p>
<div class="media-footer">
<i class="fa fa-reply"></i> Reply
<a href="#" class="pull-right text-muted">
<?php echo $row["timestamp"]; ?>
</a>
</div>
</div>
</div>
<?php
}
?>
Greetings
You can prefilter the data. For example use
$user_comments=array();
$other_comments=array();
while($row = mysql_fetch_assoc($result))
{
if($row['user']==$current_user)
{
$user_comments[]=$row;
}
else
{
$other_comments[]=$row;
}
}
$comments=array_merge($user_comments,$other_comments);
Then you can display the information the way you want to.