Why Isnt js / jquery Events working in PHP foreach - php

I understand foreach but this is new for me because within the foreach in PHP I have some script for each item that shows up and wondering why it only works for the item in the first item
In the code, I am getting an image from an MYSQL database. instead of using onclick in js I am learning more into jquery and only when I click the first image it will generate the
$("#main").load("includes/product.php");
path and change I want to change but in the product name and the cart button do not change the visuals.
<section id="new-products" class="clearfix">
<div class="title-block">
<h4>New Products</h4>
</div>
<?PHP
foreach ($item as $items){
?>
<div class="left-col-block">
<article class="product-box clearfix">
<a href="#store/category/<?PHP echo $items['category'];?>/<?PHP echo $items['brand'];?>/<?PHP echo $items['name'];?>" id="product-link">
<img src="<?PHP echo $items['image']; ?>" alt="Card Games" title="Card Games" height="80" width="80">
</a>
<?PHP echo $items['name']; ?><br>
<span class="price">$<?php echo $items['price'];?></span>
</article>
`<script>
$( "#product-link" ).click(function() {
$("#main").load("includes/product.php");
});
</script>
</div>
<div class="left-col-block">
</div>
<?PHP
}
?>
</section>
Expected:
I am wanting each link with the id of product-link to give the event trigger with jquery on each product and each id
Actual Results:
$("#main").load("includes/product.php");
only works on the first id that is equal to product-link

because of you have 3 like for each item and you want to set on click for every 3 link, you must use class attribute and set it unique for each item.
I changed your code to this:
<section id="new-products" class="clearfix">
<div class="title-block">
<h4>New Products</h4>
</div>
<?PHP
foreach ($item as $key=>$items){
?>
<div class="left-col-block">
<article class="product-box clearfix">
<a href="#store/category/<?PHP echo $items['category'];?>/<?PHP echo $items['brand'];?>/<?PHP echo $items['name'];?>" class="product-link-<?=$key?>">
<img src="<?PHP echo $items['image']; ?>" alt="Card Games" title="Card Games" height="80" width="80">
</a>
<?PHP echo $items['name']; ?><br>
<span class="price">$<?php echo $items['price'];?></span>
</article>
`<script>
$( ".product-link-<?=$key?>" ).click(function() {
$("#main").load("includes/product.php");
});
</script>
</div>
<div class="left-col-block">
</div>
<?PHP
}
?>
</section>

Found the answer was
<script>$( ".product-box a" ).click(function() {
$("#main").load("includes/product.php");
});</script>
since product-bot was a class and I didn't realize the # makes it check for id and the. makes it search for a class and class is able to be called multiple times whereas an id has to be unique

Related

How to make the button visible and hidden according to requirement

I want to display the button only when $safe_cus is 1.But the button gets displayed when the browser is resized.
<?php if($safe_cus == 1) {?>
<div class="col-sm-6 hidden-xs">
<div class="pull-right">
<a class="btn btn-primary" href="<?php echo $action_multiple; ?>"><?php echo $button_multiple_bill_payment; ?></a>
</div>
</div>;
<?php } ?>
one more thing you can do is write CSS based on your condition for ex
if($safe==1){.css{display:none;}}else{.css{display:blockl}}
Now put CSS as a class on your button
hope this helps you.
u can use javascript:
first set an id for your div;
for example "parentdiv"
then get your div to you javascript code:
document.getElementById("parentdiv");
and set an onload function for ur body tag:
body onload="show()"
then you can do ur display stuff with php:
<?php if($safe_cus == 1) {?>
<div class="col-sm-6 hidden-xs">
<div class="pull-right">
<a class="btn btn-primary" href="<?php echo $action_multiple; ?>"><?
php echo $button_multiple_bill_payment; ?></a>
</div>
</div>;
<script>
function show(){
var div = document.getElementById("parentdiv");
div.style.display="block";
}
</script>
<?php } ?>

how do i make an image to zoom in bootstrap in codeigniter view when click on image?

