2015-02-06 47 views
1

在第一頁上,我想將$row['opis']限制爲100個字符,但用戶可以輸入500個字符,因此我需要在第一頁中限制$row['opis']。我怎樣才能做到這一點?減少從數據庫中顯示的文本

這是我的代碼:

<?php 
          mysql_connect("localhost","root","123") or die(mysql_error()); 
          mysql_select_db("database") or die(mysql_error()); 
          $strSQL = "SELECT * FROM table_name ORDER BY id DESC"; 
          $rs = mysql_query($strSQL); 

          while($row = mysql_fetch_array($rs)) { 

         echo '<div class="col-md-3 col-sm-6 masonry-grid-item"> 
            <div class="listing-item"> 
            <div class="overlay-container"> 
             <img src="images/product-1.png" alt=""> 
             <a href="oglas/index.php?id=' . $row['id'] . '" class="overlay small"> 
              <i class="fa fa-plus"></i> 
              <span>View Details</span> 
             </a> 
            </div> 
            <div class="listing-item-body clearfix"> 
             <h3 class="title"><a href="oglas/index.php?id=' . $row['id'] . '">'. $row['naziv'] .'</a></h3> 
             <p>' . $row['opis'] . '</p> 
             <span class="price">' . $row['tel'] . '</span> 
             <div class="elements-list pull-right"> 
              <a href="#" class="wishlist" title="wishlist"><i class="fa fa-heart-o"></i></a> 
              <a href="oglas/index.php?id=' . $row['id'] . '">Pogledaj</a> 
             </div> 
            </div> 
           </div> 
          </div>'; 

          } 
          mysql_close(); 
         ?>  

謝謝!

+0

我從 '$ OPIS' 100需要,編寫查詢? – Mile 2015-02-06 08:40:24

+1

根據「GET」值(如果有100多個圖表,在頁面中添加一個「頁面」導航欄),在用戶點擊「查詢」按鈕時,在您的sql查詢(0,100,101,200,...)中使用「LIMIT」下一頁「,這應該觸發」YOURPAGE&p = 2「,例如...或使用POST ...你的問題對我來說並不是很清楚......,我想你想在頁面顯示中限制記錄號,但我是甚至不知道它^^,需要更多的信息/細節... – Julo0sS 2015-02-06 08:40:45

+0

問題是,如何添加子字符串以顯示只有100個圖表,如果用戶插入500個圖表評論,如何在第一頁顯示只有100個3點? – Mile 2015-02-06 08:44:05

回答

1

您可以使用mb_strimwidth,顯示前100個字符

<?php 
          mysql_connect("localhost","root","123") or die(mysql_error()); 
          mysql_select_db("database") or die(mysql_error()); 
          $strSQL = "SELECT * FROM table_name ORDER BY id DESC"; 
          $rs = mysql_query($strSQL); 

          while($row = mysql_fetch_array($rs)) { 

         echo '<div class="col-md-3 col-sm-6 masonry-grid-item"> 
            <div class="listing-item"> 
            <div class="overlay-container"> 
             <img src="images/product-1.png" alt=""> 
             <a href="oglas/index.php?id=' . $row['id'] . '" class="overlay small"> 
              <i class="fa fa-plus"></i> 
              <span>View Details</span> 
             </a> 
            </div> 
            <div class="listing-item-body clearfix"> 
             <h3 class="title"><a href="oglas/index.php?id=' . $row['id'] . '">'. $row['naziv'] .'</a></h3> 
             <p>' . mb_strimwidth($row["opis"], 0, 100, "") . '</p> 
             <span class="price">' . $row['tel'] . '</span> 
             <div class="elements-list pull-right"> 
              <a href="#" class="wishlist" title="wishlist"><i class="fa fa-heart-o"></i></a> 
              <a href="oglas/index.php?id=' . $row['id'] . '">Pogledaj</a> 
             </div> 
            </div> 
           </div> 
          </div>'; 

          } 
          mysql_close(); 
         ?> 

說明:

mb_strimwidth($YOUR_STRING, 0, 100, "")

0意味着微調從第一個字符開始,100是極限的字符。第四個選項允許您在新字符串的末尾添加「...」。

編輯

您還可以使用substr,但它是用字節計數,而不是文字。因此,如果您使用的是一些多字節編碼,如UTF-8(希臘語,塞爾維亞語,羅馬尼亞語等),則應使用上述mb_strimwidth

0

@Mile你可以像這樣以

$text = substr($text,$start,$length); 

這裏$text是你的字符串,$start是你的出發點和$length是多久你想顯示的字符串。

+0

不要爲我工作.. – Mile 2015-02-06 08:54:06

1

NETCreator非常感謝!

答案是添加mb_strimwidth($ YOUR_STRING,0,100, 「」)......

<?php 
         mysql_connect("localhost","root","123") or die(mysql_error()); 
         mysql_select_db("database") or die(mysql_error()); 
         $strSQL = "SELECT * FROM table_name ORDER BY id DESC"; 
         $rs = mysql_query($strSQL); 

         while($row = mysql_fetch_array($rs)) { 

        echo '<div class="col-md-3 col-sm-6 masonry-grid-item"> 
           <div class="listing-item"> 
           <div class="overlay-container"> 
            <img src="images/product-1.png" alt=""> 
            <a href="oglas/index.php?id=' . $row['id'] . '" class="overlay small"> 
             <i class="fa fa-plus"></i> 
             <span>View Details</span> 
            </a> 
           </div> 
           <div class="listing-item-body clearfix"> 
            <h3 class="title"><a href="oglas/index.php?id=' . $row['id'] . '">'. $row['naziv'] .'</a></h3> 
            <p>' . mb_strimwidth($row["opis"], 0, 100, "") . '</p> 
            <span class="price">' . $row['tel'] . '</span> 
            <div class="elements-list pull-right"> 
             <a href="#" class="wishlist" title="wishlist"><i class="fa fa-heart-o"></i></a> 
             <a href="oglas/index.php?id=' . $row['id'] . '">Pogledaj</a> 
            </div> 
           </div> 
          </div> 
         </div>'; 

         } 
         mysql_close(); 
        ?> 
1

您可以使用SUBSTR也..下面的代碼顯示前4個字符。

echo substr('abcdef', 0, 4); 

輸出 abcd