我遇到了一些问题!
I want to show both news-evt and news-ntc content in my web page. The JS can POST "news-evt, news-ntc", but the PHP cannot process with this result, how can I do? I am new of php and ajax... Thanks!
我的表:
**id** **category**
0 news-evt
1 news-ntc
我的 HTML:
<ul id="blog-filters">
<li data-vid="news-evt, news-ntc"><a>All</a></li>
<li data-vid="news-evt"><a>Event</a></li>
<li data-vid="news-ntc"><a>Notice</a></li>
</ul>
我的JS:
$(document).ready(function() {
$(document).on('click', '#blog-filters li', function(){
var last_id = $(this).data("vid");
$.ajax(
{
url:"load_data.php",
method:"POST",
data:{last_id:last_id},
dataType:"text",
success:function(data) {
if(data !== '') {
$('.post-item').remove();
$('.btn-load-more').remove();
$('.blog-content-rows').append(data);
} else {
$('.btn-load-more a').html("No More...");
}},
});
});
});
我的PHP:
if (isset($_POST['last_id'])){
require_once('datalogin.php');
$last_id = ($_POST['last_id']);
$showLimit = 3;
$output = '';
$id = '';
sleep(1);
$raw_results = mysql_query("SELECT * FROM article WHERE (category LIKE '%".$last_id."%') ORDER BY date DESC LIMIT ".$showLimit) or die(mysql_error());
if(mysql_num_rows($raw_results) > 0) {
while($results = mysql_fetch_array($raw_results)) {
$id = $results['id'];
$output .= 'Something Code...';
}
}
}
Solved: Simply change
$last_id = $_POST['last_id']
to$last_id = explode(',',$_POST['last_id'])
and then addforeach ($last_id as $item) {$raw_results = mysql_query............}
New Problem: But there is a Order problem...the foreach loop output 'evt' result first...
最佳答案
您始终可以使用 PHP 的 echo
。
<div class="blog-content-rows">
<?php
if (isset($_POST['last_id'])){
require_once('datalogin.php');
$last_id = ($_POST['last_id']);
$showLimit = 3;
$output = '';
$id = '';
sleep(1);
$raw_results = mysql_query("SELECT * FROM article WHERE (category LIKE '%".$last_id."%') ORDER BY date DESC LIMIT ".$showLimit) or die(mysql_error());
if(mysql_num_rows($raw_results) > 0) {
while($results = mysql_fetch_array($raw_results)) {
$id = $results['id'];
$output .= 'Something Code...';
//new code here:
echo "<div>ID: ". $id ."<br/> Output: ". $output ."</div>";
}
}
}
?>
</div>
结果是这样的:
<div class="blog-content-rows">
<div>ID: 1<br/> Output: Some_text</div>
<div>ID: 2<br/> Output: Some_other_text</div>
<div>ID: 3<br/> Output: Another_text</div>
</div>
请随时提出任何其他要求。
关于PHP 从 HTML5 获取数据属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44545819/