While loop inside if else statement not working - php

I have this code snippet that is failing to work, don't know what am doing wrong, am getting content from MYSQL database and using if else statement with while loop to echo the contents.
<?php
if ($row_item['cat_item_id'] == ''){
echo '<div class="col-sm-6 col-md-3"">
</p>
<p>
No Item To Show
</p>
</div>
</div>';
}
else {
while ($row_item = mysql_fetch_assoc($item)){
echo
'
<div class="col-sm-6 col-md-3" style="'.$row_item['display'].'">
<div class="thumbnail">
<img src="myaccount/user_data/'.$row_item['file_name'].'" />
</div>
<div class="caption">
<h3>'.$row_item['item_name'].'</h3>
<p>
<a href="item_detail.php?item='.$row_item['cat_item_id'].'" class="btn btn-primary" role="button">
View Item
</a>
<a href="contact_seller.php?contact='.$row_item['cat_item_id'].'" class="btn btn-default" role="button">
Contact Owner
</a>
</p>
</div>
</div>';
}
}
?>
Help me figure out what am not doing right.

You have to call while ($row_item = mysql_fetch_assoc($item))
first place.
Otherwise the $row_items doesn't get initialized properly.
So you put all inside
while ($row_item = mysql_fetch_assoc($item))
And check for the if-else conditions inside the while:
if ($row_item['cat_item_id'] == '') // this goes inside the while. Otherwise $row_item is not initialized properly

