2016-03-31 61 views
2

我有一個問題。我有一個頁面的網站,如果發佈了帖子,帖子出現在帖子的下方需要有圖片,但是在索引頁的最後一頁,我不想讓圖片出現。所以就好像數據庫中的ID是最高的那個帖子沒有在它下面顯示圖片。如何從數據庫行中獲取最高值?

下面是代碼:

<?php 
include 'functions/image/functions.php'; 

$countQuery = $db->prepare("SELECT paginaNummer AS max FROM pages ;"); 
$countRow = $countQuery->fetch(); 
$maxId = $countRow['max']; 

$query = $db->prepare("SELECT * FROM pages"); 
$query->execute(); 

$num_cols = 1; 
$i = 0; 

$imgList = getImagesFromDir($root . $path); 
$img = getRandomFromArray($imgList); 


while($row = $query->fetch()) { 
    if(isset($row)) { 
     echo "<section data-stellar-background-ratio='0.5'>"; 
     echo "<div class='panelContainer'>"; 
     echo "<div class='panel2'>"; 
     echo "<div class='symbol3'></div>"; 
     echo "</div>"; 
     echo "</div>"; 
     echo "</div>"; 
     echo "</section>"; 
     echo "<section class='wrapper'>"; 
     echo "<div class='box'>"; 
     echo "<div class='content'>"; 
     echo $i++ % $num_cols == 0 ? '' : ''; 
     echo "<div id='message'><h2>&nbsp;&nbsp;", $row['berichtNaam'], "</h2>"; 
     echo $row['paginaContent'], "<br />"; 

     if (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 2) { 
      echo "&nbsp;<a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>"; 
      echo "&nbsp;<a class='delete' href='functions/admin/deletePost.php?id=" . $pageNumber . " '>Delete</a>"; 
     } elseif (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 1) { 
      echo "&nbsp;<a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>"; 
     } else { 

     } 
     echo "</div>"; 
     echo "</div>"; 
     echo "</section>"; 
     echo max($row); 
     if (count($row['paginaNummer']) == max($row)){ 

     } else { 
      echo "<a href='/'><img src='<?php echo $path . $img ?>'alt=''/></a>"; 
     } 

     echo "</section>"; 

    } 
} 
echo "</div></div>"; 

?> 

我不會得到任何進一步的使用這一部分,我希望你能幫助我解決這個問題

+0

標題的問題已經回答了在第四你的代碼行(儘管我會添加一個「LIMIT 1」)。那麼究竟是你的問題? – syck

+0

實際的問題是我想在每個帖子下面顯示圖片,但是如果沒有任何帖子或只有一張圖片不能顯示出來 –

+0

'SELECT * FROM pages order by id desc' –

回答

3

很難理解你的需求。但是,嘗試此查詢:

$countQuery = $db->prepare("SELECT paginaNummer AS max 
          FROM pages ORDER BY paginaNummer DESC LIMIT 1"); 

或者可能是這樣的:

$countQuery = $db->prepare("SELECT paginaNummer AS max 
          FROM pages ORDER BY id DESC LIMIT 1"); 

更新:代碼的改變,而不是頂部:

<?php 
include 'functions/image/functions.php'; 

$query = $db->prepare("SELECT * FROM pages"); 
$query->execute(); 
$maxrow = $query->rowCount(); 

$num_cols = 1; 
$i = 0; 

$imgList = getImagesFromDir($root . $path); 
$img = getRandomFromArray($imgList); 

$n = 0; 
while($row = $query->fetch()) { 
    if(isset($row)) { 
     $n++; 
     echo "<section data-stellar-background-ratio='0.5'>"; 
     echo "<div class='panelContainer'>"; 
     echo "<div class='panel2'>"; 
     echo "<div class='symbol3'></div>"; 
     echo "</div>"; 
     echo "</div>"; 
     echo "</div>"; 
     echo "</section>"; 
     echo "<section class='wrapper'>"; 
     echo "<div class='box'>"; 
     echo "<div class='content'>"; 
     echo $i++ % $num_cols == 0 ? '' : ''; 
     echo "<div id='message'><h2>&nbsp;&nbsp;", $row['berichtNaam'], "</h2>"; 
     echo $row['paginaContent'], "<br />"; 

     if (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 2) { 
      echo "&nbsp;<a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>"; 
      echo "&nbsp;<a class='delete' href='functions/admin/deletePost.php?id=" . $pageNumber . " '>Delete</a>"; 
     } elseif (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 1) { 
      echo "&nbsp;<a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>"; 
     } else { 

     } 
     echo "</div>"; 
     echo "</div>"; 
     echo "</section>"; 
     echo max($row); 
     if ($n == $maxrow){ 

     } else { 
      echo "<a href='/'><img src='<?php echo $path . $img ?>'alt=''/></a>"; 
     } 

     echo "</section>"; 

    } 
} 
echo "</div></div>"; 

?> 
+0

它根本沒有顯示任何圖像 –

+0

你可以發佈「pages」數據庫的結構嗎? – denismart

+0

paginaNummer int(11)A_I Primairy Key,berichtNaam varchar(255),paginaContent text –