这是控制器方法
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;
}
这是调用ajax
的jsvar 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;
})
问题我无法获取内容中的查询页面的一部分然后json_encode转移到javascript,如果我删除
$data['query'] = $this->db->query("SELECT * FROM items ORDER BY id DESC");
并将其更改为
$data['content'] = $this->load->view('item', null, true);
因此没有$ data对象就可以了。
然后它变空了,但我需要在内容中显示数据库行
我该如何解决这个问题?
和echo $ data; - 它是将数据传输到js ??的唯一方法,我尝试返回$ data;,但这不起作用:(
并且主要的问题是PHP生成带有foreach函数的页面,该函数通过循环生成记录,但是如果我将其中一个记录与ajax放在一起,那部分内的变量将成为数据库查询的值??,或者我需要用js ??
设置值 我很困惑,对不起,如果我错了,你能帮助我吗?所以这就是如何在页面加载时查看php生成的页面:
<? $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>
当用户向下滚动
时,我不知道如何通过ajax分别逐渐放置记录答案 0 :(得分:1)
尝试重写这两行
$data['query'] = $this->db->query("SELECT * FROM items ORDER BY id DESC");
$data['content'] = $this->load->view('item', $data, true);
作为
$db_data['query'] = $this->db->query("SELECT * FROM items ORDER BY id DESC");
$data['content'] = $this->load->view('item', $db_data, true);
实际上,作为JSON输出的一部分,您将从DB(在$ data数组中)发送查询响应。然后用
替换最后两行echo json_encode($data);