<h5>
<a href="<?php echo base_url(); ?>/vendor/home/projects" >Back to Projects</a>
<h5>
<div>
<div class="container">
<div class="row">
<?php
foreach ($projects as $value) {
?>
<?php
$project_images = json_decode($value['project_gallery'],true);
$i=0;
foreach ($project_images as $project_image_value) {
$i++;
?>
<div class="col-md-2">
<img src="<?= base_url() ?>assets/uploads/projects/<?=$project_image_value['fname']?>" onclick="openModal(<?= $i?>)" class="img-thumbnail img-responsive " />
<div><?=$project_image_value['title']?></div>
</div> <?php
}
?>
<?php
}
?>
</div>
</div>
this is my codeigniter view to display images. How can i Apply light box so that when you click image, only that image should be zoomed
There are so many easy to use JQuery plugins available for image zoom kind of functionality. Use any of the following:
There use is as simple as:
$(document).ready(function(){
$('a.photo').zoom({url: 'photo-big.jpg'});
});
Reference
Some more reference:
https://i-like-robots.github.io/EasyZoom/
http://www.jqueryscript.net/tags.php?/image%20zoom/
http://www.jqueryscript.net/demo/Simple-jQuery-Magnifying-Glass-Image-Zoom-Plugin-magnify-js/

having trouble with jquery .context and .index

