简体   繁体   中英

How can I do this query in CodeIgniter

this is the controller method

public function ajaxLoad()
{
    $data['query'] = $this->db->query("SELECT * FROM items ORDER BY id DESC");
    $data['content'] = $this->load->view('item', $data, true);
    $data = json_encode($data);
    echo $data;
}

and this is the js that invokes ajax

var tempScrollTop, currentScrollTop = 0; 
$(document).scroll(function(){ 

currentScrollTop = $(document).scrollTop(); 

if (tempScrollTop < currentScrollTop ) 
{
    var result = currentScrollTop % 100;
    if(result == 0)
    {
        $.ajax({
          url: "ajax/load",
          type: "POST",
          success: function(res){

          $("#content").html(res);

          }
        });
    }
}
else if (tempScrollTop > currentScrollTop ) 
{
    var result = currentScrollTop % 100;
    if(result == 0)
    {
        $("#content").text("Вверх запрос " + result);
    }
}

tempScrollTop = currentScrollTop; 
})

the problem i that i can't get the part of a page with the query that goes in the content and then json_encode to transfer to javascript, if i remove the

 $data['query'] = $this->db->query("SELECT * FROM items ORDER BY id DESC");

and change this to

$data['content'] = $this->load->view('item', null, true);

thus without the $data object its ok.

then its comes empty, but i need the database rows to display in the content

how can i solve this problem??

and the echo $data; - its the only way to transfer the data to js??, i tried return $data;, but that doesn't work :(

and also the main problem is that the PHP generates the page with the foreach function that generates records by loop, but if i put one of those records with ajax will the variables inside that partial become values of the database query??, or i need to set the values with js??

i am confused about that, sorry if i am wrong, can you help me???

so this is how looks the page generated by php by when the pages loads:

<? $i = 1 ?>
<? foreach($query->result() as $q): ?>



<?
$milliseconds = $q->end;
//echo $dd;
//echo time();
 /*
$year = date('y', $t);
$month = date('m', $t);
$day = date('d', $t);
$hour = date('h', $t);
$minute = date('i', $t);
$second = date('s', $t);
*/
$d = new Datecalc;
$begin = date("Y-m-d H:i:s");
$end = date($q->end);
#$date = $d->dateDiff($begin , $end);
//$begin = strtotime($begin);
//$end = strtotime($q->end);
$d->dateDiff($begin, $end);
//echo $d->day;

$desc = substr($q->desc, 0, 225);
if(strlen($desc) > 180)
{
    $desc = $desc."...";
}
 ?>
<input type="hidden" value="<?= $milliseconds ?>" id="milliseconds_<?= $q->id ?>" />
<div class="item" id="item_<?= $q->id ?>">
        <? if(!empty($_SESSION['role']) && $_SESSION['role'] == 'admin'): ?>
        <div class="admin_delete">
        <a href="<?= base_url() ?>delete/<?= $q->id ?>"><?= img('css/img/admin_delete.png'); ?></a>
        </div>

        <? endif; ?>
    <div class="desc">
    <span class="discount">Скидка <?= $q->discount ?>%</span>
    <span class="desc"><?= $desc ?></span>
    </div>
    <div class="item_pic">
    <? $attr = array('src' => $q->image,
    'id' => 'item_pic',
    'class' => 'item_pic') ?>
    <?= img($attr) ?>
    </div>
    <div class="menu_3">
        <ul class="menu_3">
            <li class="timer">
            <? $attr = array('src' => 'css/img/time.png',
            'class' => 'time') ?>
            <?= img($attr) ?>
            <span id="days_<?= $q->id?>"><?= $d->day ?></span>д.
            <span  id="hours_<?= $q->id?>"><?= $d->hour ?></span> :
            <span  id="minutes_<?= $q->id?>"><?= $d->min ?></span> :
            <span  id="seconds_<?= $q->id?>"><?= $d->sec ?></span>
            </li>
            <li class="price">
            <?= $q->price ?>с.
            </li>
            <li class="buy">
            <div class="small">
            99
            </div>
            <a href="<?= base_url() ?>buy/<?= $q->id ?>">
            <?= img('css/img/kupit.png') ?>
            </li>
            <li class="item_arrow">
            <?= img('css/img/item_arrow.png') ?>
            <? if($i % 2 == 0): ?>
                <ul class="submenu2">
                <? else: ?>
                <ul class="submenu2">
            <? endif; ?>
                    <li>
                    <a href="<?= base_url() ?>show/<?= $q->id ?>">
                    Подробнее
                    </a>
                    </li>
                    <li>
                    <a href="<?= base_url() ?>how" style="color: #fef102;">Как это работает</a>
                    </li>
                    <li style="border-bottom: none;">
                    <a href="">Рассказать друзьям</a>
                    </li>
                    <li style="border-bottom: none;">
                    <a href=""><?= img('css/img/namba.png') ?></a>
                    <a href=""><?= img('css/img/mail_ru.png') ?></a>
                    <a href=""><?= img('css/img/odnoklassniki.png') ?></a>
                    <a href=""><?= img('css/img/vkontakte.png') ?></a>
                    <a href=""><?= img('css/img/facebook.png') ?></a>
                    </li>
                </ul>
            </li>
        </ul>
    </div>
    </div>
<? $i++; ?>
<? endforeach; ?>
    <div class="clear"></div>

i dont know how to put the records by ajax separately gradualy when the user scrolls down

Try rewriting these two lines

$data['query'] = $this->db->query("SELECT * FROM items ORDER BY id DESC");
$data['content'] = $this->load->view('item', $data, true);

as

$db_data['query'] = $this->db->query("SELECT * FROM items ORDER BY id DESC");
$data['content'] = $this->load->view('item', $db_data, true);

As it is, you are sending, as part of the JSON output, the query response from the DB (in the $data array). Then replace the last two lines with

echo json_encode($data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM