Jquery executing at all times - php

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.

Related

Storing HTML tags including PHP script in the database using Codeigniter

i'm trying to store html tag that includes php script.
This is for the navigation menu, i'm trying to display only the menu for a particular user role.
I'm storing it this way
public function edit_module(){
$id = $this->input->post('Module_ID', TRUE);
$update = $this->db->update('ref_modules', array('Module_Name'=>$this->input->post('Module_Name', TRUE),
'Module_Menu'=> htmlspecialchars($this->input->post('Module_Menu')),
'UpdatedBy'=>$this->session->userdata('user_id')), "Module_ID = '$id'");
if($update){
return TRUE;
}
}
For display
<!--Menu-->
<!--================================-->
<div id="mainnav-menu-wrap">
<div class="nano">
<div class="nano-content">
<ul id="mainnav-menu" class="list-group">
<!--Category name-->
<li class="list-header">Navigation</li>
<?php if($this->data['menu']): ?>
<?php foreach($this->data['menu'] as $row): ?>
<?php echo htmlspecialchars_decode($row->Module_Menu); ?>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div>
</div> <!-- nano -->
</div><!-- mainnav-menu-wrap -->
<!--================================-->
<!--End menu-->
When i view the source, its fine but when i click the link, special characters are showing.
http://localhost/folderName/%3C?php%20echo%20base_url(%27citizens%27);%20?%3E
Here's a sample value that i am inserting
<li class="<?php echo ($title == 'Citizens' ? 'active-link' : '');?>">
<a href="<?php echo base_url('citizens'); ?>">
<i class="fa fa-user"></i>
<span class="menu-title">
<strong>Citizens</strong>
</span>
</a>
</li>
Any idea guys? Thanks in advance!
Inserting Html into database For Different user role is not the right way. You can use if Conditions To show your Menu for different User roles.

Show / Hide different divs Onclick in different classes

I'm trying to call different divs to show or hide, when I click on a hyperlink with a class. The problem is, I can't get one to disappear when the other hyperlink is clicked. I know its in .Toggle, but I can't figure out how to do it. Thanks for your help in advance.
HTML:
<div class="menu-container">
<div class="left">
<?php if (have_rows('menu_items')):
while (have_rows('menu_items')): the_row(); ?>
<div id="<?php the_sub_field('menu_id'); ?>" class="menu-item"><?php the_sub_field('menu_item'); ?></div>
<?php endwhile;
endif; ?>
</div>
<div class="food-container">
<?php if (have_rows('food_items')):
while (have_rows('food_items')): the_row(); ?>
<div id ="<?php the_sub_field('food_id'); ?>">
<div class="food-item-container">
<div class="food"> <h3><?php the_sub_field('food_name'); ?></h3>
<?php the_sub_field('food_description'); ?>
</div><div class="price"><?php the_sub_field('price'); ?></div>
</div>
<div class="clear"></div>
<?php endwhile;
endif; ?>
</div>
Javascript:
<script>
$(document).ready(
function() {
$("#menu-item1").click(function() {
$("#food_id1_1, #food_id1_2, #food_id1_3, #food_id1_4, #food_id1_5, #food_id1_6").fadeToggle();
});
$("#menu-item2").click(function() {
$("#food_id2_1, #food_id2_2, #food_id2_3, #food_id2_4, #food_id2_5, #food_id2_6").fadeToggle();
});
});
</script>
Here is the url ---> http://dev.the-end-zone.com/menu/
I believe you want to toggle individual menu items as well as when the other is toggled you want to hide the previous menu items if they were open
You can do something like this:
Sample HTML:
<a id="a1" href="javascript:;">CLick 1</a>
<div id="f1" class='fdiv'>F1</div>
<br/>
<a id="a2" href="javascript:;">CLick 2</a>
<div id="f2" class='fdiv'>F2</div>
jQuery:
$(document).ready(function(){
$("#a1").click(function(){
$("#f2").hide();
$("#f1").toggle();
});
$("#a2").click(function(){
$("#f1").hide();
$("#f2").toggle();
});
});
https://jsfiddle.net/sandenay/L06LtLfz/

