【问题标题】:show article ID in URL when link is active链接有效时在 URL 中显示文章 ID
【发布时间】:2014-02-23 19:06:18
【问题描述】:

我有一个新闻页面,每篇文章都由 SQL while 循环显示 日期和标题可见,但要阅读更多内容,您必须单击 a href 和手风琴 向下滑动,文章将可见。 我想要达到的是,当您单击阅读更多内容时,文章 ID 会显示/传递到 url。

所以我现在有了这个:index.php?pagina=nieuws

这就是我想要达到的目标: index.php?pagina=nieuws#id=25左右

不知道要更改或添加什么,所以举个例子会有所帮助

require_once("...");
mysql_select_db("...");
$query="SELECT id,datum,titel,artikel FROM nieuws ORDER BY id DESC";
$id = $_GET['id'];
$result=mysql_query($query);
if(isset($_GET["id"])) {
echo $_GET["id"];
}

$(document).ready(function(){
$('.acc_containernieuws').hide(); 
$('.acc_triggernieuws').click(function(){
if( $(this).next().is(':hidden') ) { 
    $('.acc_triggernieuws').removeClass('active').next().slideUp();
    $(this).toggleClass('active').next().slideDown();
}
return false; //Prevent the browser jump to the link anchor
});

});

while(list($id,$datum,$titel,$artikel) = mysql_fetch_row($result)){
echo("<div id=\"artikeltitel\" align=\"center\">
<div id=\"containerdatum\">$datum</div>
<div id=\"containertitel\">$titel</div>
<div id=\"container3\" style=\"font-size:12px;\"></div>
</div>
<div class=\"containernieuws\" align=\"center\">
<h2 class=\"acc_triggernieuws\"><a href=\"#?id=$id\"> Meer weergeven </a></h2>
<div class=\"acc_containernieuws\">
<div class=\"blocknieuws\">$artikel</div>
<div class=\"fb-comments\" data-href=\"\" data-num-    posts=\"10\" data-width=\"678\"></div>
</div>
</div>
");
}

【问题讨论】:

    标签: php sql url get


    【解决方案1】:

    #id=25 只是指向当前页面中某个元素的链接。当然,如果您使用 AJAX 或 JQuery,您可以截取它并添加更多数据,但首先是使用普通参数,例如 ?pagina=nieuws&from_id=25 指定新闻项的新开始位置.这将是一个全新的 web_page 请求,您可以在其中显示新的/额外的内容。 (例如,就像一个“下一页”链接)。

    您需要修改 SQL 字符串以包含此参数。

    确保使用正确的 SQL Escapes (mysql_real_escaped_string),例如以防止 SQL 注入

    例如:

    if (isset($_GET['from_id']))
        $query="SELECT id,datum,titel,artikel FROM nieuws WHERE id > '".mysql_real_escaped_string($_GET['from_id'])."' ORDER BY id DESC";
    else
        $query="SELECT id,datum,titel,artikel FROM nieuws ORDER BY id DESC";
    

    【讨论】:

    • Jelle 你必须用一些简单的语言来解释 :-) 我知道足够的 php 来制作我的表单和 while 循环的东西,但不要放弃理解你的建议,我改变了我的 $query 在您的示例中,但对我没有多大帮助..?
    • 确实是 $(currentAnchor).show();完成了这项工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 2015-11-24
    • 1970-01-01
    相关资源
    最近更新 更多