Php code in html not working as expected - php

I want to use this code to get results from my database and show it in html but this is not happening. Getting all weird results and am not able to fix it.
it display } ? > on my webpage at the end wonder why.And i cannot get any results from my database displayed either.
Database is proper I believe some error in html and php code(php variables in html tags).
<?php
while($row = mysqli_fetch_array($result)) {
$price = ($row['ISBN']%1000);
echo $price;
<li>
<div class="book-display-bg">
<div class="book-display-placeholder">
<div class="book-display-container">
<div class="book-display-centered">
<a href='product/taqatu-niran-min-yaumiyat-al-intifada-as-suriya-9789953892368-(105523).html' >
<img src=''$row['Image_URL_M']'' style='border: 0;' alt=''$row['Book_Title']''/>
</a>
</div>
</div>
</div>
</div>
<div class="book-display-details">
<div class="book-display-title"><p><a href='product/taqatu-niran-min-yaumiyat-al-intifada-as-suriya-9789953892368-(105523).html' title=''$row['Book_Title']'' >$row['Book_Title']</a></p></div>
<div class="book-display-author"><p>$row['Book_Author']</p></div>
<div id="ctl00_ctl00_CMSContentMasterPlaceHolder_cphContent1_ctl01_ProductSummary1_DataList1_ctl02_divStock" class="book-display-stock"></div>
<div class="book-display-price">$price</div>
<input type="submit" name="ctl00$ctl00$CMSContentMasterPlaceHolder$cphContent1$ctl01$ProductSummary1$DataList1$ctl02$btnBuyNow" value="add to basket" id="ctl00_ctl00_CMSContentMasterPlaceHolder_cphContent1_ctl01_ProductSummary1_DataList1_ctl02_btnBuyNow" title="add to basket" class="ecom-add-basket" />
</div>
<div class="clear"></div>
</li>
}
?>
Thanks in advance

You forget to add echo for the li element. You can try following:
<?php
while($row = mysqli_fetch_array($result)) {
$price = ($row['ISBN']%1000);
echo $price; ?>
<li>
<div class="book-display-bg">
<div class="book-display-placeholder">
<div class="book-display-container">
<div class="book-display-centered">
<a href='product/taqatu-niran-min-yaumiyat-al-intifada-as-suriya-9789953892368-(105523).html' >
<img src='<?php echo $row['Image_URL_M']; ?>' style='border: 0;' alt='<?php echo $row['Book_Title']; ?>'/>
</a>
</div>
</div>
</div>
</div>
<div class="book-display-details">
<div class="book-display-title"><p><a href='product/taqatu-niran-min-yaumiyat-al-intifada-as-suriya-9789953892368-(105523).html' title='<?php echo $row['Book_Title']; ?>' ><?php echo $row['Book_Title']; ?></a></p></div>
<div class="book-display-author"><p><?php echo $row['Book_Author']; ?></p></div>
<div id="ctl00_ctl00_CMSContentMasterPlaceHolder_cphContent1_ctl01_ProductSummary1_DataList1_ctl02_divStock" class="book-display-stock"></div>
<div class="book-display-price"><?php echo $price; ?></div>
<input type="submit" name="ctl00$ctl00$CMSContentMasterPlaceHolder$cphContent1$ctl01$ProductSummary1$DataList1$ctl02$btnBuyNow" value="add to basket" id="ctl00_ctl00_CMSContentMasterPlaceHolder_cphContent1_ctl01_ProductSummary1_DataList1_ctl02_btnBuyNow" title="add to basket" class="ecom-add-basket" />
</div>
<div class="clear"></div>
</li><?php
}
?>
or You can do it this way:
<?php
while($row = mysqli_fetch_array($result)) {
$price = ($row['ISBN']%1000);
echo $price;
echo '<li>
<div class="book-display-bg">
<div class="book-display-placeholder">
<div class="book-display-container">
<div class="book-display-centered">
<a href="product/taqatu-niran-min-yaumiyat-al-intifada-as-suriya-9789953892368-(105523).html" >
<img src="' . $row['Image_URL_M'] . '" style="border: 0;" alt="$row[\'Book_Title\']"/>
</a>
</div>
</div>
</div>
</div>
<div class="book-display-details">
<div class="book-display-title"><p><a href="product/taqatu-niran-min-yaumiyat-al-intifada-as-suriya-9789953892368-(105523).html" title="' . $row['Book_Title'] . '" >' . $row['Book_Title'] . '</a></p></div>
<div class="book-display-author"><p> ' .$row['Book_Author'] . '</p></div>
<div id="ctl00_ctl00_CMSContentMasterPlaceHolder_cphContent1_ctl01_ProductSummary1_DataList1_ctl02_divStock" class="book-display-stock"></div>
<div class="book-display-price">' . $price . '</div>
<input type="submit" name="ctl00$ctl00$CMSContentMasterPlaceHolder$cphContent1$ctl01$ProductSummary1$DataList1$ctl02$btnBuyNow" value="add to basket" id="ctl00_ctl00_CMSContentMasterPlaceHolder_cphContent1_ctl01_ProductSummary1_DataList1_ctl02_btnBuyNow" title="add to basket" class="ecom-add-basket" />
</div>
<div class="clear"></div>
</li>';
}
?>
hope it helped