Loop Script inside 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

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>

Magento Related Product don't show up

I am trying to get the related products block to show up on my product detail page.
I have the folling code in the respective .phtml file
<?php
<?php echo "Related product block"?>
<?php if($this->getItems()->getSize()): ?>
<div class="block block-related">
<div class="block-title">
<strong><span><?php echo $this->__('Related Products') ?></span></strong>
</div>
<div class="block-content">
<p class="block-subtitle"><?php echo $this->__('Check items to add to the cart or') ?> <?php echo $this->__('select all') ?></p>
<ol class="mini-products-list" id="block-related">
<?php foreach($this->getItems() as $_item): ?>
<li class="item">
<?php if(!$_item->isComposite() && $_item->isSaleable()): ?>
<?php if (!$_item->getRequiredOptions()): ?>
<input type="checkbox" class="checkbox related-checkbox" id="related-checkbox<?php echo $_item->getId() ?>" name="related_products[]" value="<?php echo $_item->getId() ?>" />
<?php endif; ?>
<?php endif; ?>
<div class="product">
<img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(50) ?>" width="50" height="50" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" />
<div class="product-details">
<p class="product-name"><?php echo $this->htmlEscape($_item->getName()) ?></p>
<?php echo $this->getPriceHtml($_item, true, '-related') ?>
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<?php echo $this->__('Add to Wishlist') ?>
<?php endif; ?>
</div>
</div>
</li>
<?php endforeach ?>
</ol>
<script type="text/javascript">decorateList('block-related', 'none-recursive')</script>
</div>
<script type="text/javascript">
//<![CDATA[
$$('.related-checkbox').each(function(elem){
Event.observe(elem, 'click', addRelatedToProduct)
});
var relatedProductsCheckFlag = false;
function selectAllRelated(txt){
if (relatedProductsCheckFlag == false) {
$$('.related-checkbox').each(function(elem){
elem.checked = true;
});
relatedProductsCheckFlag = true;
txt.innerHTML="<?php echo $this->__('unselect all') ?>";
} else {
$$('.related-checkbox').each(function(elem){
elem.checked = false;
});
relatedProductsCheckFlag = false;
txt.innerHTML="<?php echo $this->__('select all') ?>";
}
addRelatedToProduct();
}
function addRelatedToProduct(){
var checkboxes = $$('.related-checkbox');
var values = [];
for(var i=0;i<checkboxes.length;i++){
if(checkboxes[i].checked) values.push(checkboxes[i].value);
}
if($('related-products-field')){
$('related-products-field').value = values.join(',');
}
}
//]]>
</script>
The echo above the code shows up on my page. Which of course proves that i implemented the block correctly.
Just everything in the if-statement doesn't show.
I spend some time looking for solutions and i tried rebuilding the indexes and my related product is visible on the frontend.
Anyone know how i can fix this?
Magento will always return a low number or 0 for the condition:
$this->getItems()->getSize()
if some/all of the related products are in the user's cart, and consequently items will appear as though they are missing.
To prevent this behaviour, duplicate the core Magento 'Related' class:
/app/code/core/Mage/Catalog/Block/Product/List/Related.php
to local:
/app/code/local/Mage/Catalog/Block/Product/List/Related.php
Then comment out the following lines in the if statement:
if (Mage::helper('catalog')->isModuleEnabled('Mage_Checkout')) {
//mod - show all related products, even if they have been added to the cart
//Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection,
// Mage::getSingleton('checkout/session')->getQuoteId()
//);
$this->_addProductAttributesAndPrices($this->_itemCollection);
}
On your 4 line:
<?php if($this->getItems()->getSize() > 0 ? true : false) ?>
Try it.
At the top of your code type
var_dump($this->getItems()->getSize()) // does this print NUll, False or > 0
If the code above print NULL or false or less than 1, then do
print_r($this->getItems());
If none of the above print the information you expected then check your block getItems() method
Also where is the closing IF for if($this->getItems()->getSize()):

Categories