You are checking if
if ($row_item['cat_item_id'] == ''){
before you actually call
while ($row_item = mysql_fetch_assoc($item)){
To get $row_item
Try changing it to something like this:
while ($row_item = mysql_fetch_assoc($item)){
if ($row_item['cat_item_id'] == ''){
echo '<div class="col-sm-6 col-md-3"">
</p>
<p>No Item To Show</p></div></div>';
}else {
echo
'
<div class="col-sm-6 col-md-3" style="'.$row_item['display'].'">
<div class="thumbnail">
<img src="myaccount/user_data/'.$row_item['file_name'].'" />
</div>
<div class="caption">
<h3>'.$row_item['item_name'].'</h3>
<p>
<a href="item_detail.php?item='.$row_item['cat_item_id'].'" class="btn btn-primary" role="button">
View Item
</a>
<a href="contact_seller.php?contact='.$row_item['cat_item_id'].'" class="btn btn-default" role="button">
Contact Owner
</a>
</p>
</div>
</div>';
}
}

Try mysql_fetch_array
while ($row_item = mysql_fetch_array($item)){
echo
'
<div class="col-sm-6 col-md-3" style="'.$row_item['display'].'">
<div class="thumbnail">
<img src="myaccount/user_data/'.$row_item['file_name'].'" />
</div>
<div class="caption">
<h3>'.$row_item['item_name'].'</h3>
<p>
<a href="item_detail.php?item='.$row_item['cat_item_id'].'" class="btn btn-primary" role="button">
View Item
</a>
<a href="contact_seller.php?contact='.$row_item['cat_item_id'].'" class="btn btn-default" role="button">
Contact Owner
</a>
</p>
</div>
</div>';
}

Related

append not inserting values

here my problem is the values that are selected through append are not getting inserted in to the post value..here am pasting my code
<div id="update_new_<?php echo $value->id;?>" class="update_new_<?php echo $value->id;?>">
<div class="form-group mb0" style="margin-bottom: 0px">
<label for="field-1"
class="col-sm-4 control-label">Other Document</label>
<div class="col-sm-5">
<div class="fileinput fileinput-new" data-provides="fileinput">
<?php
if (!empty($value->documents)) {
$uploaded_file = json_decode($value->documents);
}
if (!empty($uploaded_file)):foreach ($uploaded_file as $v_files_image): ?>
<div class="">
<input type="hidden" name="fileName[]"
value="<?php echo $v_files_image ?>">
<span class=" btn btn-default btn-file">
<span class="fileinput-filename"> <?php echo $v_files_image ?></span>
×
</span>
<strong>
<a href="javascript:void(0);" class="RCF"><i
class="fa fa-times"></i> Remove</a></strong>
<p></p>
</div>
<?php endforeach; ?>
<?php else: ?>
<span class="btn btn-default btn-file"><span
class="fileinput-new">Select File</span>
<span class="fileinput-exists"><?= lang('change') ?></span>
<input type="file" name="files[]">
</span>
<span class="fileinput-filename"></span>
<a href="#" class="close fileinput-exists" data-dismiss="fileinput"
style="float: none;">×</a>
<?php endif; ?>
</div>
<div id="msg_pdf" style="color: #e11221"></div>
</div>
<div class="col-sm-3">
<strong><a href="javascript:void(0);" id="update_more" onclick="add_field('<?php echo $value->id;?>');" u_id="<?php echo $value->id;?>" class="addCF update_more_<?php echo $value->id;?>"><i
class="fa fa-plus"></i> Add More
</a></strong>
</div>
</div>
</div>
here is my script please have a look
<script type="text/javascript">
var maxAppends = 0;
function add_field(id)
{
var update_new = $('<div class="form-group" style="margin-bottom: 0px">\n\
<label for="field-1" class="col-sm-4 control-label">Documentsss</label>\n\
<div class="col-sm-5">\n\
<div class="fileinput fileinput-new" data-provides="fileinput">\n\
<span class="btn btn-default btn-file"><span class="fileinput-new">Select file</span><span class="fileinput-exists" >Change</span><input type="file" name="files[]" ></span> <span class="fileinput-filename"></span>×</div></div>\n\<div class="col-sm-2">\n\<strong>\n\<i class="fa fa-times"></i> Remove</strong></div>');
maxAppends++;
$(".update_new_"+id).append(update_new);
}
</script>
Here while clicking to add more fields its coming correctly but the values selected in that field is not getting while passing the value through post and i have a doubt that if it is caused by using dynamic way off adding the data.
The problem i figured out is the problem is the <div id="update_new_<?php echo $value->id;?>"> is inside a foreach loop so how can we done this with in a foreach loop.
In your script, change .update_new_ to #update_new_

how to give a if else here

i want to include a else statement after the completion of the block after the second if for category equal to helathcare ....now i want another else for finance but i am not getting where excatly to place that else
i have a confusion on where to keep the else for finance and 2 more categories
now whenever i try to place the one else at the last goes unrecable to its if statement i donno its very confusing as to where to place 2 more else if statments for 2 more categories
<?php
global $row2;
if(isset($_POST['category']))
{
if($_POST['category']== 'Healthcare')
{
$query = "select *from event where category = 'Healthcare';";
$result=mysqli_query($conn,$query)or die(mysqli_error($conn));
while($row2= mysqli_fetch_array($result))
{
?>
<div class="events events-full event-list">
<div class="container">
<div class="row">
<div class="col-md-9 col-sm-8">
<!--Blog Post Start-->
<div class="blog-post">
<div class="post-thumb">
<div class="link-wrap"> <i class="fa fa-search"></i> <i class="fa fa-link"></i> </div>
<img src="images/gallery/<?php echo $row2['event_image']?>" alt='user'></div>
<div class="event-text">
<div class="event-counter"></div>
<h4> <?php echo($row2['title']); ?> </h4>
<p><?php echo($row2['descrption']); ?></p>
<p><span class="glyphicon glyphicon-map-marker"><?php echo($row2['location']); ?></span></p>
<p><span class="glyphicon glyphicon-grain"><?php echo($row2['organizer']); ?></span></p>
<a class="nd" href="">
<form action="eventdetail.php" method="post">
<input type='hidden' value="<?php echo $row2['id']; ?>" name='id'/>
<button type="submit" class="btn btn-primary" name="detail" value=”detail”>Event Detail</button>
</div>
</a> </div>
</div>
</form>
<!--Blog Post End-->
<?php }
}
}
else
{
global $row2;
$query = "select *from event;" ;
$result=mysqli_query($conn,$query)or die(mysqli_error($conn));
while($row2= mysqli_fetch_array($result))
{
?>
<div class="events events-full event-list">
<div class="container">
<div class="row">
<div class="col-md-9 col-sm-8">
<!--Blog Post Start-->
<div class="blog-post">
<div class="post-thumb">
<div class="link-wrap"> <i class="fa fa-search"></i> <i class="fa fa-link"></i> </div>
<img src="images/gallery/<?php echo $row2['event_image']?>" alt='user'></div>
<div class="event-text">
<div class="event-counter"></div>
<h4> <?php echo($row2['title']); ?> </h4>
<p><?php echo($row2['descrption']); ?></p>
<p><span class="glyphicon glyphicon-map-marker"><?php echo($row2['location']); ?></span></p>
<p><span class="glyphicon glyphicon-grain"><?php echo($row2['organizer']); ?></span></p>
<a class="nd" href="">
<form action="eventdetail.php" method="post">
<input type='hidden' value="<?php echo $row2['id']; ?>" name='id'/>
<button type="submit" class="btn btn-primary" name="detail" value=”detail”>Event Detail</button>
</div>
</a> </div>
</div>
</form>
<!--Blog Post End-->
<?php } } ?>
?>
You can't put multiple else blocks along with the if block. However, you can use multiple(as many as you want) elseif/else if blocks along with the if block, like this:
if($_POST['category'] == 'Healthcare'){
...
}elseif($_POST['category'] == 'Finance'){
...
}elseif($_POST['category'] == '...'){
...
}else{
...
}
Here's the reference: http://php.net/manual/en/control-structures.elseif.php
Part of your problem seems to be poor formatting. If you lined up your open and close tags for your if statements I think you would be able to see this better. Having said that, you can add any elseif/if clauses for the category if where I marked below:
<!--Blog Post End-->
<?php
} // closes while loop
} else{ } // closes healthcare if, add extra else here
} // closes POST if
else
{

how to count specific coumns row data in mysql without any conditons in php pdo?

I have table "POST_DATA" and consists of total (13) fields. like userID, user_name,pass, cam_name,cam_model etc.. whenever a user registers on my website some fields get updated like (userid,user_name,pass) and other fields are empty.
But the problem is when I retrieve data from another PHP file using rowcount() on specific columns it returns "1" every time, I want to "0" if no data found on that specific fields like(cam_img,cam_name,cam_model etc..) still it not get updated.
what is the problem...
here is my code:
$stmt = $index_home->getDetails();
if($stmt->rowCount() > 0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
?>
<div class="row">
<div class="span9" style="float:right;">
<ul class="thumbnails list row" style="display:block;" >
<li>
<div class="thumbnail">
<div class="row">
<div class="span3"> <span class="sale tooltip-test"> <i class="icon-gift font24"></i> <br>
New</span> <img alt="" src="user_images/<?php echo $row['cam_img']; ?>">
</div>
<div class="span6"> <span class="prdocutname" style="font-size:20px;" ><?php echo ucfirst($cam_name) ." " .$cam_model?></span></div>
<div class="span6"> <span class="prdocutname" ><?php echo "Rent: " . $cam_rent . "/- (per day)"?></span></div>
<div class="span6"> <span class="prdocutname" ><?php echo "Mobile: " . $mobile ?></span></div>
<div class="span6">
<button data-original-title="Cart" class="btn btn-orange tooltip-test"> <i class="icon-shopping-cart icon-white"></i> </button>
<button data-original-title="Wishlist" class="btn btn-orange btn-small tooltip-test"> <i class="icon-heart icon-white"></i> </button>
<button data-original-title="Compare" class="btn btn-orange btn-small tooltip-test"> <i class="icon-refresh icon-white"></i> </button>
</div>
Detail view </div>
</div>
</div>
</li>
</ul>
</div>
<?php
}
}
else
{
?>
<div class="col-xs-12">
<div class="alert alert-warning">
<span class="glyphicon glyphicon-info-sign"></span> No Data Found ...
</div>
</div>
<?php
}
?>
And getDetails() implementation in USER class
public function getDetails()
{
try
{
$stmt = $this->conn->prepare('SELECT userID, user_name, cam_name, cam_model, cam_rent, cam_img, mobile FROM post_data ORDER BY userID DESC');
$stmt->execute();
return $stmt;
}
catch(PDOException $ex)
{
echo $ex->getMessage();
}
}
I want to restrict rowCount() to return "1" if no data found in that particular columns.

