Loop Script inside PHP - php

Well im having this problem and cant resolved it would like to have some help from you guys, so here it is:
I have this code:
<?php if (mysql_num_rows($tournaments) !=0){
do { ?>
<div id="mainContainer">
<div id="leftContainer">
<img src="images/tournaments/<?php echo $row_tournaments['logo']; ?>.png">
</div>
<div id="rightContainer">
<div id="rightContent">
<p><?php echo $row_tournaments['description']; ?></p>
<a href="tournaments.php?id_tournament=<?php echo $row_tournaments['id_tournament']; ?>">
<div id="galleryButton">
<p>Entrar no torneio</p>
</div>
</a>
</div>
<div id="rightDetails">
<i class="fa fa-gamepad"></i>
<a href="games.php?id_game=<?php echo $row_tournaments['id_game']; ?>">
<?php echo $row_tournaments['game']; ?>
</a>
<br>
<i class="fa fa-calendar-o"></i>
<a href="#">
<?php echo $row_tournaments['date']; ?>
</a>
<br>
<i class="fa fa-pencil-square-o"></i>
<a href="tournaments.php?id_tournament=<?php echo $row_tournaments['id_tournament']; ?>#disqus_thread">
Sem comentários
</a>
<br>
<script type="text/javascript">
function giveThatInputAValue(){
var elem = document.getElementById("search");
elem.value = "<?php echo $row_tournaments['city']; ?>";
/*document.forms["form"].submit();
*/}
</script>
<i class="fa fa-map-marker"></i>
<a onClick="giveThatInputAValue()">
<?php echo $row_tournaments['city']; ?>
</a>
<br>
<img src="images/<?php echo $row_tournaments['online']; ?>.png">
<a href="#">
<?php echo $row_tournaments['online']; ?>
</a>
</div>
</div>
</div>
<?php } while ($row_tournaments = mysql_fetch_assoc($tournaments));
} else {
?>
<div id="noresults">
<p>Sem torneios</p>
</div>
<?php } ?>
Everything loop fine but this part is not looping at all:
<script type="text/javascript">
function giveThatInputAValue(){
var elem = document.getElementById("search");
elem.value = "<?php echo $row_tournaments['city']; ?>";
/*document.forms["form"].submit();*/
}
</script>
And dont know why is it, someone please help. Cumps.

You are defining your function in a loop. Over and over again. So when you call your function, which one should it call? Probably all previous ones have been overwritten by the last version.
You should define your function outside of any loop and then call it with the parameters that you have available in the loop.
Apart from that you are also adding elements with ID's in your loop. ID's need to be unique as well so you should use classes or give them a unique ID.

a real php programmer knows this (meaning i am not one). been a while away from php.
ok first as i see it you never get your first row from result set (someone advise here). you are checking to see if you have rowcount then plod about. i reserve the right to be very wrong.
try something like
if ($result) {
while($row = mysql_fetch_array($result)) {
// output $row info
}
}
else {
echo "nada";
}
put your script for the function at the top
don't surround the main div section creations like you are with a loop

Related

Can't get output cart using ajax, codeignier

I've been struggling to create a shopping cart using ajax. I'm new to codeigniter and ajax but it seems the request from ajax is successful since the alert('success') in success:function is working. But I can't output data from the controller. At the place where I want to output cart I just get "welcome to xamp window" Could you tell me what I'm doing wrong?
Product view:
<?php if(count($data)): ?>
<?php foreach ($data as $item) { ?>
<div class="shop-item">
<div class="image">
<a href="<?php echo base_url(); ?>index.php/items/item1">
<img class="shop-item-image" src="<?php echo base_url("uploads/".$item->image) ?>" alt="Ariana Grande: thank u, next exclusive clear/pink lp"/>
</a>
<?php if($item->status=='sold'): ?>
<p class="sold-btn">Sold out</p>
<?php else: ?>
<button class="cart-btn" data-productid="<?php echo $item->id;?>" data-productartist="<?php echo $item->artist;?>"
data-producttitle="<?php echo $item->title;?>" data-productprice="<?php echo $item->price;?>" data-productimage="<?php echo $item->image;?>">
<i class="fa fa-shopping-cart"></i>Add to cart</button>
<?php endif; ?>
</div>
<a class="artist" href="#">
<div class="shop-item-details">
<span class="brand"><?php echo $item->artist; ?></span>
</div>
</a>
<div class="title">
<span class="format double-vinyl-lp"><?php echo $item->title; ?></span>
</div>
<div>
<span class="price">€<?php echo $item->price; ?></span>
</div>
</div>
<?php } ?>
<?php endif; ?>
</div>
Jquery
$(document).ready(function() {
$('.cart-btn').click(function () {
var product_id = $(this).data("productid");
var product_artist = $(this).data("productartist");
var product_title = $(this).data("producttitle");
var product_price = $(this).data("productprice");
var image = $(this).data("productimage");
var quantity = 1;
$.ajax({
url :'/index.php/cart/add_to_cart',
method : "POST",
data : {product_id: product_id, product_artist: product_artist, product_title: product_title, product_price: product_price, quantity:quantity, image:image},
success: function(data){
alert(product_artist);
$('#detail-cart').html(data);
},
});
});
$('#detail-cart').load("<?php echo site_url('index.php/cart/load_cart');?>");
})
Controller
public function add_to_cart() {
$data = array(
'id' => $this->input->post('product_id'),
'name' => $this->input->post('product_artist'),
'price' => $this->input->post('product_price'),
'qty' =>1
//'title'=>$this->input->$_POST('product_title'),
//'image'=>$this->input->$_POST('image')
);
$this->cart->insert($data);
print_r(($this->cart->total_items()));
echo $this->show_cart();
}
public function show_cart(){
$output = '';
$no = 0;
foreach ($this->cart->contents() as $items) {
$no++;
$output .='
<div>
<a href="">
<p>hello</p>
</a>
</div>
<div class="description">
<a class="artist" href="">'.$items['name'].'</a>
<p class="mini-total"><span>'.number_format($items['qty']).'</span>×<span>'.number_format($this->cart->total()).'</span></p>
remove
</div>
<div class="quantity">
<i class="fa fa-chevron-up"></i>
<i class="fa fa-chevron-down"></i>
</div>
';
}
return $output;
}
public function load_cart(){
echo $this->show_cart();
}
}
cert view I want to display it in a different view as a part of the header
<div id="detail-cart" class="mini-cart-item">
</div>
I figured the problem. the problem was in url
url :'/index.php/cart/add_to_cart'
didn't work; neither:
url :'<?phph echo base_url();?>index.php/cart/add_to_cart
what i did was declaring :
<script>
base_url= '<?phph echo base_url();?>index.php/cart/add_to_cart'
</script>
and then set url as
url: base_url+'index.php/cart/add_to_cart'
and it worked;

Jquery executing at all times

I have a dropdown switcher in order to change storeviews. I would like to implement a function done with PHP once an option from the dropdown is clicked.
I have been trying to implement this event via Jquery as below:
<?php if (count($block->getGroups()) > 1): ?>
<div class="switcher store switcher-store" id="switcher-store">
<strong class="label switcher-label"><span><?php /* #escapeNotVerified */
echo __('Select Store') ?></span></strong>
<div class="actions dropdown options switcher-options">
<?php foreach ($block->getGroups() as $_group): ?>
<?php if ($_group->getId() == $block->getCurrentGroupId()): ?>
<div class="action toggle switcher-trigger"
role="button"
tabindex="0"
data-mage-init='{"dropdown":{}}'
data-toggle="dropdown"
data-trigger-keypress-button="true"
id="switcher-store-trigger">
<strong>
<span><?php echo $block->escapeHtml($_group->getName()) ?></span>
</strong>
</div>
<?php endif; ?>
<?php endforeach; ?>
<ul class="dropdown switcher-dropdown" data-target="dropdown">
<?php foreach ($block->getGroups() as $_group): ?>
<?php if (!($_group->getId() == $block->getCurrentGroupId())): ?>
<li class="switcher-option">
<a href="#" data-post='<?= $block->getTargetStorePostData($_group->getDefaultStore()); ?>'>
<?php echo $block->escapeHtml($_group->getName()) ?>
</a>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php endif; ?>
<script>
require(['jquery', 'jquery/ui'], function($){
$("#switcher-option").trigger("click", clickConfirm());
function clickConfirm(){
var isGood=confirm('You will lose your cart items if you switch between seasons!');
if (isGood) {
<?php $seasonsHelper->getClearCart(); ?>
} else {
<?php $this->getUrl('*');?>
}
}
});
</script>
The store change works perfectly, but the confirm event appears on different places, not only when I select the dropdown button. If I add to cart, it also appears, when I load the page, It also appears.
I've been trying to accurate identifier with ".class" or "#id" but nothing works.
Is this the proper way to do it?
You need to make some changes to your JS code:
require(['jquery', 'jquery/ui'], function($){
if( $("#switcher-option").lengh>0 ){ /// check for element existance
$("#switcher-option").trigger("click", clickConfirm());
}
function clickConfirm(){
var isGood=confirm('You will lose your cart items if you switch between seasons!');
if (isGood) {
<?php $seasonsHelper->getClearCart(); ?>
} else {
<?php $this->getUrl('*');?>
}
}
});
Note: code is not tested, just shared the logic.