Please do use open and close braces correctly.
If you want to use the HTMl code in php then all you have to do is echo
for example:
<?php
while($row = mysqli_fetch_array($result)) {
echo "<div class='book-display-centered'>{$row['Book_Author']}</div>"
}
?>
And since you have lot of data in html you can use it like this
<?php
while($row = mysqli_fetch_array($result)) {
?>
<div class='book-display-centered'><?php echo $row['Book_Author'] ?></div>
<?php
}
?>

You must echo html content or close php tag before them
Try this:
<?php
while($row = mysqli_fetch_array($result)) {
$price = ($row['ISBN']%1000);
echo $price;
?>
<li>
<div class="book-display-bg">
<div class="book-display-placeholder">
<div class="book-display-container">
<div class="book-display-centered">
<a href='product/taqatu-niran-min-yaumiyat-al-intifada-as-suriya-9789953892368-(105523).html' >
<img src="<?php echo $row['Image_URL_M']?>" style='border: 0;' alt=" <?php echo $row['Book_Title']?>"/>
</a>
</div>
</div>
</div>
</div>
<div class="book-display-details">
<div class="book-display-title"><p><a href='product/taqatu-niran-min-yaumiyat-al-intifada-as-suriya-9789953892368-(105523).html' title="<?php echo $row['Book_Title'] ?>" > <?php echo $row['Book_Title']?></a></p></div>
<div class="book-display-author"><p><?php echo $row['Book_Author'] ?></p></div>
<div id="ctl00_ctl00_CMSContentMasterPlaceHolder_cphContent1_ctl01_ProductSummary1_DataList1_ctl02_divStock" class="book-display-stock"></div>
<div class="book-display-price"><?php echo $price ?></div>
<input type="submit" name="ctl00$ctl00$CMSContentMasterPlaceHolder$cphContent1$ctl01$ProductSummary1$DataList1$ctl02$btnBuyNow" value="add to basket" id="ctl00_ctl00_CMSContentMasterPlaceHolder_cphContent1_ctl01_ProductSummary1_DataList1_ctl02_btnBuyNow" title="add to basket" class="ecom-add-basket" />
</div>
<div class="clear"></div>
</li>
<?php
}
?>

Use below code...
while($row = mysqli_fetch_array($result)) {
$price = ($row['ISBN']%1000);
echo $price;
?>
<li>
<div class="book-display-bg">
<div class="book-display-placeholder">
<div class="book-display-container">
<div class="book-display-centered">
<a href='product/taqatu-niran-min-yaumiyat-al-intifada-as-suriya-9789953892368-(105523).html' >
<img src=''$row['Image_URL_M']'' style='border: 0;' alt=''$row['Book_Title']''/>
</a>
</div>
</div>
</div>
</div>
<div class="book-display-details">
<div class="book-display-title"><p><a href='product/taqatu-niran-min-yaumiyat-al-intifada-as-suriya-9789953892368-(105523).html' title=''$row['Book_Title']'' >$row['Book_Title']</a></p></div>
<div class="book-display-author"><p>$row['Book_Author']</p></div>
<div id="ctl00_ctl00_CMSContentMasterPlaceHolder_cphContent1_ctl01_ProductSummary1_DataList1_ctl02_divStock" class="book-display-stock"></div>
<div class="book-display-price">$price</div>
<input type="submit" name="ctl00$ctl00$CMSContentMasterPlaceHolder$cphContent1$ctl01$ProductSummary1$DataList1$ctl02$btnBuyNow" value="add to basket" id="ctl00_ctl00_CMSContentMasterPlaceHolder_cphContent1_ctl01_ProductSummary1_DataList1_ctl02_btnBuyNow" title="add to basket" class="ecom-add-basket" />
</div>
<div class="clear"></div>
</li>
<?php
}
?>

