2013-11-28 19 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']);