fetch from database in ajax

im my script i want to read from ajax.php and show them and in my ajax.php i want to fetch from database . my problem is fetching from database doesnt work!
echo
'<?php while ($row = mysqli_fetch_array($products)):?>
<div class="item-in-cart clearfix">
<div class="image">
<img src="images/<?php echo $row['id'] ?>" width="124" height="124" alt="cart item" />
</div>
<div class="desc">
<strong>
s
</strong>
<span class="light-clr qty">
</span>
</div>
<div class="price">
<strong></strong>
</div>
</div>';
<?php endwhile;?>
$(document).ready(function(){
$(".addCart").click(function(){
var id = $(this).val();
$.ajax({
type:'post',
url:'ajax.php',
data :{
id : 'id',
},
success : function (response) {
$('#cart').html(response);
}
});
});
});
On the loop of return of your AJAX, put all the result in one concatenated string inside a variable. then echo that variable serve as return in ajax.
make sure that there is an tags with an ID "cart"
ajax.php
<?php
$return = "";
while($row = mysqli_fetch_assoc($products)):
$id = $row['id'];
$return .= "<div class='item-in-cart clearfix'>
<div class='image'>
<img src='images/$id' width='124' height='124' alt='cart item' />
</div>
<div class='desc'>
<strong>
<a href='product.html'>s</a>
</strong>
<span class='light-clr qty'>
<a href='#' class='icon-remove-sign' title='Remove Item'></a>
</span>
</div>
<div class='price'>
<strong></strong>
</div>
</div>";
endwhile;
echo $return;
?>
then the lump of HTML data will be push and insert inside the cart ID tags, but also upon calling the ajax, or before it get insert, empty the cart id first
$('#cart').html('');
$('#cart').html(response);

show detail information when click an image with php