Related

Foreach Loop in a view doesn't work in CI3 view

Hi I'm trying to do foreach loop on a view but it's not showing. I've checked the database and there's data. I've checked the QueryBuilder on the model and run the query on phpmyadmin and its working. Do you guys know what happened?
view:
<div class="content-wrapper">
<section class="content">
<div class="row">
<?php
foreach ($node as $d) {
?>
<div class="col-lg-6 col-md-6 col-xs-6">
<div class="box box-device box-solid" id="device_<?php echo $d['id_device']; ?>">
<div class="box-header">
<div class="row">
<div class="col-sm-3 col-xs-3">
<button aria-pressed="false" data-device="<?php echo $d['id_device']; ?>" class="btn btn-onoff btn-sm btn-toggle<?php echo ($d['status_device'] == 1) ? ' active': ''; ?>" data-toggle="button" type="button">
<div class="handle"></div>
</button>
</div>
<div class="col-sm-9 col-xs-9">
<h3 class="box-title<?php echo ($d['rule'] == 0) ? ' notactive': ''; ?>"><?php echo $d['nama_device']; ?></h3>
</div>
</div>
</div>
<div class="box-body text-center" onclick="window.location.href = '<?php echo site_url('Setting/rule/' . $d['id_device']); ?>'">
<img alt="<?php echo $d['nama_device']; ?>" src="<?php echo base_url('uploads/device/' . $d['foto']); ?>" />
<h4><?php echo $d['nama_device']; ?></h4>
<p><?php echo $d['id_device']; ?></p>
</div>
<div class="box-footer">
<a class="btn btn-default btn-block btn-lg btn-detail" href="<?php echo site_url('Setting/rule/' . $d['id_device']); ?>">View Rule</a>
</div>
</div>
</div>
<?php
}
?>
</div>
</section>
<?php $this->load->view('components/version'); ?>
</div>
Controller:
public function rulenode() {
$this->data['sub_title'] = 'Setting - Rule';
$this->load->model('Mrule');
$this->data['node'] = $this->Mrule->device();
$this->load->view('setting/rulenode', $this->data);
}
Model:
public function device() {
$this->db->select('id_device, nama_device, foto, status_device');
$this->db->from('tb_device');
$this->db->join('arduino_rule', 'id_device = id_node');
return $this->db->get()->result_array();
// var_dump($this->db->last_query());
}
<?php
$node = $this->db->get('table_name');
foreach ($node as $d) {
?>

Dynamic image gallery with thumbnails

I am trying to create dynamic image gallery with thumbnails and popup full screen option.
Here is my HTML5 gallery (static version)
<div class="property__slider">
<div class="property__ribon">Buy</div>
<div id="properties-thumbs" class="slider slider--small js-slider-thumbs">
<div class="slider__block js-slick-slider">
<div class="slider__item slider__item--0"><img data-lazy="assets/media-demo/properties/big.jpg" src="assets/img/lazy-image.jpg" alt=""><span class="slider__description">1</span></div>
<div class="slider__item slider__item--1"><img data-lazy="assets/media-demo/properties/big.jpg" src="assets/img/lazy-image.jpg" alt=""><span class="slider__description">2</span></div>
<div class="slider__item slider__item--2"><img data-lazy="assets/media-demo/properties/big.jpg" src="assets/img/lazy-image.jpg" alt=""><span class="slider__description">3</span></div>
</div>
</div>
<div class="slider slider--thumbs">
<div class="slider__wrap">
<div class="slider__block js-slick-slider">
<div data-slide-rel='0' class="slider__item slider__item--0">
<div class="slider__img"><img data-lazy="assets/media-demo/properties/thumb.jpg" src="assets/img/lazy-image.jpg" alt=""></div>
</div>
<div data-slide-rel='1' class="slider__item slider__item--1">
<div class="slider__img"><img data-lazy="assets/media-demo/properties/thumb.jpg" src="assets/img/lazy-image.jpg" alt=""></div>
</div>
<div data-slide-rel='2' class="slider__item slider__item--2">
<div class="slider__img"><img data-lazy="assets/media-demo/properties/thumb.jpg" src="assets/img/lazy-image.jpg" alt=""></div>
</div>
<div data-slide-rel='3' class="slider__item slider__item--3">
<div class="slider__img"><img data-lazy="assets/media-demo/properties/thumb.jpg" src="assets/img/lazy-image.jpg" alt=""></div>
<button type="button" class="slider__control slider__control--prev js-slick-prev">
<svg class="slider__control-icon">
<use xlink:href="#icon-arrow-left"></use>
</svg>
</button>
<button type="button" class="slider__control slider__control--next js-slick-next">
<svg class="slider__control-icon">
<use xlink:href="#icon-arrow-right"></use>
</svg>
</button>
</div>
*******UPDATED*******
And here is my php mysql version (dynamic) -
$id = $_GET['id'];
$name = $_GET['name'];
$my_images_arr = scandir('assets/media-demo/properties/'.$id);
$directory = 'assets/media-demo/properties/'.$id.'/';
if ((glob($directory . '*.jpg') != false) || (glob($directory . '*.png') != false))
{
$filecount = count(glob($directory . '.jpg'))+count(glob($directory . '.png'));
}
$img_string = "";
$cnt = 0;
foreach($my_images_arr as $img_name)
{
$cnt = count($my_images_arr);
$cnt = $cnt-2;
if(strlen($img_name) >2)
{
$img_string .= '<img data-lazy="assets/media-demo/properties/'.$id.'/'.$img_name.'" src="assets/media-demo/properties/'.$id.'/'.$img_name.'">';
//print_r($img_string);
}
}
<div id="properties-thumbs" class="slider slider--small js-slider-thumbs">
<div class="slider__block js-slick-slider">
<?php for($i=1; $i<=$filecount; $i++) { ?>
<div class="slider__item slider__item--<?php echo $i; ?>"><img data-lazy="assets/media-demo/properties/<?php echo $my_images_arr[2]; ?>" src="assets/img/lazy-image.jpg" alt=""><span class="slider__description"><?php echo $i; ?></span></div>
<?php } ?>
</div>
</div>
<div class="slider slider--thumbs">
<div class="slider__wrap">
<div class="slider__block js-slick-slider">
<?php for($i=1; $i<=$filecount; $i++) { ?>
<div data-slide-rel='<?php echo $i; ?>' class="slider__item slider__item--<?php echo $i; ?>">
<div class="slider__img"><?php echo $img_string; ?></div>
</div>
<?php } ?>
</div>
<button type="button" class="slider__control slider__control--prev js-slick-prev">
<svg class="slider__control-icon">
<use xlink:href="#icon-arrow-left"></use>
</svg>
</button>
<button type="button" class="slider__control slider__control--next js-slick-next">
<svg class="slider__control-icon">
<use xlink:href="#icon-arrow-right"></use>
</svg>
</button>
</div>
</div>
</div>
</div>
I am facing problem here
<?php for($i=1; $i<=$filecount; $i++) { ?>
<div data-slide-rel='<?php echo $i; ?>' class="slider__item slider__item--<?php echo $i; ?>">
<div class="slider__img"><?php echo $img_string; ?></div>
</div>
<?php } ?>
In thumbnails all three images are becoming a single image, loop has to be
<?php for($i=1; $i<=$filecount; $i++) { ?> <div class="slider__img"><?php echo $img_string; ?></div>
<?php } ?>
But if i did so,
<div data-slide-rel='<?php echo $i; ?>' class="slider__item slider__item--<?php echo $i; ?>">
this will be out of loop.
So not able to trace where i am doing wrong. Here is the screenshot how it looks now https://imgur.com/a/Vk6NVmA

Bootstrap, why are products displayed arbitrarily?

Have a website in three languages. The porducts select from a mysql database and showed wiht a do-while-loop.
<!-- Shop Page Area Start-->
<div class="shoppage-area">
<div class="container">
<div class="about-desc">
<h2>
<?php echo $page['subtitle']; ?>
</h2>
<?php echo $page['content']; ?>
</div>
<div class="row">
<?php
do {
?>
<!--Product Start-->
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
<div class="sngle-product">
<div class="product-thumb">
<img src="<?php echo $product['img']; ?>" alt=""/>
<h2>
<a href="#">
<?php echo $product['name']; ?>
</a>
</h2>
</div>
<h3>
<?php echo $product['desc']; ?>
</h3>
<span><?php echo $product['size']; ?></span> <span class="price"><?php echo $product['price']; ?> LE</span>
<div class="product-overlay">
<ul>
<li>
<div class="product-quantity">
<div class="input-number">
<input type="text" value="1" name="quantity">
</div>
</div>
</li>
<li><a class="orderbtn" href="#" data-uid="<?php echo $product['uniqueid']; ?>">ORDER</a></li>
</ul>
</div>
</div>
</div>
<!-- Product End -->
<?php
} while ($product = $res->fetch_assoc()) ?>
</div>
<!-- end Row -->
</div>
</div>
The problem is that in one language showed correctly and in the other i get a lot spaces between the products. See the image
enter image description here
How i can solve this
Add an additional class flex-row to the product wrapping row element. And try applying the following styles.
.flex-row{
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
You can divide your column like this
<div class="shoppage-area">
<div class="container">
<div class="about-desc">
</div>
<div class="row">
<?php $i=1;
do {
?>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
<div class="sngle-product">
<div class="product-thumb">
<img src="<?php echo "test"; ?>" alt=""/>
<h2><?php echo "test"; ?> </h2>
</div>
<h3><?php echo ($i==2)? " testtesttesttesttesttest testtesttesttest test": "test"; ?></h3>
<span><?php echo "test"; ?></span> <span class="price"><?php echo 12321; ?> LE</span>
<div class="product-overlay">
<ul>
<li>
<div class="product-quantity">
<div class="input-number">
<input type="text" value="1" name="quantity">
</div>
</div>
</li>
<li><a class="orderbtn" href="#" data-uid="<?php echo "1231"; ?>">ORDER</a></li>
</ul>
</div>
</div>
</div>
<?php
echo $i%4;
if($i%4== 0 && $i > 1) {
echo '</div><div class="row">';
}
$i++;
} while ($i<=10) ?>
</div>
</div>
</div>

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.

Pagination with jQuery / AJAX

What plugin should I use to paginate the output of the below PHP foreach (around 400 <articles>):
<div id="hotel-list" class="hotel-list listing-style3 hotel">
<?php foreach ($hotelName as $index => $name):?>
<article class="box" data-board="<?php echo $board[$index];?>" data-stars="<?php echo $StarRating[$index];?>" name="<?php echo $hotelName[$index];?>" data-price="<?php $pret1 =$hotelPrices[$index];echo round($pret1 + ($pret1 * 0.20));?>">
<figure class="col-sm-5 col-md-4">
<a title=""><img width="270" height="160" alt="" src="<?php echo $img[$index][0];?>"></a>
</figure>
<div class="details col-sm-7 col-md-8">
<div>
<div>
<h3 class="box-title"><?php echo $name;?><small><i class="soap-icon-departure yellow-color"></i> <?php echo $hotelAddress[$index];?>, <?php echo $hotelDestination[$index];?></small></h3>
</div>
<div>
<div class="stars-<?php echo $StarRating[$index];?>-container">
<span class="stars-<?php echo $StarRating[$index];?>" style="width: 80%;"></span>
</div>
</div>
</div>
<div>
<div>
<p class="article-text"><label>Perioada: </label> <?php echo $In;?> - <?php echo $Out;?> <?php echo $adults;?> adulti / <?php echo $kids;?> copii; </p>
<p class="article-text"><label>Tip Masa: </label><?php echo $boardType[$index];?></p>
<p class="article-text"><label>Camera(e): </label><?php echo $availHotelss[$index]->rooms[0]->roomCategory;?> <?php echo $availHotelss[$index]->rooms[1]->roomCategory;?> <?php echo $availHotelss[$index]->rooms[2]->roomCategory;?> <?php echo $availHotelss[$index]->rooms[3]->roomCategory;?></p>
</div>
<div>
<span class="price"><small>De la</small>€<?php $pret1 =$hotelPrices[$index];echo round($pret1 + ($pret1 * 0.2)); ?></span>
<a class="button btn-small full-width text-center" title="" href="Hotel-Detalii.php?hotel=<?php echo $hotelCodes[$index];?>">DETALII</a>
</div>
</div>
</div>
</article>
<?php endforeach?>
</div>
I found only pagination plugin for MYSQL records.

Categories