How to show the loader image until the page data loads? - php

I'm a newbie to the concept of displaying loaded image until the data loads. The time to load the data is too much so till then I want to display the loader image. Can anyone help me in this regard? For your reference I'm putting below the code from the PHP file and Smarty template file(i.e. HTML).
Following is my PHP code:
<?php
require_once("../../includes/application-header.php");
$objQuestionMatch = new QuestionMatch();
$request = empty( $_GET ) ? $_POST : $_GET ;
if($request['subject_id']!="")
$subject_id = $request['subject_id'];
if($request['topic_id']!="")
$topic_id = $request['topic_id'];
if($subject_id !='' && $topic_id !='')
$all_match_questions = $objQuestionMatch->GetSimilarQuestionsBySubjectIdTopicId($subject_id, $topic_id);
$smarty->assign('all_match_questions', $all_match_questions);
$smarty->display("match-question.tpl")
?>
The Smarty template code is as follows:
<form id="delete-questions-form" name="delete-questions-form" action="{$control_url}modules/questions/match_question.php" method="post">
<table width="100%" class="base-table tbl-practice" cellspacing="0" cellpadding="0" border="0">
<tr class="evenRow">
<th width="33%" style="text-align:center;" class="question-id">Que ID</th>
<th width="33%" style="text-align:center;" class="question-id">Matching Que IDs</th>
<th width="33%" style="text-align:center;" class="question-id">Percentage(%)</th>
</tr>
{if $all_match_questions}
{foreach from=$all_match_questions item=qstn key=key}
{if $qstn.similar_questions_ids_and_percentage}
{assign var=counter value=1}
<tr class="oddRow">
<td class="question-id" align="center" valign="top">
QUE{$qstn.question_id}{if $qstn.question_appeared_count gt 0}-Appeared({$qstn.question_appeared_count}){/if}
</td>
{foreach from=$qstn.similar_questions_ids_and_percentage item=question key=q_no}
{if $counter gt 1}
<tr class="oddRow"><td class="question-id" align="center" valign="top"></td>
{/if}
<td class="question" align="center" valign="top">
{if $question.question_id!=''}
QUE{$question.question_id}{if $question.question_appeared_count gt 0}-Appeared({$question.question_appeared_count}){/if}
{if $question.question_appeared_count eq 0}
<a id ="{$question.question_id}" href="#" class="c-icn c-remove delete_question" title="Delete question"> Delete</a>{/if}
{/if}
</td>
<td class="question" align="center" valign="top">
{if $question.percentage!=''}{$question.percentage}{/if}
{assign var=counter value=$counter+1}
</td>
</tr>
{/foreach}
{/if}
{/foreach}
{else}
<tr>
<td colspan="2" align="center"><b>No Questions Available</b></td>
</tr>
{/if}
</table>
</form>

You cannot show loading image if you are showing data using PHP directly. However if you load the data using ajax, then you can use ajax callbacks to show and hide the preloader.

Make a div with ID loading and add spinner icon or text there and add this to your JS file.
$( document ).ajaxStart(function() {
$( "#loading" ).show();
});
$( document ).ajaxComplete(function( event,request, settings ) {
$( "#loading" ).hide();
});

Try
Add this div on top of your page
<div id="spinner_load" ></div>
Put this script to your page
$(window).load(function() { $("#spinner_load").fadeOut("slow"); });
style
#spinner_load
{
position: fixed;left: 0px;
top: 0px;
width: 100%;height:
100%;
z-index: 9999;
background:#000 url(images/483.GIF) no-repeat;
background-position:center
}

PHP is not gonna do it for you. jQuery/JS will.
Something like this
$(‘#image-selector’).load(function(){
//Do stuff after image is loaded
});

Related

How display the first element of a list ? Smarty, Prestashop module