How to display hidden data of one div to another div?

I am working on a project where I came across where when I click plus sign the data will hide and the appear in the div next to it.
here is the code:
<fieldset class="col-md-4" >
<legend>Services</legend>
<div class="col-md-12" >
<?php
$id = 0;
foreach ($servicesname as $val) {
$id++;
?>
<div class="col-md-12" style="font-size: 16px;" id="itemservices<?php echo $id ?>">
<span style="float:left;" ><?php echo $val[0]['servicename']; ?></span>
<a style="float:right;" href onclick="return addSrvToCart('itemservices<?php echo $id ?>')" >
<strong>&#8377 <?php echo $val[0]['amount']; ?></strong>
<span class="glyphicon glyphicon-plus-sign" id="id_<?php echo $id; ?>"></span>
</a>
</div>
<?php } ?>
</div>
</fieldset>
and the code for next div:
<fieldset class="col-md-4" >
<legend>Cart</legend>
<div style="list-style:none;" class="no-left-padding" id="cart">
<div class="col-md-12" >
</div>
</div>
<div class="col-sm-12 no-right-padding" style="background-color:#f3f0f0; padding-top:6px; border:1px solid#ccc">
<label class="pull-right">/*what should i write here to show the sum */: ₨ </label>
</div>
<button style="margin-top:10px;" class="btn btn-primary btn-sm pull-right" onclick="bookNowAfterFilter()">Book Now</button>
</fieldset>
And This the Jquery code:
<script>
function addSrvToCart(elem){
alert($('#' + elem).html());
$('#' + elem).hide();
//return false;
//what should i write here the show that hidden div
document.getElementById('summery);
return false;
}
</script>
This is the picture and I want to display the data in the Cart div and show the amount
You need to change in both HTML and Script to achieve the result
Try like this
HTML
<fieldset class="col-md-4" >
<legend>Services</legend>
<div class="col-md-12" >
<?php
$id = 0;
foreach ($servicesname as $val) {
$id++;
?>
<div class="col-md-12" style="font-size: 16px;" id="itemservices<?php echo $id ?>">
<span style="float:left;" ><?php echo $val[0]['servicename']; ?></span>
<a style="float:right;" onclick=" addSrvToCart('itemservices<?php echo $id ?>')" >
&#8377<strong> <?php echo $val[0]['amount']; ?></strong>
<span class="glyphicon glyphicon-plus-sign" id="id_<?php echo $id; ?>"></span>
</a>
</div>
<?php } ?>
</div>
</fieldset>
and the code for next div:
<fieldset class="col-md-4" >
<legend>Cart</legend>
<div style="list-style:none;" class="no-left-padding">
<div class="col-md-12" id="cart" >
</div>
</div>
<div class="col-sm-12 no-right-padding pull-right" style="background-color:#f3f0f0; padding-top:6px; border:1px solid#ccc">
Total: ₨ <label class="" id="sumAmount">0</label>
</div>
<button style="margin-top:10px;" class="btn btn-primary btn-sm pull-right" onclick="bookNowAfterFilter()">Book Now</button>
</fieldset>
Script:
function addSrvToCart(elem){
var div=$('#' + elem);
div.hide();
$('#cart').append(div.html());
var total = parseInt(div.find('strong').html()) + parseInt($('#sumAmount').html());
$('#sumAmount').html(total);
}
It will produce output as
I think it will help you.

I want to repeat row after 2 columns where datas are retrieved dynamically in php

Upon doing this I am gating the data in an un alligned manner. I am getting 2 datas but the third data is coming under second data and 4th below first data but 3rd and forth are not in a single row there placed one after the other.
My View
<div class="container col-md-9">
<?php
foreach($unidet as $tempuni)
{
?>
<div class="service-items col-md-6">
<h4 class="title-normal"><img class="img-responsive thumb" src="images/service/service-icon1.png" alt=""><strong><?php echo $tempuni['name'] ?></strong></h4>
<div class="row">
<div class="col-sm-6 service-item-img">
<img class="img-responsive thumbnail" src="admin/<?php echo $tempuni['image'];?>" alt="">
</div>
<div class="col-sm-6 service-item-content">
<p><?php echo $tempuni['description'];?></p>
<button type="button" class="btn btn-warning btn-sm" onclick="more(<?php echo $tempuni['id'];?>)">Read More</button>
</div>
</div>
</div>
<?php
}
?>
</div>
This is what I am getting
Nice solution would be to use array_chunk()
<?php foreach (array_chunk($unidet, 2) as $row): ?>
<div class="row">
<?php foreach ($row as $tempuni): ?>
<div class="service-items col-md-6">
<h4 class="title-normal"><img class="img-responsive thumb" src="images/service/service-icon1.png" alt=""><strong><?php echo $tempuni['name'] ?></strong></h4>
<div class="row">
<div class="col-sm-6 service-item-img">
<img class="img-responsive thumbnail" src="admin/<?php echo $tempuni['image'];?>" alt="">
</div>
<div class="col-sm-6 service-item-content">
<p><?php echo $tempuni['description'];?></p>
<button type="button" class="btn btn-warning btn-sm" onclick="more(<?php echo $tempuni['id'];?>)">Read More</button>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>

Categories