i developed a deal and coupon site and my data source is JSON. and my json file is big around 4MB. i want to show first 30 data and after scroll load next 30. so please tell me how can i do this work using JSON.
here is my code:
<?php $json = file_get_contents('offers.json');
$json_decode = json_decode($json,true);
foreach($json_decode as $data){
?>
<div class="ad-box">
<div class="ad-category"><?php echo $data['category'];?></div>
<div class="ad-image"><img class="lazy" data-src="<?php echo $data['imageUrl'];?>" src="" width="150" height="140" border="0" alt="lazy Image"/></div>
<div class="ad-title"><?php echo $data['title'] . " : " . $data['description'];?></div>
<div class="ad-url">Goto Offer</div>
</div>
<?php }
?>
your Json array is unknow but:
Main page:
<div id="loadbox">
<?php
$json = file_get_contents('offers.json');
$json_decode = json_decode($json, true);
for ($i = 0; $i < 29; $i++):
$data = $json_decode[$i];
?>
<div class="ad-box">
<div class="ad-category"><?php echo $data['category']; ?></div>
<div class="ad-image"><img class="lazy" data-src="<?php echo $data['imageUrl']; ?>" src="" width="150" height="140" border="0" alt="lazy Image"/></div>
<div class="ad-title"><?php echo $data['title'] . " : " . $data['description']; ?></div>
<div class="ad-url">Goto Offer</div>
</div>
<?php
endfor;
?>
</div>
<script type="text/javascript">
var last = 30;
function getData(lst) {
$.get("loader.php", {'last': lst}, function (data) {
$("#loadbox").append(data);
last += 30 ;
});
}
$(function () {
$(window).scroll(function () {
buffer = 40 // # of pixels from bottom of scroll to fire your function. Can be 0
if ($("html").prop('scrollHeight') - $("html").scrollTop() <= $("html").height() + buffer) {
getDate(last);
}
});
});
</script>
in the main page you load first 30 records and active ajax on scroll end.
when one loader for ajax request:
<?php
$json = file_get_contents('offers.json');
$json_decode = json_decode($json, true);
$start = (int) $_GET['last'] ;
for ($i = $start ; $i < ($start+30) ; $i++):
$data = $json_decode[$i];
?>
<div class="ad-box">
<div class="ad-category"><?php echo $data['category']; ?></div>
<div class="ad-image"><img class="lazy" data-src="<?php echo $data['imageUrl']; ?>" src="" width="150" height="140" border="0" alt="lazy Image"/></div>
<div class="ad-title"><?php echo $data['title'] . " : " . $data['description']; ?></div>
<div class="ad-url">Goto Offer</div>
</div>
<?php
endfor;
?>
Related
I didn't build this PHP file, but I am trying to figure out why it is giving me errors. If the "Block layout mode" ACF variable is set to "Modal" then the block works, but gives me this error. "Undefined variable $vids on line 176". If I select the "Embed & Thumbnail" mode, then none of the block is displayed and I get this error. "Call to undefined function vc_embed_layout() on line 252". I can't pin down why this is happening, and I haven't been able to find any unclosed tags or anything in my php file. This is the file in question, and I've marked the lines that trigger errors.
<?php
global $theme_text_domain;
$block_class_identifier = 'blk__vidcol';
// default variables
$block_id = $top_class = $top_style = $inner_class = $inner_style = '';
// check for custom id
$block_id .= BLOCK::set_id( get_sub_field('content_block_id') );
// set block class, $i comes from FLEX class loop and represents the block order on a page
$top_class .= BLOCK::set_standard_classes( $i );
$top_class .= BLOCK::set_custom_classes( get_sub_field('content_block_classes') );
// check for top padding adjustment
$top_class .= BLOCK::set_padding_class( get_sub_field('block_padding_adjustment') );
$top_style .= BLOCK::set_padding_style( get_sub_field('block_padding_adjustment'), get_sub_field('custom_top_padding'), get_sub_field('custom_bottom_padding'));
// check for custom color theme
$top_style .= BLOCK::set_colors( get_sub_field('color_theme'), get_sub_field('text_color'), get_sub_field('custom_background_color') );
// check for width settings
$inner_class .= BLOCK::set_width_class( get_sub_field('content_width') );
$inner_style .= BLOCK::set_width_style( get_sub_field('content_width'), get_sub_field('custom_maximum_content_width') );
$block_assets = get_stylesheet_directory_uri() . '/inc/blocks/' . basename(__FILE__, '.php') . '/assets/';
/*
CUSTOM BLOCK SPECIFIC SETUP
*/
$layout = get_sub_field('block_layout_mode');
$top_class .= ' layout-' . $layout;
// title color
$h2_style = (get_sub_field('color_theme') == 'custom') ? ' color: ' . get_sub_field('custom_title_color') . ';' : '';
/**
* outputs code for the modal version of the video collection block
* #return html output
*/
if (!function_exists('vc_modal_layout')) {
function vc_modal_layout() {
global $block_class_identifier;
$cta_label = get_sub_field('cta_label');
$counter = 0;
$cta = array(
'style' => get_sub_field('cta_style'),
'button_color' => get_sub_field('button_color'),
'button_text_color' => get_sub_field('button_color')
);
$cta_class = BLOCK::get_cta_style( $cta, '.' . $block_class_identifier . ' .button');
?>
<?php // list section ?>
<?php if( have_rows('videos') ): ?>
<div class="vid-container">
<?php while ( have_rows('videos') ) : the_row(); ?>
<?php if ($counter == 0): ?>
<div class="vid-featured">
<div class="video-wrapper">
<iframe src="https://www.youtube.com/embed/<?php echo get_sub_field('youtube_embed_code'); ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>
<a class="vid_overlay" data-fancybox data-src="#<?php echo $block_class_identifier; ?>_vid_0" href="javascript:;"></a>
</div>
<!-- modal -->
<div id="<?php echo $block_class_identifier; ?>_vid_0" class="modal video-modal" style="display: none;">
<div class="modal-inner">
<h3><?php echo get_sub_field('video_title'); ?></h3>
<div class="video-wrapper">
<iframe src="https://www.youtube.com/embed/<?php echo get_sub_field('youtube_embed_code'); ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>
</div>
</div>
</div>
</div>
<div class="vid-list">
<?php else: ?>
<div class="vid-item">
<div class="item-btn">
<a class="button <?php echo $cta_class; ?>" data-fancybox data-src="#<?php echo $block_class_identifier; ?>_vid_<?php echo $counter; ?>" href="javascript:;"><?php echo $cta_label; ?></a>
</div>
<div class="item-title"><?php echo get_sub_field('video_title'); ?></div>
</div>
<!-- modal -->
<div id="<?php echo $block_class_identifier; ?>_vid_<?php echo $counter; ?>" class="modal video-modal" style="display: none;">
<div class="modal-inner">
<h3><?php echo get_sub_field('video_title'); ?></h3>
<div class="video-wrapper">
<iframe src="https://www.youtube.com/embed/<?php echo get_sub_field('youtube_embed_code'); ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>
</div>
</div>
</div>
<?php endif; ?>
<?php $counter++; ?>
<?php endwhile; ?>
</div>
</div>
<?php else: ?>
No videos defined.
<?php endif; ?>
<?
}
}
/**
* outputs code for the embed version of the video collection block
* #return html output
*/
if (!function_exists('vc_embed_layout')) {
function vc_embed_layout($vids, $i) {
?>
<?php if ( $vids ): ?> **this is line 176**
<div class="vid-container2">
<h2 class="vid-title"><?php echo $vids[0]['video_title']; ?></h2>
<div class="vid-container2-inner">
<div class="vid-embed">
<div class="video-wrapper">
<iframe src="https://www.youtube.com/embed/<?php echo $vids[0]['youtube_embed_code']; ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen onload="this.style.visibility = 'visible';"></iframe>
</div>
</div>
<div class="vid-thumbnails">
<?php foreach ($vids as $key => $vid): ?>
<?php $wrap_class = ($key == 0) ? ' tn-hide' : ''; ?>
<div class="tn-wrapper <?php echo $wrap_class; ?>" data-embed="<?php echo $vid['youtube_embed_code']; ?>" data-title="<?php echo $vid['video_title']; ?>">
<img src="https://img.youtube.com/vi/<?php echo $vid['youtube_embed_code']; ?>/mqdefault.jpg" alt="">
</div>
<?php endforeach; ?>
</div>
</div>
<script>
var vid_swap = function() {
$('.block_<?php echo $i; ?> .vid-embed').css({opacity: 1});
$('.block_<?php echo $i; ?> .tn-wrapper').on('click', function(e) {
var $this = $(this);
var embed_id = $this.attr('data-embed');
var title = $this.attr('data-title');
console.log('embed = ' + embed_id);
$('.block_<?php echo $i; ?> .vid-title').animate({opacity: 0}, 100);
$('.block_<?php echo $i; ?> .vid-embed').animate({opacity: 0}, 1000, function() {
$('.block_<?php echo $i; ?> .vid-embed, .block_<?php echo $i; ?> .vid-title').css('visiblity','hidden');
$('.block_<?php echo $i; ?> .vid-title').html( title );
$('.block_<?php echo $i; ?> .vid-embed iframe').attr('src', 'https://www.youtube.com/embed/' + embed_id);
$('.block_<?php echo $i; ?> .tn-wrapper.tn-hide').removeClass('tn-hide');
$this.addClass('tn-hide');
$('.block_<?php echo $i; ?> .vid-title').css('visiblity','visible').delay(500).animate({opacity: 1}, 500);
$('.block_<?php echo $i; ?> .vid-embed').delay(500).animate({opacity: 1}, 1000);
});
});
}
jQuery(document).on('block_init', vid_swap);
</script>
</div>
<?php else: ?>
No videos defined.
<?php endif; ?>
<?php
}
}
?>
<section id="<?php echo $block_id; ?>" class="<?php echo $block_class_identifier; ?> <?php echo $top_class; ?>" style="<?php echo $top_style; ?>">
<div class="inner <?php echo $inner_class; ?>" style="<?php echo $inner_style; ?>">
<?php // title section ?>
<?php if (get_sub_field('title')): ?>
<h2 style="<?php echo $h2_style; ?>"><?php echo get_sub_field('title'); ?></h2>
<?php endif; ?>
<?php $vids = get_sub_field('videos'); ?>
<?php if ($layout == 'modal') vc_modal_layout($vids); ?>
<?php if ($layout == 'embed') vc_embed_layout($vids, $i); ?> **This is line 252**
</div>
</section>
You're not accepting any arguments in vc_modal_layout() function declaration.
if (!function_exists('vc_embed_layout')) {
function vc_embed_layout() {
needs to have parameters in order to accept arguments.
something like this is what you're looking for:
if (!function_exists('vc_embed_layout')) {
function vc_embed_layout($vids=[], $i=0) {
And it's the same idea with vc_modal_layout, the function needs to be able to accept the variables from outside it.
When we call a function, the function needs to be able to accept all arguments passed to it. And all variables we use need to be defined, if there's a risk of them being undefined or empty arrays, we can check them with if(!empty($var)) or if(isset($var))
An alternative to passing the variable to the function is to access it as a global. But it's generally better to pass the variable to the function because when we pass the variable to a function in PHP it works with a copy of the variable's value inside the function. When we do something like this:
$var = 10;
function doIt(){
global $var;
$var++;
}
doit();
echo $var;
we could overcomplicate things..
let's have a closer look here:
if ($layout == 'modal') vc_modal_layout($vids);
if ($layout == 'embed') vc_embed_layout($vids, $i); ?> **This is line 252**
function vc_modal_layout(){ ... }
// should be:
function vc_modal_layout($vids){ ... }
function vc_embed_layout($vids, $i)
// looks good.
Aside from that, nothing stands out as really unusual & I'd want to have a look at the actual site to tinker with this problem further.
First some general information.
My PHP runs approx 1000-1500 while loops depending on what the database returns.
Normal execution time is approx 0.3 sec. That's great :)
Now I need to load the PHP output on another page using AJAX and therefore I start adding the output to a $html variable in stead of just doing echo on it, so that it can be encoded to JSON format and send over AJAX.
I use the $html .= some_output for this. (this task must be done as many times as the while loop runs).
However after adding the $html .= in stead of echo the script is now taking +9 sec's to finish!!
That's clearly not optimal to the end user who is waiting for the AJAX call to return with some results.
I did some microtime on the code to be sure where the problem origins from - and no doubt it is the .= operator.
Any suggestions on how to minimize this?
EDIT: Here comes the code blocks.
First the one that loads flawlessly.
<?php
$time_start = microtime(true);
session_start();
include "../functions/sqlsrv_connect.php";
$skid_id = $_GET['skid_id'];
$vendor_id = $_GET['vendor_id'];
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<link rel="stylesheet" type="text/css" href="../stylesheets/fonts.css">
<link rel="stylesheet" type="text/css" href="../stylesheets/linkcontainer.css">
<script src="/js/jquery-1.11.3.min.js"></script>
</head>
<body>
<div id="parent" style="display: none;">
<?php
$sql_get_vdm_sections = "
SELECT
ml2.level2_descr,
ml2.level2_id
FROM
main_vdm_level2 AS ml2
JOIN
vdm_index AS vdm_i
ON
vdm_i.level2_id = ml2.level2_id
WHERE
vdm_i.skid_id = $skid_id
AND
vdm_i.vendor_id = $vendor_id
GROUP BY
ml2.level2_descr,
ml2.level2_id
ORDER BY
ml2.level2_id
";
$get_vdm_sections = sqlsrv_query($sqlsrv, $sql_get_vdm_sections);
while($vdm_section = sqlsrv_fetch_array($get_vdm_sections,SQLSRV_FETCH_ASSOC)){
$level2_id = $vdm_section['level2_id'];
$level2_descr = $vdm_section['level2_descr'];
?>
<div id="S<?php echo $level2_id;?>" class="section">
<div class="sectionhead">
<?php echo $level2_descr;?>
</div>
<div class="container_holder">
<div id="O<?php echo $level2_id;?>" class="obsolete_section">
<div class="obsolete_lineholder">
<div class="obsolete_header">
<div class="clmn_header">
<p class="small bold">Obsolete Documentation</p>
</div>
</div>
</div>
<div class="obsolete_lineholder">
<div class="obsolete_linkholder">
<a href="#" class="div" target="_blank">
<div class="obsolete_text">
</div>
</a>
</div>
</div>
<div class="obsolete_lineholder">
<div class="obsolete_linkholder">
<a href="#" class="div" target="_blank">
<div class="obsolete_text">
</div>
</a>
</div>
</div>
<div class="obsolete_lineholder">
<div class="obsolete_linkholder">
<a href="#" class="div" target="_blank">
<div class="obsolete_text">
</div>
</a>
</div>
</div>
</div>
<div class="active_cnt">
<div class="button_holder">
<div class="active_btn">
<p class="small">Active Documentation</p>
</div>
<div class="obsolete_btn">
<p class="small">View Obsolete Documentation</p>
</div>
</div>
<div class="lineholder">
<div class="linkholder">
<div class="clmn_header">
<p class="small bold">Chapters</p>
</div>
</div>
</div>
<div class="linkholder_cnt">
<?php
$file_verification = true;
$sql_get_section_content = "
SELECT
file_verification,
level3_head,
level3_descr,
level4_descr,
doc_no,
doc_place,
doc_denominator,
doc_type
FROM
vdm_index
WHERE
skid_id = $skid_id
AND
vendor_id = $vendor_id
AND
level2_id = $level2_id
AND
level4_descr IS NULL
";
$get_section_content = sqlsrv_query($sqlsrv, $sql_get_section_content);
$row_number = 1;
while ($section_content = sqlsrv_fetch_array($get_section_content,SQLSRV_FETCH_ASSOC)){
$level3_file_verification = $section_content['file_verification'];
$level3_head = $section_content['level3_head'];
$level3_descr = $section_content['level3_descr'];
$level3_doc_no = $section_content['doc_no'];
$level3_doc_place = $section_content['doc_place'];
$level3_doc_denominator = $section_content['doc_denominator'];
$level3_doc_type = $section_content['doc_type'];
$level3_width = 100;
$row_id = 'S'.$level2_id.'_R'.$row_number;
$sql_get_level4 = "
SELECT
file_verification,
level4_descr,
doc_no,
doc_place,
doc_denominator,
doc_type
FROM
vdm_index
WHERE
skid_id = $skid_id
AND
vendor_id = $vendor_id
AND
level2_id = $level2_id
AND
level3_descr = '$level3_descr'
AND
level4_descr IS NOT NULL
";
$get_level4 = sqlsrv_query($sqlsrv, $sql_get_level4);
$level4_array = array();
while ($level4 = sqlsrv_fetch_array($get_level4,SQLSRV_FETCH_ASSOC)){
array_push($level4_array, $level4);
}
if (!empty($level4_array)){
$level3_width -= 10;
$level4_active = true;
}
else {
$level4_active = false;
}
if ($file_verification != true){
$level3_width -= 10;
$level3_flag = true;
}
else {
$level3_flag = false;
}
?>
<div class="lineholder">
<a href="../proj_hist.pdf" class="div" target="_blank">
<div class="project">
<?php
if ($level3_doc_denominator != NULL){
?>
<svg x="0px" y="0px" width="45px" height="100%" viewBox="0 0 45 25" preserveAspectRatio="none">
<rect fill="#66FECB" width="27.1" height="25"></rect>
<polygon fill="#66FECB" points="45,12.5 27,0.000 27,25.000 "></polygon>
</svg>
<?php
}
?>
</div>
<div class="project_name">
<div class="project_text">
<?php echo $level3_doc_denominator;?>
</div>
</div>
</a>
<div class="linkholder" >
<div id="<?php echo $row_id;?>" class="top_row" >
<div class="projectstatus" style="background: #ffffff; width: 100%;">
</div>
<?php
if(!(empty($level3_head))&&empty($level3_descr)){
?>
<div class="link_text level3_head" style="float: left; width: 100%; white-space: nowrap;">
<?php echo $level3_head;?>
</div>
<?php
}
else {
?>
<a href="../functions/load_doc.php?doc_no=<?php echo $level3_doc_no.'&doc_place='.$level3_doc_place.'&doc_type='.$level3_doc_type.'&doc_denominator='.$level3_doc_denominator;?>" class="div" target="_blank">
<div class="link_text" style="float: left; width: <?php echo $level3_width;?>%; white-space: nowrap; overflow: hidden;">
<?php echo $level3_descr;?>
</div>
</a>
<?php
if($level3_flag == true){
?>
<div class="flag_this">
</div>
<?php
}
if ($level4_active == true){
?>
<div id="<?php echo $row_id;?>_EXPAND" class="expand">
<div class="expand_icon <?php echo $row_id;?>_EXPAND_icon">
</div>
</div>
<?php }
}
?>
</div>
<div class="clear">
</div>
<?php
if ($level4_active == true) {
?>
<div class="<?php echo $row_id;?>_level4">
<?php
foreach ($level4_array as $level4){
$level4_doc_no = $level4['doc_no'];
$level4_doc_place = $level4['doc_place'];
$level4_doc_type = $level4['doc_type'];
?>
<div id="<?php echo $row_id;?>_SL1" class="sub_row <?php echo $row_id;?>_EXPAND_sub">
<div class="projectstatus" style="background: #ffffff; width: 50%;">
</div>
<a href="../functions/load_doc.php?doc_no=<?php echo $level4_doc_no.'&doc_place='.$level4_doc_place.'&doc_type='.$level4_doc_type;?>" class="div" target="_blank">
<div class="sub_link_text">
<?php echo $level4['level4_descr'];?>
</div>
</a>
</div>
<div class="clear">
</div>
<?php
}
?>
</div>
<?php
}
?>
</div>
</div>
<?php
$row_number++;
}
?>
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
<div id="additional_box">
<div id="additional_info">
I hold additional information about this document!
</div>
<div id="close_additional" onclick="close_additional()">
</div>
</div>
<script>
var mouseX;
var mouseY;
$(document).mousemove( function(e) {
mouseX = e.pageX;
mouseY = e.pageY;
});
function additional() {
mouseY -= 5;
mouseX += 20;
$('#additional_box').css({'top':mouseY,'left':mouseX}).fadeIn('slow');
};
function close_additional() {
$('#additional_box').hide();
};
function reset_expansion_icon(){
var index;
var i = document.getElementsByClassName('expand_icon');
for (index =0; index < i.length; ++index) {
i[index].style.backgroundImage = "url('../img/expand.png')";
}
}
function reset_this_expansion_icon(me){
var index;
var e = $(me).children('.expand_icon')
for (index =0; index < e.length; ++index) {
e[index].style.backgroundImage = "url('../img/expand.png')";
}
}
$('.flag_this').click(function(){
additional();
});
$('.obsolete_btn').click(function() {
$('.section').hide();
var section = $(this).closest('.section');
$(section).show();
//var btn_parent = $(this).parent();
var container = $(this).closest('.container_holder');
var obsolete = $(container).children('.obsolete_section');
$(obsolete).fadeIn(500);
$('.sub_row').hide();
reset_expansion_icon();
});
$('.active_btn').click(function() {
$('.section').fadeIn(500);
$('.obsolete_section').hide();
$('.sub_row').hide();
reset_expansion_icon();
});
$('.expand').click(function() {
var parent = $(this).parent('.top_row');
var level4_class = $(parent).attr('id') + '_level4';
var sub_row_class = $(parent).attr('id') + '_EXPAND_sub';
var index;
var e = document.getElementsByClassName(sub_row_class);
var d = document.getElementsByClassName(level4_class);
for (index = 0; index < e.length; ++index) {
if(e[index].style.display == 'block'){
$(d[0]).slideUp('slow');
e[index].style.display = 'none';
}
else{
d[0].style.display = 'block';
$(e[index]).fadeIn('slow');
}
}
var expand_icon_class = $(parent).attr('id') + '_EXPAND_icon';
var i = document.getElementsByClassName(expand_icon_class);
if (e[0].style.display == 'block')
i[0].style.backgroundImage = "url('../img/condense.png')";
else
var me = $(this);
reset_this_expansion_icon(me);
});
$(window).load(function(){
$('#parent').fadeIn(300);
});
</script>
</body>
</html>
<?php
echo 'Total execution time in seconds: ' . (microtime(true) - $time_start);
?>
And the the .= version (html, head and body is removed as it will load on another page)
<?php
$time_start = microtime(true);
session_start();
include "sqlsrv_connect.php";
$skid_id = $_POST['skid_id'];
$vendor_id = $_POST['vendor_id'];
$html = "
<div id='parent'>
";
$sql_get_vdm_sections = "
SELECT
ml2.level2_descr,
ml2.level2_id
FROM
main_vdm_level2 AS ml2
JOIN
vdm_index AS vdm_i
ON
vdm_i.level2_id = ml2.level2_id
WHERE
vdm_i.skid_id = $skid_id
AND
vdm_i.vendor_id = $vendor_id
GROUP BY
ml2.level2_descr,
ml2.level2_id
ORDER BY
ml2.level2_id
";
$get_vdm_sections = sqlsrv_query($sqlsrv, $sql_get_vdm_sections);
while($vdm_section = sqlsrv_fetch_array($get_vdm_sections,SQLSRV_FETCH_ASSOC)){
$level2_id = $vdm_section['level2_id'];
$level2_descr = $vdm_section['level2_descr'];
$html .= "
<div id='S$level2' class='section'>
<div class='sectionhead'>
$level2_descr
</div>
<div class='container_holder'>
<div id='O$level2_id' class='obsolete_section'>
<div class='obsolete_lineholder'>
<div class='obsolete_header'>
<div class='clmn_header'>
<p class='small bold'>Obsolete Documentation</p>
</div>
</div>
</div>
<div class='obsolete_lineholder'>
<div class='obsolete_linkholder'>
<a href='#' class='div' target='_blank'>
<div class='obsolete_text'>
</div>
</a>
</div>
</div>
<div class='obsolete_lineholder'>
<div class='obsolete_linkholder'>
<a href='#' class='div' target='_blank'>
<div class='obsolete_text'>
</div>
</a>
</div>
</div>
<div class='obsolete_lineholder'>
<div class='obsolete_linkholder'>
<a href='#' class='div' target='_blank'>
<div class='obsolete_text'>
</div>
</a>
</div>
</div>
</div>
<div class='active_cnt'>
<div class='button_holder'>
<div class='active_btn'>
<p class='small'>Active Documentation</p>
</div>
<div class='obsolete_btn'>
<p class='small'>View Obsolete Documentation</p>
</div>
</div>
<div class='lineholder'>
<div class='linkholder'>
<div class='clmn_header'>
<p class='small bold'>Chapters</p>
</div>
</div>
</div>
<div class='linkholder_cnt'>
";
$file_verification = true;
$sql_get_section_content = "
SELECT
file_verification,
level3_head,
level3_descr,
level4_descr,
doc_no,
doc_place,
doc_denominator,
doc_type
FROM
vdm_index
WHERE
skid_id = $skid_id
AND
vendor_id = $vendor_id
AND
level2_id = $level2_id
AND
level4_descr IS NULL
";
$get_section_content = sqlsrv_query($sqlsrv, $sql_get_section_content);
$row_number = 1;
while ($section_content = sqlsrv_fetch_array($get_section_content,SQLSRV_FETCH_ASSOC)){
$level3_file_verification = $section_content['file_verification'];
$level3_head = $section_content['level3_head'];
$level3_descr = $section_content['level3_descr'];
$level3_doc_no = $section_content['doc_no'];
$level3_doc_place = $section_content['doc_place'];
$level3_doc_denominator = $section_content['doc_denominator'];
$level3_doc_type = $section_content['doc_type'];
$level3_width = 100;
$row_id = 'S'.$level2_id.'_R'.$row_number;
$sql_get_level4 = "
SELECT
file_verification,
level4_descr,
doc_no,
doc_place,
doc_denominator,
doc_type
FROM
vdm_index
WHERE
skid_id = $skid_id
AND
vendor_id = $vendor_id
AND
level2_id = $level2_id
AND
level3_descr = '$level3_descr'
AND
level4_descr IS NOT NULL
";
$get_level4 = sqlsrv_query($sqlsrv, $sql_get_level4);
$level4_array = array();
while ($level4 = sqlsrv_fetch_array($get_level4,SQLSRV_FETCH_ASSOC)){
array_push($level4_array, $level4);
}
if (!empty($level4_array)){
$level3_width -= 10;
$level4_active = true;
}
else {
$level4_active = false;
}
if ($file_verification != true){
$level3_width -= 10;
$level3_flag = true;
}
else {
$level3_flag = false;
}
$html .= "
<div class='lineholder'>
<a href='../proj_hist.pdf' class='div' target='_blank'>
<div class='project'>
";
if ($level3_doc_denominator != NULL){
$html .= "
<svg x='0px' y='0px' width='45px' height='100%' viewBox='0 0 45 25' preserveAspectRatio='none'>
<rect fill='#66FECB' width='27.1' height='25'></rect>
<polygon fill='#66FECB' points='45,12.5 27,0.000 27,25.000 '></polygon>
</svg>
";
}
$html .= "
</div>
<div class='project_name'>
<div class='project_text'>
$level3_doc_denominator
</div>
</div>
</a>
<div class='linkholder' >
<div id='$row_id' class='top_row' >
<div class='projectstatus' style='background: #ffffff; width: 100%;'>
</div>
";
if(!(empty($level3_head))&&empty($level3_descr)){
$html .= "
<div class='link_text level3_head' style='float: left; width: 100%; white-space: nowrap;'>
$level3_head
</div>
";
}
else {
$html .= "
<a href='../functions/load_doc.php?doc_no=$level3_doc_no&doc_place=$level3_doc_place&doc_type=$level3_doc_type&doc_denominator=$level3_doc_denominator' class='div' target='_blank'>
<div class='link_text' style='float: left; width: $level3_width%; white-space: nowrap; overflow: hidden;'>
$level3_descr
</div>
</a>
";
if($level3_flag == true){
$html .= "
<div class='flag_this'>
</div>
";
}
if ($level4_active == true){
$html .= "
<div id='$row_id_EXPAND' class='expand'>
<div class='expand_icon $row_id_EXPAND_icon'>
</div>
</div>
";
}
}
$html .= "
</div>
<div class='clear'>
</div>
";
if ($level4_active == true) {
$html .= "
<div class='$row_id_level4'>
";
foreach ($level4_array as $level4){
$level4_doc_no = $level4['doc_no'];
$level4_doc_place = $level4['doc_place'];
$level4_doc_type = $level4['doc_type'];
$level4_descr = $level4['level4_desr'];
$html .= "
<div id='$row_id_SL1' class='sub_row $row_id_EXPAND_sub'>
<div class='projectstatus' style='background: #ffffff; width: 50%;'>
</div>
<a href='../functions/load_doc.php?doc_no=$level4_doc_no&doc_place=$level4_doc_place&doc_type=$level4_doc_type' class='div' target='_blank'>
<div class='sub_link_text'>
$level4_descr
</div>
</a>
</div>
<div class='clear'>
</div>
";
}
$html .= "
</div>
";
}
$html .= "
</div>
</div>
";
$row_number++;
}
$html .= "
</div>
</div>
</div>
</div>
";
$time_end = microtime(true);
$total_time = $time_end - $time_start;
$html .= 'Total execution time in seconds: ' . $total_time;
}
$html .= "
</div>
<div id='additional_box'>
<div id='additional_info'>
I hold additional information about this document!
</div>
<div id='close_additional' onclick='close_additional()'>
</div>
</div>
";
$script = "
var mouseX;
var mouseY;
$(document).mousemove( function(e) {
mouseX = e.pageX;
mouseY = e.pageY;
});
function additional() {
mouseY -= 5;
mouseX += 20;
$('#additional_box').css({'top':mouseY,'left':mouseX}).fadeIn('slow');
};
function close_additional() {
$('#additional_box').hide();
};
function reset_expansion_icon(){
var index;
var i = document.getElementsByClassName('expand_icon');
for (index =0; index < i.length; ++index) {
i[index].style.backgroundImage = 'url('../img/expand.png')';
}
}
function reset_this_expansion_icon(me){
var index;
var e = $(me).children('.expand_icon')
for (index =0; index < e.length; ++index) {
e[index].style.backgroundImage = 'url('../img/expand.png')';
}
}
$('.flag_this').click(function(){
additional();
});
$('.obsolete_btn').click(function() {
$('.section').hide();
var section = $(this).closest('.section');
$(section).show();
//var btn_parent = $(this).parent();
var container = $(this).closest('.container_holder');
var obsolete = $(container).children('.obsolete_section');
$(obsolete).fadeIn(500);
$('.sub_row').hide();
reset_expansion_icon();
});
$('.active_btn').click(function() {
$('.section').fadeIn(500);
$('.obsolete_section').hide();
$('.sub_row').hide();
reset_expansion_icon();
});
$('.expand').click(function() {
var parent = $(this).parent('.top_row');
var level4_class = $(parent).attr('id') + '_level4';
var sub_row_class = $(parent).attr('id') + '_EXPAND_sub';
var index;
var e = document.getElementsByClassName(sub_row_class);
var d = document.getElementsByClassName(level4_class);
for (index = 0; index < e.length; ++index) {
if(e[index].style.display == 'block'){
$(d[0]).slideUp('slow');
e[index].style.display = 'none';
}
else{
d[0].style.display = 'block';
$(e[index]).fadeIn('slow');
}
}
var expand_icon_class = $(parent).attr('id') + '_EXPAND_icon';
var i = document.getElementsByClassName(expand_icon_class);
if (e[0].style.display == 'block')
i[0].style.backgroundImage = 'url('../img/condense.png')';
else
var me = $(this);
reset_this_expansion_icon(me);
});
$(window).load(function(){
$('#parent').fadeIn(300);
});
";
$json = array();
$json['html'] = $html;
$json['script'] = $script;
header('Content-Type: application/json');
echo json_encode( $json );
?>
First some basic benchmarking
Actually... looking a bit more into it, the concat operator should be faster than the (often more convenient) array method...
<?php
ini_set('memory_limit', '256M');
$start = microtime(true);
$html = array();
for ($i=0; $i < 900000; $i++) {
$html[] = "Line number $i\n";
}
$html_out = implode('', $html);
$time_spent = microtime(true) - $start;
printf("Array method: %ss\n", number_format($time_spent, 5));
printf("Array method md5: %s\n", md5($html_out));
unset($start);unset($html);unset($html_out);
$start = microtime(true);
$html = '';
for ($i=0; $i < 900000; $i++) {
$html .= "Line number $i\n";
}
$time_spent = microtime(true) - $start;
printf("Concat method: %ss\n", number_format($time_spent, 5));
printf("Concat method md5: %s\n", md5($html));
unset($start);unset($html);unset($html_out);
$start = microtime(true);
ob_start();
for ($i=0; $i < 900000; $i++) {
echo "Line number $i\n";
}
$html = ob_get_clean();
$time_spent = microtime(true) - $start;
printf("Output buffering method: %ss\n", number_format($time_spent, 5));
printf("Output buffering method md5: %s\n", md5($html));
Output:
Array method: 0.43333s
Array method md5: d3700cc66c04760d857e8bc9a986399c
Concat method: 0.21945s
Concat method md5: d3700cc66c04760d857e8bc9a986399c
Output buffering method: 0.22360s
Output buffering method md5: d3700cc66c04760d857e8bc9a986399c
...then trying to answer the question
Possible cause #1
I think the only reasonable explanation to your problem is lack of physical memory - when you do a plain echo (without output buffering), you don't keep much in memory - but if the strings you are concatenating are relatively long, then you may reach the limit of your physical memory when you store them in memory (no matter which method you use to do so)... so swapping could occur...?
Possible cause #2
On line 237 in the old code you have
<div id="<?php echo $row_id;?>_EXPAND" class="expand">
In the new code (on line 250) that becomes
echo "[...]<div id='$row_id_EXPAND' class='expand'>[...]"
Which means you are looking up a variable called $row_id_EXPAND - which I'm guessing doesn't exist, and therefore could cause a write operation to one or more error-logs for each iteration of the loop (and you have several of the same mistake elsewhere) This could potentially mean tens or even hundreds of thousands of lines in the error log per request === a lot of time spent writing all that data.
To avoid this kind of problem always encapsulate variables in {}, ie. make it:
echo "[...]<div id='{$row_id}_EXPAND' class='expand'>[...]"
Try ob_start() maybe.
Not actually verfied to be faster by myself but allows you to collect the results of echo from the buffer.
Example of using ob_start():
ob_start();
echo("Hello there!"); //would normally get printed to the screen/output to browser
$output = ob_get_clean();
None of the echo's will work as PHP will use a different string buffer whose results can be loaded into an array at the end. The ob_get_clean() will allow echo to start working again.
Hope this helps.
I'm using ajax call api return json and render html append to page like below.
I have question is is possible after post parameter to api function then api function render in php then return rendered data to js, js just only append?
Because there is to much html structure if i write in js so difficult to read. Wondering most people how to do this?
$.ajax({
url: public_path+'/api_for_ajax/category/'+category_id+'/'+visible+'/'+rows_perpage+'/'+page,
type: 'POST',
processData: false,
contentType: false,
async: false
}).done(function(response) {
var response = JSON.parse(response);
$(data_article.article).each(function(i, each_data_article) {
var output = '<div class="article-list"><a href="'+public_path+'/article/'+each_data_article.id+'">
<div class="thumbnail"><img src="'+public_path+'/assets/thumbnails/'+each_data_article.thumbnail.thumbnail_id+'/'+each_data_article.thumbnail.file_name+'.'+each_data_article.thumbnail.file_format+'" alt=""></div>
<div class="subject">'+each_data_article.subject+'</div>
</a></div>';
// append
});
});
api
$data_select_category_id_page = $this->article_model->select_category_id_page($response_message, $category_id, $visible, $rows_perpage, $page);
$data = array();
$data['article'] = $data_select_category_id_page;
echo json_encode($data);
I tried in slim framework, it not work why??
api
$data_select_category_id_page = $this->article_model->select_category_id_page($response_message, $category_id, $visible, $rows_perpage, $page);
// 1
$app->render('frontstage/article/page_view.php', array(
'data_select_category_id_page' => $data_select_category_id_page,
)
);
// 2
return <<<HTML
<div><?php print_r($data_select_category_id_page);?></div>
HTML;
layout
<?php $column_count = 1; ?>
<div class="row">
<?php foreach ($data['article']['article'] as $i => $each_article) { ?>
<?php if ($i >= 4) { ?>
<div class="article-list">
<a href="<?php echo $uri['public']; ?>/article/<?php echo $each_article['id']; ?>">
<div class="thumbnail"><img src="<?php echo $uri['public']; ?>/assets/thumbnails/<?php echo $each_article['thumbnail']['thumbnail_id']?>/<?php echo $each_article['thumbnail']['file_name']?>.<?php echo $each_article['thumbnail']['file_format']?>" alt=""></div>
<div class="subject"><?php echo $each_article['subject'];?></div>
<div class="category-list-container">
<?php foreach ($each_article['category'] as $each_article_category) { ?>
<div class="category-list"><?php echo $each_article_category['subject']; ?></div>
<?php } ?>
<div class="clear"></div>
</div>
<?php
$old_date = $each_article['create_at'];
$old_date_timestamp = strtotime($old_date);
$new_date = date('d M Y', $old_date_timestamp);
?>
<div class="create-at"><?php echo $new_date;?></div>
</a>
</div>
<?php if (($column_count % 4) == 0) { ?>
<div class="clear"></div></div><div class="row">
<?php } ?>
<?php $column_count++;?>
<?php } ?>
<?php } ?>
<div class="clear"></div>
It is possible to have the server return the html rendered rather than JSON.
$.post( "ajax/test.php", function( data ) {
$( ".result" ).html( data );
});
Where test.php returns some kind of HTML data.
The test.php code will contain the logic to render the relevant html. Using the OP example as the logic, would look something like (untested) :
$public_path = '/sitepath/';
foreach($data_select_category_id_page as $article)
{
echo '<div class="aricle-list"><a href="' . $public_path . '/article/' . $article->id . '">
<div class="thumbnail"><img src="' . $public_path . '/assets/thumbnails/' . $article->thumbnail_id . '/' . $article->thumbnail->file_name . '.' . $article->thumbnail->file_format . '" alt=""></div>
<div class="subject">' . $article->subject . '</div>
</a></div>';
}
Or alternatively use a layout to render the html.
Beaware that the size of the rendered html being returned from the server is large than JSON.
I'm retrieving data out of a database using PHP's PDO extension. I never had a problem with this and it always shows up in all browsers. BUT when I tested the website on a friends laptop, the foreach loop works but the content is not displayed. The content is present in the source code.
this is how I set up the code that should be displayed:
<?php
foreach($advertentiesManager->recenteAdvertenties() as $advertentie) {
$verkoperData = $usersManager->userData($advertentie->verkoper);
?>
<div class="col-md-4" style="margin:10px 0;">
<div class="advertentie">
<div class="overlay"></div>
<div class="img" style="background:url('images/categorie/<?php echo $advertentie->categorie; ?>.jpg') no-repeat center center;background-size:cover;"></div>
<div class="picture" style="background:url('<?php echo str_replace("../", "", $verkoperData->foto); ?>') no-repeat center center;background-size:cover;"></div>
<div class="prijs"><p class="h2"> <?php echo $advertentie->verkoopprijs; ?></p></div>
<div style="padding: 10px;padding-left:80px;margin-top:-40px;">
<a href="<?php echo $advertentie->type.'/'.strtolower(str_replace(" ", "-", $advertentie->titel)).'-'.$advertentie->id; ?>">
<?php echo $advertentie->titel; ?>
</a>
<br>
<p style="color:#777;font-size:.9em;">
<?php
$beschrijving = strip_tags($advertentie->omschrijving);
$max_length = 100;
if (strlen($beschrijving) > $max_length) {
$offset = ($max_length - 3) - strlen($beschrijving);
$beschrijving = substr($beschrijving, 0, strrpos($beschrijving, ' ', $offset)) . '... Meer';
} else {
$beschrijving = $advertentie->omschrijving;
}
echo $beschrijving;
?>
</p>
</div>
</div>
</div>
<?php
}
?>
You can view a live example at http://www.coupontrade.nl/ the content should be displayed under "Recente advertenties" and above the twitter part.
I have the following foreach:
<?php
foreach($result15 as $row15) {
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
?>
<div class='wrapper'>
<div class="album"><img src="img/<?php echo $thumb15; ?>" alt="" width="246" height="246"></div>
</div>
<?php } ?>
But thus appears only a div .album within each div .wrapper. How do I see two divs .album within each div .wrapper?
UPDATE
Guys, found the solution:
<?php
$total = 0;
foreach($result15 as $row15){
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
if($total == 0){
echo '<div class="wrapper">';
}
?>
<div class="album" data-disco="disco<?php echo $id15; ?>">
<img src="img/<?php echo $thumb15; ?>" alt="" width="246" height="246">
</div>
<?php
$total = $total + 1;
if($total % 2 == 0){
echo '</div>';
$total = 0;
}
}
?>
<div class='wrapper'>
<div class="album"><img src="img/<?php echo $thumb15; ?>" alt="" width="246" height="246">
<div class="album"><img src="img/..." alt="" width="246" height="246"></div>
</div>
Something like this?
EDIT
I dont understand what you want. But you can do this to get two divs, but you will need to get your image path for the second div image:
<?php
foreach($result15 as $row15) {
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
echo "<div class='wrapper'>";
echo '<div class="album"><img src="img/'.$thumb15.' alt="" width="246" height="246"></div>';
echo '<div class="album"><img src="img/'.$thumb15.' alt="" width="246" height="246"></div>';
echo '</div>';
} ?>
Try this:
<?php
foreach($result15 as $row15) {
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
echo "<div class='wrapper'>";
echo '<div class="album"><img src="img/'.$id15 .' alt="" width="246" height="246"></div>';
echo '<div class="album"><img src="img/'.$id15 .' alt="" width="246" height="246"></div>';
echo '</div>';
} ?>
A nice solution could be to use array chunk considering you want to treat the data in 'chunks' of 2 images at a time. This way, if you want to alter how many images appear in a wrapper you only need to change your chunk size.
$chunkSize = 2;
foreach (array_chunk($result15, $chunkSize) as $wrapperImages) {
echo '<div class="wrapper">';
foreach ($wrapperImages as $image) {
$thumb = $image->thumb;
echo '<div class="album"><img src="img/'.$thumb.' alt="" width="246" height="246"></div>';
}
echo '</div>';
}