I'm trying to change a view in a Prestashop Module. I would like to display just the first one case of a list.
Here, is the original list:
{if count($tax_details)>0 && $use_taxes}
<br />
<br />
<table cellpadding="3">
<thead>
<tr style="color:#FFFFFF; background-color: #4D4D4D; font-weight:bold; padding:2px;">
<th style="width: 40%;">{l s='TAX DETAILS' mod='opartdevis'}</th>
<th style="width: 20%;">{l s='Tax rate' mod='opartdevis'}</th>
<th style="width: 20%;">{l s='Total without tax' mod='opartdevis'}</th>
<th style="width: 20%;text-align: right;">{l s='Total tax' mod='opartdevis'}</th>
</tr>
</thead>
{foreach $tax_details as $tax}
<tr>
<td style="width: 40%;">{$tax.prefix|escape:'htmlall':'UTF-8'}</td>
<td style="width: 20%;">{$tax.name|escape:'htmlall':'UTF-8'}</td>
<td style="width: 20%;">{Tools::displayPrice($tax.total_ht)}</td>
<td style="width: 20%;text-align: right;">{Tools::displayPrice($tax.total_tax)}</td>
</tr>
{/foreach}
</table>
{/if}
It display this :
And I would like to display just this number in yellow on another place.
This is my code (it situated on the same page than the original list):
{if count($tax_details)>0}
<tr>
<td colspan="7" style="text-align:right;">
Total éco taxes :
</td>
<td colspan="1" style="text-align:right;">
<span id="total_price_remiseincl">
{Tools::displayPrice($tax_details[0].total_tax)}
</span>
</td>
</tr>
{/if}
I have this error:
I tried many ways to write it, but I don't find how to do.
Could you help me pls ? I'm new with Smarty.
Thanks in advance
Malaury
You need to set up a loop again
{assign 't' '0'}
{foreach $tax_details as $tax}
{if $t === '0'}
{Tools::displayPrice($tax.total_tax)}
{assign 't' '1'}
{/if}
{/foreach}
Alternatively, you can use the 'first' properly like below. (docs)
In your foreach, use,
{if $smarty.foreach.tax_details.first}
<tr>
<td colspan="3"></td>
<td style="width: 20%;text-align: right;">
{Tools::displayPrice($tax.total_tax)}
</td>
</tr>
{/if}

Smarty new table row every nth time

Hi I'm making this HTML email and thanks to the good old Outlook 07 that adds a really nice page break I kinda have to change my logic. Currently my html looks something like this:
<table>
<tr> //I want for every 6th element to create a new <tr>
<td>
<table>
{foreach from=$data.elements item=element name=elements}
{if $smarty.foreach.elements.iteration is odd}
<tr>
<td>
{/if}
<table align="{if $smarty.foreach.elements.iteration is odd}left{else}right{/if}">
Some content
</table>
{if $smarty.foreach.elements.iteration is odd}
<table align="left" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" height="40" style="font-size:1px; line-height:1px; mso-line-height-rule: exactly;"> </td>
</tr>
</table>
{else}
</td>
</tr>
{/if}
{/foreach}
</table>
</td>
</tr>
</table>
Right now after the 6th element it creates this page break so I want is when I have let's say 12 elements each six to have their own table row with table inside.
The following code should work for you:
<table>
{foreach from=$data.elements item=element name=elements}
{if $smarty.foreach.elements.iteration mod 6 eq 1}
<tr>
<td>
<table>
{/if}
{if $smarty.foreach.elements.iteration is odd}
<tr>
<td>
{/if}
<table align="{if $smarty.foreach.elements.iteration is odd}left{else}right{/if}">
Some content
</table>
{if $smarty.foreach.elements.iteration is odd}
<table align="left" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" height="40" style="font-size:1px; line-height:1px; mso-line-height-rule: exactly;"> </td>
</tr>
</table>
{else}
</td>
</tr>
{/if}
{if $smarty.foreach.elements.iteration mod 6 eq 0 or $smarty.foreach.elements.last}
</table>
</td>
</tr>
{/if}
{/foreach}
</table>
but because of many tables I cannot check it for sure. You could also look at Break UL tag after 'n' times in while-loop where I answered how to do the same in PHP.

IF/ELSE for header.tpl

