2017-08-18 25 views
-4

我想顯示文章總數的計數。我正在使用以下代碼來顯示錶格數據。顯示文章總數的個數

<table> 
    <tr> 
    <th>Id</th> 
    <th>title</th> 
    <th>com</th> 
    <th>com1</th> 
    </tr> 
    <tr> 
    <?php 
     $limit=10; 
     if(empty($_GET['p'])){ 
      $start=0; 
     }else{ 
      $pi=$_GET['p']; 
      $end=$pi*$limit; 
      $start=$end-$limit; 
     } 
     if(!empty($_GET['s'])){ 
      $ss=$_GET['s']; 
      $query="select * from sports where title like '%$ss%'"; 
     }else{ 
      $query="SELECT * FROM sports limit $start,$limit"; 
     } 
     $result=mysqli_query($connect,$query); 
     while($row=mysqli_fetch_assoc($result)){ 
    ?> 
      <tr> 
      <td><?php echo $row['id'] ?></td> 
      <td><?php echo $row['title'] ?></td> 
      <td><?php echo $row['com'] ?></td> 
      <td><?php echo $row['com1'] ?></td> 
      </tr> 
    <?php 
     } 
    ?> 
</table> 

我的表格中共插入120條記錄。請讓我知道如何顯示帖子總數,如果在表中插入新數據,則計數將自動增加。

我想顯示開始表之前的帖子總數。

+0

自動之間...... –

+0

簡單,你將獲得數'mysqli_num_rows($結果);?' – JYoThI

+0

請參閱此鏈接將有助於[function.mysql- NUM-行數](http://php.net/manual/en/function.mysql-num-rows.php) –

回答

0

剛剛更新這部分代碼:

<?php 
    $count = 1; // here 
    $result=mysqli_query($connect,$query); 
    while($row=mysqli_fetch_assoc($result)){ 
?> 
     <tr> 
     <td><?php echo $count; // here ?></td> 
     <td><?php echo $row['id']; ?></td> 
     <td><?php echo $row['title']; ?></td> 
     <td><?php echo $row['com']; ?></td> 
     <td><?php echo $row['com1']; ?></td> 
     </tr> 
<?php 
     $count++; // here 
    } 
?> 

,這部分太:

<tr> 
<th>#</th> 
<th>Id</th> 
<th>title</th> 
<th>com</th> 
<th>com1</th> 
</tr> 
1

如果我收到了你的問題吧,我想你可以使用mysqli_num_rows獲得的數您的結果集中的元素: 請查看以供參考: mysqli_result::$num_rows

0

看到您的代碼後,我認爲您需要fi重新進行其他查詢以獲取記錄的總數,即您在何處獲取分頁數據。我更新了代碼。

希望它會有幫助。

<table> 
<tr> 
    <th>Id</th> 
    <th>title</th> 
    <th>com</th> 
    <th>com1</th> 
</tr> 
<tr> 
<?php 
    $limit=10; 

    // Declared variable total 
    $total = 0; 
    if(empty($_GET['p'])) { 
     $start=0; 
    } else { 
     $pi=$_GET['p']; 
     $end=$pi*$limit; 
     $start=$end-$limit; 
    } 

    if(!empty($_GET['s'])) { 
     $ss=$_GET['s']; 
     $query="SELECT * FROM sports WHERE title LIKE '%$ss%'"; 
     $result=mysqli_query($connect,$query); 

     //Get total number of records for search query 
     $total=$result->num_rows; 
    } else { 
     // Get total number of records for pagination 
     $totalQuery = "SELECT * FROM sports"; 
     $totalResult = mysqli_query($connect, $totalQuery); 
     $total = $totalResult->num_rows; 

     // Get page data 
     $query = "SELECT * FROM sports limit $start,$limit"; 
     $result = mysqli_query($connect,$query); 
    } 

    // you can echo $total to display total records 
    // anywhere you want 

    while($row=mysqli_fetch_assoc($result)) { 
?> 
     <tr> 
     <td><?php echo $row['id'] ?></td> 
     <td><?php echo $row['title'] ?></td> 
     <td><?php echo $row['com'] ?></td> 
     <td><?php echo $row['com1'] ?></td> 
     </tr> 
<?php 
    } 
?> 

0

嘗試。寫我的代碼有兩種查詢

$result=mysqli_query($connect,$query); 
$count=mysqli_num_rows($result);//it will store your total selected row 
while($row=mysqli_fetch_assoc($result)){ 
}