here is my PHP code to generate a dynamic product table:
while($item = mysqli_fetch_array($result))
{
enter code here
$i++;
$pic = "cartimg/".$item[2];
echo "<div class='prod_box'".$i.">
<div class='center_prod_box'>
<div class='product_title'><a>".$item[1]."</a></div>
<div class='product_img'><a ><img src='".$pic."' alt='' border='0' /></a></div>
<div class='prod_price'><span class='reduce'>".$item[3]."$</span> <a class='price'>".$item[4]."$</a></div>
</div>
<div class='prod_details_tab'> <a class='prod_buy'>Add to Cart</a> <a class='prod_details'>Details</a> </div>
</div>";
}
?>
and here is my jQuery to respond to the clicked event to add the product into the shopping cart
enter code here
cart.addBuyButton(".prod_buy",{
name:'MacBook', // Item name appear on the cart
thumbnail:'media/macbook.jpg', // Thumbnail path of the item (Optional)
price:$("span").index(0), // Cost of the item
shipping:20
// Shipping cost for the item (Optional)
});
prettyPrint();
and here is the query for addBuyButton function
enter code here
self.addBuyButton=function(target,data){$(target).click(function(){self.cart.add(data)
the problem is that, I will have 10 containers, contain 10 products, with the same class name and Id names, and I cant figure out how to read the amount of ".$item[4]."$ if the customer clicked on Add to Cart of different products.
right now the function inserts 12 as the price.
please help me out here, I have surf through many jQuery tutorials but wasn't lucky to find a way.
thanks
Instead of having an addBuyButton for each item on the jQuery I would have just one event listener that handles all of it like this (I personally don't like huge chunks of php in quotes because my IDE doesn't color highlight so pardon my php syntax. You don't have to follow it if you don't like it):
PHP
<div id="products">
<?php while ($item = mysqli_fetch_array($result)) : ?>
<!-- additional code here -->
<div class="product prod_box<?php echo ++i; ?>">
<div class="center_prod_box">
<div class="product_title"><?php echo $item[1] ?></div>
<div class="product_img"><img src="cartimg/<?php echo $item[2] ?>" border="0" /></div>
<div class="prod_price">
<span class="reduce"><?php echo $item[3] ?>$</span>
<a class="price"><?php echo $item[4] ?>$</a>
</div>
</div>
<div class="prod_details_tab">
<a class="prod_buy">Add to Cart</a>
<a class="prod_details">Details</a>
</div>
</div>
<?php endwhile; ?>
</div>
JAVASCRIPT
$('#products').on('click', '.prod_buy', function() {
var product = $(this).closest('.product');
cart.add({
name: product.find('.product_title').first().text(),
thumbnail: product.find('.product_img').first().text(),
price: product.find('.price').first().text(),
shipping: 20
});
});
So that's one event listener for ALL of your prod_buy buttons and it gets the values for the product out of the DOM and adds it to the cart.
If you need any further help add a comment to this post and I'll add more to my answer.

foreach loop and jQuery

I have a PHP and a jQuery script that I use to display a few images. A large image on the left side and 4 thumbnails on the right side. Each time the user clicks an image-thumbnail it will show up on the large image placeholder on the left side.
This is the PHP code I'm using to display the large image and thumbnails:
<div class="pic"><img title="<?php echo $current->alttext ?>" alt="<?php echo $current->alttext ?>" src="<?php echo $current->imageURL; ?>" />
</div>
<ul class="ngg-gallery-list-ir">
<!-- Thumbnail list -->
<?php foreach ( $images as $image ) : ?>
<?php if ( $image->hidden ) continue; ?>
<li id="ngg-image-<?php echo $image->pid ?>" class="ngg-thumbnail-list <?php if ($image->pid == $current->pid) echo 'selected' ?>" >
<a href="<?php echo $image->imageURL ?>" title="<?php echo $image->description ?>" >
<img title="<?php echo $image->alttext ?>" alt="<?php echo $image->alttext ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
</a>
</li>
<?php endforeach; ?>
This is the jQuery I'm using to update the large image when an user clicks on any thumbnail-image:
jQuery(document).ready(function($){
// handle the click of thumbnail images
// redirect it to change the main image
$(".ngg-thumbnail-list a").click(function(){
var src = $(this).attr("href");
$(".ngg-galleryoverview-ir .pic img").attr("src", src);
return false;
});
// preload the large images
function preload(arrayOfImages) {
$(arrayOfImages).each(function(){
$('<img/>')[0].src = this;
});
}
// populate the list of images to load
preload(images);
});
Everything works fine in this setup but I also need to display below the main large-image its title and description. This is the code I'm using:
<div class="container-title-description">
<div class="title"><?php echo $current->alttext ?></div>
<div class="descripton"><?php echo $current->caption ?></div>
</div>
The problem is this: if I add this code inside the foreach loop I get the title and description below each thumbnail-image. If I add this code outside the foreach loop when the main image changes the title and description will stay the same. How can I solve this?
You can view how this setup looks like on this website.
You already add the title and description as hidden title attributes inside the anchor element, so just extract them and update the HTML on demand:
$(".ngg-thumbnail-list a").click(function(){
var src = $(this).attr("href"),
desc = $(this).attr('title'),
title = $(this).find('img').attr('title');
$(".ngg-galleryoverview-ir .pic img").attr("src", src);
$('.container-title-description .title').text(title);
$('.container-title-description .description').text(desc);
return false;
});
Initial HTML (outside your foreach loop):
<div class="container-title-description">
<p class="title"></p>
<p class="description"></p>
</div>

how to pass row from mysql database into div popup window

I need help passing row from a mysql database into a div popup window?
how doI pass product id into popup div which I call through View?
For Example I Want Like This
View</li>
Here My Code
<div class="latest_products_sec">
<div class="latest_products">
<div class="title_box">
</div>
<!--Latest Products Slider -->
<div class="latest_pro_box">
<h4>Latest Shirt's Collection</h4>
<div class="latest_products_slider">
<?php
$queryshirt=mysql_query("select image,id from products
where cid='1' LIMIT 5") or die ('die shirt query');
while($rowshirt=mysql_fetch_array($queryshirt))
{
echo '<ul>';
echo '<li><img src="admin/'.$rowshirt['image'].'"
width="225" height="300" alt="" />
View</li>';
echo '</ul>';?>
}
?>
#shirt Div Popup Window
<div id="shirt" class="proquickview">
<span class="close_btn" onclick="parent.jQuery.fancybox.close();">Close</span>
<h2 class="title">quick view</h2>
<div class="quickviewinfo">
<?php
// I Need To Get ID Here
echo $id=$_REQUEST['id'];
?>
<div class="quickviewinforight">
<div class="titlerow">
<h2>Latest Shirt's Collection</h2>
<div class="start_ratings">
<div class="start_rating">
</div>
</div>
<div>
<div class="quick_review">
<h3>Quick Discription</h3>
<p>TEST.</p>
</div>
<div class="qty_row">
<div class="qty_field">
<label>Select Quantity</label>
<span>
<select name="S">
<option>5</option>
</select>
</span>
</div>
<br class="clear" />
<span class="total_price">Price <strong>$88.00</strong>
<del>$102.00</del></span>
ADD TO CART
</div>
<div class="total_price_row">
</div>
</div>
</div>
</div>
Javascript For Popup Window
$(document).ready(function() {
$(".view_btn").fancybox({
'titlePosition' : 'inside',
'transitionIn' : 'none',
'transitionOut' : 'none'
});
});
$(document).ready(function(){
// Cufon Functions //
Cufon.replace ('.latest_products_slider ul li a.view_btn',{hover:true});
});
You can use new 'echo'
echo 'View</li>';
instead of using this
echo '<li><img src="admin/'.$rowshirt['image'].'" width="225" height="300" alt="" />
View</li>';
combined 'echo' statement for image and link to View
Use jQuery .load function. Then make a hidden div, tgen on an action, use the load function to load the hidden div and change the css selection to visible. Very simple, add effects as well.

Categories