I've planning to build an website which mostly work with Ajax but I concern about the speed of my PHP code structure.
First technique
I've initial all data values which I query from DB and combine with HTMl, CSS selector to a variable in my method and then echo it out to the client browser as below function.
Both of this techniques I've using Ajax to respond all data but for this method I will using Ajax dataType as HTML because my PHP echo with client side script.
public function last_update() {
$this->select_cat = $this->select_cats('tmv');
$out = '';
$out .= '<div class="soc"><div class="sr"><div class="s"><span class="">Newest and Last update</span></div></div></div><div id = "cat_menu2" class = "owl-carousel owl-theme">';
foreach ($this->select_cat as $k => $cat_val) {
if ($cat_val) {
$out .= '<div style = "border-right:1px solid rgba(69, 69, 69, 0.48);"><div class = "row mi"><a>Hello man dfds dfgdfg </a></div></div>';
} else {
$out .= '<div style = "border-right:1px solid rgba(69, 69, 69, 0.48);"><div class = "row mi"><a>Hello man dfds dfgdfg </a></div></div>';
}
}
$out .= '</div>';
return $out;
}
Second technique
Another hand, I though how it is difference if I using javascript with Ajax method to query only data from database after I got all data I will using Javascript, CSS, HTM, to make up or make the layout for client view.
<script>
$(window).ready(function () {
var prodId = '', prodName = '', prodPrice = '', prodImg = '', prodSImg = '';
var data_array = [];
$.ajax({
type: "GET",
url: "<?php echo base_url('main/fuck'); ?>",
dataType: "json",
cache: false,
success: function (data, st) {
if (st == 'success') {
$.each(data.data, function (i, obj) {
obj = {
prodId: obj.prodId,
prodName: obj.prodName,
prodPrice: obj.prodPrice,
prodImg: obj.prodImg,
prodSImg: obj.prodSImg,
}
data_array.push(obj);
});
}
console.log(data_array); // I will using html,css to make up or create layout of my task here and then append to some elements
}
});
});
</script>