check if specific row exists mysql - php

I have a mysqli query which fetches all the images from the table(I have 5 images that I display). I am using a jquery slider to display them. The problem is if there are no 5 images I see blank page like if the user uploads just two images then the rest three thumbnails will be empty and when you click on them it shows empty area. I don't want that happen so how do I only show the thumbnail if the image exists instead of showing empty thumbnail?
I tried the below but this doesn't work. I just need to see if image_one exists then show the thumbnail and the same for the rest of the images.
<?php
$stmt = $mydb->prepare("SELECT * FROM table where title = ? AND id = ? limit 1 ");
$stmt->bind_param('ss', $title, $id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$path = 'images/';
?>
<div id="slides">
<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_one']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_one']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_two']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_two']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?> <div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_three']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_three']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_four']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_four']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_five']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_five']?>"/></a></div><?php };?>
</div>
<div id="slide_menu">
<ul id="slide"> <!-- This is the thumbnail area -->
<li class="fbar"> </li>
<?php if($result->num_rows > 0){?><li class="menuItem"><img src="<?php echo $path.$row['image_one']?>" /></li><?php }; ?>
<?php if($result->num_rows > 0){?><li class="menuItem"><img src="<?php echo $path.$row['image_two']?>" /></li><?php }; ?>
<?php if($result->num_rows > 0){?><li class="menuItem"><img src="<?php echo $path.$row['image_three']?>" /></li><?php }; ?>
<?php if($result->num_rows > 0){?><li class="menuItem"><img src="<?php echo $path.$row['image_four']?>" /></li><?php }; ?>
<?php if($result->num_rows > 0){?><li class="menuItem"><img src="<?php echo $path.$row['image_five']?>" /></li><?php }; ?>
</ul>
</div>

we can not see your database design so look at your default value for
image_one and so on
The row count is allways 0 or 1 (because of Limit 1)
Important are the fields image_one to image_five
These fields are always present, regardless of whether they are empty or with file names are filled.
depending on the default value test it
for example one of
if ($row['image_one'] > '') {
if ($row['image_one'] > null {
put an if arround building html.
<?php if($result->num_rows > 0){?>
<?php if ($row['image_one'] > '')
{?>
<li class="menuItem">
<img src="<?php echo $path.$row['image_one']?>" />
</li>
<?php
}?>
.... next 4 other tests
<?php if ($row['image_two'] > '')
....
<?php } // END__$result->num_rows > 0 ?>

try something like
...
<ul id="slide"> <!-- This is the thumbnail area -->
<li class="fbar"> </li>
<?php
foreach ( $row as $element => $val){
if(isset($val)) {
print "<li class='menuItem'><a href=''><img src=".$path.$val."/></a></li>";
}
}
?>
</ul>
...
and the same for the 1st (<div id="slides">) block

Im not 100% sure for pdo but its not too far away
<?php
$sql = "SELECT * FROM table where title = $title AND id = $id limit 1 ";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
?>
<div class="slide">
<a class="fancybox" href="images/<?php echo $row['DB-IMAGE-PATH-HERE']; ?>" data-fancybox-group="gallery">
<img class="cloudzoom appsld" src="images/<?php echo $row['DB-IMAGE-PATH-HERE']; ?>"/>
</a>
</div>
<?php } // end while loop ?>
</div> <!-- end id="slides" -->

Related

How to load more comments without refreshing the browser

Am working on comment system, but am stuck here, after loading my PHP on my browser, it shows me exactly 2 comments that I want to see, when I click the link it fetches the other 2 comments as I programmed it on jQuery, but after that the button disappears and I cant load more comments.
Please help!
Here is my code,
<script>
$(document).ready(function() {
var commentCount = 2;
$("button").click(function() {
commentCount = commentCount + 2;
$("#comments").load("2.php", {
commentNewCount: commentCount
});
});
});
</script>
<body>
<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!";
}
?>
<button>More comments</button>
</body>
and this down here is my load-coments.php (i decided to call it 2.php)
<?php
include 'include/dbconnect.php';
$commentNewCount = $_POST['commentNewCount'];
$sql = "SELECT * FROM comments ORDER BY id LIMIT $commentNewCount";
$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!";
}
?>
What do you mean with "button disappears"?. The load function of jquery replaces the html with the recieved html so maybe your button is in the div you're replacing. It's hard to debug your code because of the lack of indents.

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