I'm doing a website where I want to show products from a database, and when clicking a product it opens a div with more detail info (without refreshing page or going to another).
The product gallery looks like this:
<div class="item_galery">
<?php
$i=0;
while($info = mysql_fetch_array( $data )){
$titulo = $info['titulo'];
$descr_corta = $info['descr_corta'];
$img_galeria = $info['img_galeria'];
$img_detalle = $info['img_detalle'];
$sub_img1 = $info['sub_img1'];
$sub_img2 = $info['sub_img2'];
$sub_img3 = $info['sub_img3'];
$acabado = $info['acabado'];
$aux_tamaños = $info['medidas'];
$tamaños = explode(" ", $aux_tamaños);
//echo $aux_tamaños.' '.sizeof($tamaños);
$patron_tornillos = $info['patron_tornillos'];
$coleccion = $info['coleccion'];
?>
<div class="item">
<ul><li onclick="mostra_detall(<?php echo $i;?>);"><img alt="" src="<?php echo 'images/llantas/'.$img_galeria; ?>"/></li></ul>
<ul><li class="descr"><span><?php echo $titulo; ?></span></br><?php echo $acabado; ?></li></ul>
</div>
<?php
$i++;
}
?>
in the class item, the first li has a js call function wihic shoud open the detail info. This info looks like:
<div id="detalle" class="llanta_detalle">
<img class="foto_gran" src="<?php echo 'images/llantas/'.$img_detalle; ?>">
<div class="info">
<div class="cerrar"><ul onclick="tanca_detall(<?php echo $i;?>);"><li><img src="images/llantas/icono_cerrar.jpg"></li><li>CERRAR</li></ul><div class="clear"></div></div>
<div class="detalle_titol">
<ul><li id="titol_llanta" class="titol"><?php echo $titulo; ?></li></ul>
<ul><li id="coleccion_llanta" class="coleccion"><?php echo $coleccion; ?></li></ul>
</div>
<div class="clear"></div>
<div class="detalle">
<ul><li class="requadre">ACABADO</li></ul>
<ul><li id="acabado_llanta" class="acabado"><?php echo $acabado; ?></li></ul>
<ul><li class="requadre">PATRON DE TORNILLOS</li></ul>
<ul><li id="tornillos_llanta" class="tornillos"><?php echo $patron_tornillos; ?></li></ul>
</div>
<div class="clear"></div>
<div class="subimagenes"><ul>
<li><a id="fancyBox" href="<?php echo 'images/llantas/'.$sub_img1; ?>"><img src="<?php echo 'images/llantas/'.$sub_img1; ?>"></a></li>
<li><a id="fancyBox" href="<?php echo 'images/llantas/'.$sub_img2; ?>"><img src="<?php echo 'images/llantas/'.$sub_img2; ?>"></a></li>
<li><a id="fancyBox" href="<?php echo 'images/llantas/'.$sub_img3; ?>"><img src="<?php echo 'images/llantas/'.$sub_img3; ?>"></a></li>
</ul></div>
</div>
<div class="clear"></div>
</div>
my problem is that when the div with detail info is open, I need the specific info from the query of that info, so I would need to do a query or somthing again and do it without refreshing the page. How could I do this?
Thanks and sorry for my english
I think that you should use Jquery for this. Here is a link. I gotta go to sleep now, but if you don`t have it done by tomorrow, I will help you! Hope the jquery link helps you though!

Looping over PHP array several times at once whilst not showing same data

I am creating some jQuery functionality on my website whereas an array of Facebook posts and Tweets is looped through to show on the front page.
I have 5 boxes on my front page which I need 5 to show random elements from this array at the same time, then I using some jQuery cycle functionality to cycle through this array. The only issue is, as I am looping over this array 5 times (with each box) and there are only 20 items in this array it can show repeated data.
To help explain this more here is my code:
<?php
$social_feeds = array_merge($tweets_feed, $facebook_feeds);
$social_feeds_arranged = $tweets_feed_instance->sortArrayItemsByDate($social_feeds);
?>
<div id="social_box_item1" class="social_box_item">
<?php foreach($social_feeds_arranged as $item) { ?>
<a class="item">
<?php echo $item['text'] ?>
</a>
<?php } ?>
</div>
<div id="social_box_item2" class="social_box_item">
<?php foreach($social_feeds_arranged as $item) { ?>
<a class="item">
<?php echo $item['text'] ?>
</a>
<?php } ?>
</div>
<div id="social_box_item3" class="social_box_item">
<?php foreach($social_feeds_arranged as $item) { ?>
<a class="item">
<?php echo $item['text'] ?>
</a>
<?php } ?>
</div>
<div id="social_box_item4" class="social_box_item">
<?php foreach($social_feeds_arranged as $item) { ?>
<a class="item">
<?php echo $item['text'] ?>
</a>
<?php } ?>
</div>
<div id="social_box_item5" class="social_box_item">
<?php foreach($social_feeds_arranged as $item) { ?>
<a class="item">
<?php echo $item['text'] ?>
</a>
<?php } ?>
</div>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#social_box_item1').cycle({
});
jQuery('#social_box_item2').cycle({
});
jQuery('#social_box_item3').cycle({
});
jQuery('#social_box_item4').cycle({
});
jQuery('#social_box_item5').cycle({
});
});
</script>
What I need to happen I think is for the first loop to push the item it is displaying ot the back of the array, so when the second loop accesses the first item it will not show the same array item, the same with the 3rd, 4th and 5th loops. I have attempted to do this using array_push and array_shift but can't seem to get it working.
Does anybody have any ideas or can help point out where I am going wrong?
Those loops are not needed then. You can access those array items one by one, if you meant that. Try this:
$social_feeds = array_merge($tweets_feed, $facebook_feeds);
$social_feeds_arranged = $tweets_feed_instance->sortArrayItemsByDate($social_feeds);
?>
<div id="social_box_item1" class="social_box_item">
<a class="item">
<?php echo $social_feeds_arranged[0]['text']; ?>
</a>
</div>
<div id="social_box_item2" class="social_box_item">
<a class="item">
<?php echo $social_feeds_arranged[1]['text']; ?>
</a>
</div>
<div id="social_box_item3" class="social_box_item">
<a class="item">
<?php echo $social_feeds_arranged[2]['text']; ?>
</a>
</div>
<div id="social_box_item4" class="social_box_item">
<a class="item">
<?php echo $social_feeds_arranged[3]['text']; ?>
</a>
</div>
<div id="social_box_item5" class="social_box_item">
<a class="item">
<?php echo $social_feeds_arranged[4]['text']; ?>
</a>
</div>

Categories