How Insert PHP syntax on if conditional with html code inside - php

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' ?>

Related

Changing class for subsequent rows

I have this piece of code regarding a carousel. I need the first card to have the "active" class but not the following ones or the carousel wont slide. How can i do that?
Here's my code...
$sql = "select wine.wine_name, wine.id, wine.wine_img,
region.region_name, winetype.winetype_name from wine, region, winetype
where wine.region_id = region.id and wine.winetype_id = winetype.id
and sponsored = 1;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<div class="carousel-item col-md-4 active">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src='.$row["wine_img"].' alt="Card image cap">
<div class="card-body">
<h4 class="card-title">'.$row["wine_name"].'</h4>
<p class="card-text">'.$row["winetype_name"].' - '.$row["region_name"].'</p>
</div>
</div>
</div>';
}
} else {
echo "ERRO!";
}
you can use a counter to set the active class.
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$count_results = 0;
while($row = $result->fetch_assoc()) {
echo '<div class="carousel-item col-md-4 ' . ($count_results == 0 ? 'active' : '') . '">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src='.$row["wine_img"].' alt="Card image cap">
<div class="card-body">
<h4 class="card-title">'.$row["wine_name"].'</h4>
<p class="card-text">'.$row["winetype_name"].' - '.$row["region_name"].'</p>
</div>
</div>
</div>';
$count_results++;
}
} else {
echo "ERRO!";
}
You could just have a field with the extra class part set to active and then reset it at the end of the loop...
// output data of each row
$class = ' active';
while($row = $result->fetch_assoc()) {
echo '<div class="carousel-item col-md-4' .$class. '">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src='.$row["wine_img"].' alt="Card image cap">
<div class="card-body">
<h4 class="card-title">'.$row["wine_name"].'</h4>
<p class="card-text">'.$row["winetype_name"].' - '.$row["region_name"].'</p>
</div>
</div>
</div>';
$class = '';
}
This should do the trick:
while($row = $result->fetch_assoc()) {
$active = isset($active) ? '' : 'active';
echo '<div class="carousel-item col-md-4 ' . $active . '">
...
}
The variable $active will only be unset (undefined or null) on the first iteration. Then, on the other iterations, the variable will be defined and not null (since it's an empty string) so then we simply set it as an empty string again.
I would make a counter and simply check if you need to add the class. Of course, since you only need it once, you can use a boolean.
<?php
$sql = "select wine.wine_name, wine.id, wine.wine_img,
region.region_name, winetype.winetype_name from wine, region, winetype
where wine.region_id = region.id and wine.winetype_id = winetype.id
and sponsored = 1;";
$result = $conn->query($sql);
?>
<?php if ($result->num_rows > 0): ?>
<?php $first = true; ?>
<?php while ($row = $result->fetch_assoc()): ?>
<div class="carousel-item col-md-4 <?= $first ? 'active' : '' ?>">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src=<?= $row["wine_img"] ?> alt="Card image cap">
<div class="card-body">
<h4 class="card-title"><?= $row["wine_name"] ?></h4>
<p class="card-text"><?= $row["winetype_name"] ?> - <?= $row["region_name"] ?></p>
</div>
</div>
</div>
<?php $first = false; ?>
<? endwhile; ?>
<?php else: ?>
<span>Error!</span>
<?php endif; ?>
I've also taken the liberty of taking all of your HTML out of the single echo. This is much cleaner :)

How to fetch comments from mysql database without destroying my html tags

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.

fetching data from php Admin for display

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
}
}
}
?>`

Displaying data from while loop into html code

I need tips or direction on how can I display data from mysql using echo. But I want to display it in html code. I want to display $row["title"] of first title in mysql instead title1 and $row["content"] of first content in mysql instead content1 and do that for all 3 divs. php code works fine I just can't figure out how to make that possible.
<div class="carousel-inner" style="background-size:cover;">
<div class="item active">
<img src="img/road1.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>title1</h2>
<p>content1</p>
</div>
</div>
<div class="item">
<img src="img/road2.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>title2</h2>
<p>content2</p>
</div>
</div>
<div class="item">
<img src="img/road3.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>title3</h2>
<p>content3</p>
</div>
</div>-->
<?php
session_start();
include_once("db.php");
$sql = "SELECT * FROM news";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_assoc($query)) {
echo "<h2>" . $row["title"] . "</h2>";
echo "<p>" . $row["content"] . "</p>";
}
} else {
echo "0 results";
}
?>
You're almost there. Just move the html into the echo of the while loop.
echo '<div class="carousel-inner" style="background-size:cover;">';
$counter = 1;
while($row = mysqli_fetch_assoc($query)) {
echo '
<div class="item ' . ($counter == 1 ? 'active' : '') . '">
<img src="img/road{$counter}.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>' . $row["title"] . '</h2>
<p>' . $row["content"] . '</p>
</div>
</div>';
$counter++;
}
echo '</div>';
The only issue is the image, realistically you'd save the image in the database with the title and content then use the same method but for this simple case lets just use a counter
please note that I change your entire code a little bit to make the desired results...
<div class="carousel-inner" style="background-size:cover;">
<?php
session_start();
include_once("db.php");
$sql = "SELECT * FROM news";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_assoc($query)) { ?>
<div class="item active">
<img src="img/road1.jpg">
<div class="carousel-caption d-none d-md-block">
<h2><?php echo $row["title"]; ?></h2>
<p><?php echo $row["content"]; ?></p>
</div>
</div>
<?php
}
} else {
echo "0 results";
}
?>
Also note that I'm repeating just the first image... You need an extra on planning to determine how to handle images in code and then update this one.

How do I sort elements in a while loop?

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.

Categories