2013-11-28 17 views
0

但让我们从开始说话。我正在创建类似wordpres博客索引页面的网站(无法显示图片,因为没有足够的声望)。然后我点击介绍文章中的read more链接,我想将该文章ID从数据库保存到php sesions中。这是代码。如何从数据库设置ID到PHP会话链接点击

   while($row = mysqli_fetch_array($result)) 
         { 
          echo '<div class="img"> 
          <img src="img/smiley.gif" alt="Smiley face" width="250" height="250">  
          </div>'; 
         echo "<h2>".$row['Name'] . "</h2> " ; 
         $string = strip_tags($row['Content']); 

         if (strlen($string) > 500) { 
          $stringCut = substr($string, 0, 500); 
          $string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="post.php?link=1">Read more</a>'; 
         } 
         //echo $string; 
         if(isset($_GET['link']) /*you can validate the link here*/){ 
                $_SESSION['link']= true; 
               } 
         echo $string; 
         echo "<br>"; 

         echo '<div class="content dashboard clearfix"></div>'; 
         echo '<hr>'; 
         } 
         mysqli_close($con); 
         ?> 

所以我在index.php文件3篇介绍文章,我读whant一个,所以我按更多(那我应该写的文章编号,以会话),然后转到其他网页,都我想我应该从会话中获取文章ID。我tryyng做与

if(isset($_GET['link'])){ /*you can validate the link here*/ 
    $_SESSION['link']= true; 
} 

但它总是写号5,从数据库中的最后一个ID,所以我想我应该用AJAX或许,JavaScript的?也许有人能给我举个例子吗? 谢谢。

+0

'如果(isset($ _ GET ['link'] && $ _GET ['link'] == $ row ['id'])'? – Sean

回答

0

您可以设置通过Ajax一个会话变量,但...这会得到相当疯狂,使事情变得过于复杂,不是很搜索引擎友好

有一个更好的方法:让你的“更多”实际链接你的内容。所以,你已经有了一个“更多”链接http://example.com/page.php?id=5,那里面“page.php文件”你根本:

$Id = intval($_GET['id']); 

您可以阅读how to create friendly URL in php?所以它们看起来像http://example.com/page/5后,这更漂亮的做。

从您的代码,您点击链接时,自动转到所需的页面。因此,你只需要创建页面post.php中,并从数据库中检索单行,以类似的方式,因为我上面,但是适当的名称表示:

$Id = intval($_GET['link']);