I am working on a site that isn't SEO friendly. Specifically, the header.tpl is inserted automatically into every page, with no option to change it based on the content of the page. I.e., whether category = Bathroom or category = Kitchen, etc.
So I need an if/else command, but having trouble figuring it out in this instance, plus the change that goes along with it.
The code on the portfolio_category.php page is as follows, and what needs to change based on vf_category is parts/header.tpl (I can create Bathroomheader.tpl, Kitchensheader.tpl, etc so that relevant tpl has the relevant Title and Description tags for the page).
<?php
$vc_root_friendly = '.';
$vc_root_site = '.';
include_once("$vc_root_site/config.php");
include_once("$vc_includes_folder/IT.php");
$template_body = new HTML_Template_IT();
$template_body->loadTemplateFile("tpl_portfolio_category.html");
include_once("$vc_includes_folder/images.php");
if(!isset($_REQUEST['vf_category'])){
header("location:portfolio.php");
die();
}
//Show header
$template_header = new HTML_Template_IT();
$template_header->loadTemplateFile("parts/header.tpl",true,false);
$template_body->setVariable('header', $template_header->get());
//Show footer
$template_footer = new HTML_Template_IT();
$template_footer->loadTemplateFile("parts/footer.tpl",true,false);
$template_body->setVariable('footer', $template_footer->get());
$template_body->setVariable("image_category", $_REQUEST['vf_category']);
//Select photos for this category
etc.
Complicating things there is another page referenced in the code above:
tpl_portfolio_category.html
And this page too has its own header.tpl include:
<? include_once 'parts/header.tpl'; ?>
{header}
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td class="main"><h1><span class="firstLetter">{image_category}</span></h1>
<p>
</p>
<table height="89" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="7"> </td>
</tr>
<!-- BEGIN block_thumb -->
<tr>
<td width='180' height="120" background="./images/thumb-bg.gif"> <div
align="center">{thumb1}</div></td>
<td width="10"> </td>
<td width='180' background="./images/thumb-bg.gif"> <div align="center">{thumb2}
</div></td>
<td width="10"> </td>
<td width='180' background="./images/thumb-bg.gif"> <div align="center">{thumb3}
</div></td>
<td width="10"> </td>
<td width='180' background="./images/thumb-bg.gif"> <div align="center">{thumb4}
</div></td>
</tr>
<tr valign="bottom">
<td height="3"></td>
<td height="3"></td>
<td height="3"></td>
<td height="3"></td>
<td height="3"></td>
<td height="3"></td>
<td height="3"></td>
</tr>
<!-- END block_thumb -->
</table>
<br>
<img src="images/spacer.gif"></td>
</tr>
</table>
{footer}
Any guidance would be appreciated! I'm just trying to get parts/header.tpl to change to parts/Bathroomheader.tpl or parts/Kitchenheader.tpl based on the vf_category pulled from the database. But it's driving me nuts.
Thanks in advance,
Gary
there are a few ways to do this, but in order to minimize the number of files you're changing, I suggest that you:
a) Pull the variable from the database and assign it to smarty, on your first php file, for both the header and body:
//Assuming you have retrieved $vf_category from your database;
$template_header->setVariable('vf_category', $vf_category);
$template_body->setVariable('vf_category', $vf_category);
b) Edit your file header.tpl and append the following code at the top:
{if 'vf_category' == 'some_value'}
{include file="parts/Kitchenheader.tpl"};
{elseif 'vf_category' == 'other_value'}
{include file="parts/Bathroomheader.tpl"};
{else}
//the rest of the header.tpl code goes here
{/if}

how to update all the input form data based from session in one hit submit?