how to add a script after showing 10 videos in a php loop

I want to add a script after showing 10 videos in my website. But I don't understand how to use an if condition for this. This is my php code which displays all the videos from my database. I want that after displaying 10 videos it display this script.
<script>
This may contain the code of chitika code.
<script>
This is PHP code.
<section class="videos">
<?php while ($res=$stmt_today->fetch()) { ?>
<?php if ($res === NULL) { ?>
<section class="box">
<a href="" class="video-box">
<img src="" width="190" height="90" alt="">
</a>
<strong class="title">Coming Soon</strong>
</section>
<?php } else {
$immg = basename($images);
$imagee = "img"."/".$immg; ?>
<section class="box" style="width:100%; padding-top:2px;">
<?php if($images!=''){?>
<a href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="<?php echo $imagee; ?>" width="190" height="90" alt="">
</a>
<?php } else {?>
<a style="margin-left:5px;"href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="http://img.youtube.com/vi/<?php echo $video_thumbnail; ?>/mqdefault.jpg" width="190" height="90" alt="">
</a>
<?php } ?>
</section> <hr>
<?php } ?>
<?php } ?>
</section>
Inside your loop you just need a simple condition and a counter.
Here's an example. This is a loop that runs 12 times using a while loop like your code.
$i is an incrementing variable ($i++ increments it's value at the end of each loop cycle). The if statement says if $i is equal to 10 then do something. In this case it echos "10 records reached".
Make sure that when you define your counter first ($i = 1) it is before the while loop starts.
<?php
$i = 1;
while ($i <= 12) {
echo "record $i<br/>";
if ($i == 10) {
echo "10 records reached<br/>";
}
$i++;
}
?>
So adapting that thought process to your code would look like this:
<section class="videos">
<?php $i = 1; // This is where the counter is defined ?>
<?php while ($res=$stmt_today->fetch()) { ?>
<?php if ($res === NULL) { ?>
<section class="box">
<a href="" class="video-box">
<img src="" width="190" height="90" alt="">
</a>
<strong class="title">Coming Soon</strong>
</section>
<?php } else {
$immg = basename($images);
$imagee = "img"."/".$immg; ?>
<section class="box" style="width:100%; padding-top:2px;">
<?php if($images!=''){?>
<a href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="<?php echo $imagee; ?>" width="190" height="90" alt="">
</a>
<?php } else {?>
<a style="margin-left:5px;"href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="http://img.youtube.com/vi/<?php echo $video_thumbnail; ?>/mqdefault.jpg" width="190" height="90" alt="">
</a>
<?php } ?>
</section><hr>
<?php } ?>
<?php if ($i == 10) { // This is the new condition ?>
<script>
This may contain the code of chitika code.
<script>
<?php } // condition ends here ?>
<?php $i++ // increment the counter ?>
<?php } ?>
</section>
If you don't want to include your null values in the count (your 'coming soon' videos), you can simply move the counter increment $i++ up into the else { ... } portion of your condition.
<?php if (res === NULL) {
...
} else {
...
$i++;
} ?>

In PHP fetch content and truncate the string or variable, till 3rd breakline then display

//In PHP fetch content and truncate the string or variable, till 3rd breakline then display
//when i use this method the entire div deallocated and the its not displaying properly...hope the fetch content contain some image tags too // ...
<div class="mid-blks-cont">
<!-- Block1 -->
<div class="mid-block-1 boxgrid caption">
<?php
foreach ($querypost as $row) {
$content = $row->post_content;
$pcontent_overview = (strlen($content) > 300) ? substr($content,0,300).'... Read More' : $content;
if($img == "No Image Uploaded" ) {
?>
<img alt="" src="<?php echo base_url(); ?>assets/img/samples/sample1.jpg"style="width: 391px; height:231px"/>
<?php } else { ?>
<img alt="" src="<?php echo base_url(); ?>uploads/<?php echo $row->post_media ;?>" style="width: 391px; height:231px" />
<?php }?>
<h4 class="cat-label cat-label2"><?php echo $row->category;?></h4>
<div class="cover boxcaption">
<h3><?php echo $row->post_title;?><span class="topic-icn"><?php echo $row->comment_count;?></span></h3>
<p> <?php echo $pcontent_overview;?>....</p>
MORE <i class="fa fa-angle-double-right"></i>
</div>
<?php } ?>
</div>
</div>

Categories