I
I
Ivan Petrov2017-06-20 15:12:54
Yii
Ivan Petrov, 2017-06-20 15:12:54

Getting the result of the php script, after loading the page in yii?

Hello, there is a script that accesses the API, takes certain data from there and displays it in the view. Now all this is done in the component and thus I pass it on the view:

<?  foreach (Yii::app()->spsr->getCalcDelivery() as $cityName => $city) : ?>
                        <div class="product-body">
                            <div class="num num-box">
                                <input type="text" name="" value="1" class="js-inc-count"/>
                                <a href="" class="plus js-inc-plus"></a>
                                <a href="" class="minus js-inc-minus"></a>
                            </div>
                            <div class="price">
                                <span><?= $item->getEndPrice() ?> руб.</span>
                            </div>

                            <div class="time">
                                <span><span class="test"><?= $city['time']; ?></span> дн.</span>
                            </div>

                            <div class="cart">
                                <button data-stock-id="<?= $city['stock_id'] ?>" href="" class="button js-to-cart">В корзину</button>
                            </div>
                            <? /*
                    <div class="favorite">
                        <a href="" data-id="<?= $item->id; ?>" class="<?= $item->isFavorite() ? 'act' : ''; ?>">В
                            избранное</a>
                    </div>
                    */ ?>
                        </div>
                    <?php endforeach; ?>


$city['time'] is loaded, in the component we get the term and the average time is calculated and passed to this variable. As a result, my page loads from 2 to 10 seconds, if cached data comes across, then quickly, and if new, then for a long time. And I wanted to know how it is possible to implement, as far as I understand, ajax, so that the page is loaded, and after that calculations have already taken place and loaded into the view? I do not quite understand how this can be implemented. Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-06-20
@webinar

Wait for the page to finish loading, throw an ajax request into a pre-prepared action, process the request there and give out some data. Or renders and renders some html, for example:

public function actionGetMyBlock(){
  return $this->renderAjax('someView',[
                 'data'=>Yii::app()->spsr->getCalcDelivery()
  ]);
}

And the js itself is like this:
$( document ).ready(function() {
    $('.someContainer').load('/mycontroller/get-my-block');
});

in someView.php
<?php  foreach ($data as $cityName => $city) : ?>
.............

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question