I have this shopping cart page, wherein, it displays the products + price and their quantities
now my problem is, I dunno how to make the total number of items and price updated, once I input a different number of quantity and hit update button
here's my function that "lists" the products based from the $_SESSION data
public function getCartListings(){
if (is_array($_SESSION['cart'])){
foreach($_SESSION['cart'] as $id => $qty){
$query = "SELECT * FROM gg_t_wsproducts WHERE ProductID = $id";
$result = mysql_query($query);
if (!$result){
echo "Couldn't execute query: ".mysql_error();
exit;
}
$product = $this->getProduct($id);
echo '<div class="cart-content">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td width="20%" align="center" style="vertical-align:top !important;">
<img src="images/shopping-cart/thumb-image.png" />
</td>
<td width="80%" style="vertical-align:top !important;">
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="border-bottom:2px solid #c6c6c6; padding-bottom:10px">
<tbody>
<tr>
<td width="60%"><b style="font-size:18px">'.$product["ProductName"].'</b></td>
<td width="20%">Qty <input id="proditem" name="'.$id.'" type="text" style="width:20px; margin:0 0 0 4px;" value="'.$qty.'"></td>
<td width="20%"><span style="font-size:24px; font-weight:bold;">$'.number_format($product['ProductOverridePrice'],2).'</span></td>
</tr>
</tbody>
</table>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td width="50%" valign="bottom" align="left"><br>
<span style="font-size:18px; font-weight:bold;">$'.number_format($product['ProductOverridePrice'],2).'</span><br>
<img src="images/shopping-cart/star.png" /> <img src="images/shopping-cart/star.png" /> <img src="images/shopping-cart/star.png" /> <img src="images/shopping-cart/star.png" /> <img src="images/shopping-cart/star.png" /><br>
<span style="font-size:12px; color:#828282;">14 reviews</span>
</td>
<td width="50%" valign="bottom" align="right">
<a class="removefromcart" name="'.$id.'" id="'.$qty.'" href="#"><img src="images/shopping-cart/remove-item-btn.png" border="0" /></a>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>';
}//end foreach
}
}
As you can see above, the items are coming from the foreach loop
here's the front-end code that display the actual items
<div id="catalog-wrapper">
<form action="#" method="POST" id="updatecart">
<h1 class="cart-header">ITEMS IN YOUR CART <span style="float:right"><input type="image" name="updatecart" src="images/shopping-cart/update-cart-btn.png" border="0" /> <img style="float:right;" src="images/shopping-cart/continue-shopping-btn.png" border="0" /></span></h1>
<?php $myCart->getCartListings(); ?>
</form>
</div>
and now, here's the "useless" jquery code i have
$('input[name="updatecart"]').click(function(){
var pid = $('input#proditem').attr('id');
var qty2 = $('input[type="text"]').each(function(idx,elem){
});
$.ajax({
type: "POST",
url: "classes/ajax.cartupdate.php",
data: "pid="+pid+"&qty2="+qty2,
success: function(data){
alert(data);
location.reload();
}
});
return false;
});
the ajax PHP code should have been something like this...but am still having a problem with those codes above..not really sure what to do
if ($qty2){
$cart->updateCart($qty2);
$_SESSION['total_items'] = $cart->totalItems($_SESSION['cart']);
$_SESSION['total_price'] = $cart->totalPrice($_SESSION['cart']);
echo "cartUpdated!";
}
Why you are getting multiple variables? Just get one array and explode it on server side with PHP. I mean, create a javascript array and include every input value in it and then send it to PHP. But in your code there will be two array, no problem. It would like this:
pid="1,2,3,4,5,6,7"+&qtty="0,1,1,0,1,1,0"
Get this values with PHP and explode them to "," and do whatever you want with for loop. By the way, if you will reload the page after sending, then why you are using the ajax method?

Draw td by Smarty Templates?

i have categories and subs and i want draw every category with sub in td. i write this code but it draw all category with sub in one td. what is the problem? thank you
{section name=catsubs loop=$arrClientsCategorySub}
<td width="28" height="26" align="center" style="background-image:url(images/categorycolor.png); background-repeat:no-repeat; background-color:{$arrClientsCategorySub[catsubs].color}">
{section name=catsub2 loop=$arrClientsCategorySub[catsubs].arrsubCompanies}
<a id="ict-font-inner5sub" href="categoriescompanies.php?categoryid={$arrClientsCategorySub[catsubs].arrsubCompanies[catsub2].catsubid}">{$arrClientsCategorySub[catsubs].arrsubCompanies[catsub2].catsubnumber}</a>
{/section}
</td>
<td width="5"></td>
{/section}
I think your <td> code needs to go one {section} deeper to go around every subcateogry. Try this:
{section name=catsubs loop=$arrClientsCategorySub}
{section name=catsub2 loop=$arrClientsCategorySub[catsubs].arrsubCompanies}
<td width="28" height="26" align="center" style="background-image:url(images/categorycolor.png); background-repeat:no-repeat; background-color:{$arrClientsCategorySub[catsubs].color}">
<a id="ict-font-inner5sub" href="categoriescompanies.php?categoryid={$arrClientsCategorySub[catsubs].arrsubCompanies[catsub2].catsubid}">{$arrClientsCategorySub[catsubs].arrsubCompanies[catsub2].catsubnumber}</a>
</td>
{/section}
<td width="5"></td>
{/section}
Also, your code will be a lot more efficient and lightweight if your <td> was assigned a styled class instead of all that code in the long style tag.

Categories