0

アイテムの詳細の一部のみを表示しようとしていて、substr を while ループに実装しました。ただし、最初の製品では機能しますが、2 番目の製品には「...」のみが表示され、最初の 7 文字は表示されません。

while($row = mysqli_fetch_array($sql)){ 
    $id = $row["id"]; 
    $product_name = $row["product_name"]; 
    $price = $row["price"]; 
    $details = $row["details"]; 
    if (strlen($details) > 10){
        $details1 = substr($details, 0, 7) . '...';
    } else {
        $details1 = $details;
    }
    $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); 
    $dynamicList .= '<div class="contentImage"><a href="product.php?id=' . $id .'"><img src="images/stock_images/' . $id . '.png" alt="' . $product_name .'" width="136" height="97"></a></div> 
            <div class="contentDes"><strong>' . $product_name .'</strong><br> 
            Price: £' . $price .'<br> 
                    ' . $details1 . '<br> 
                    <a href="product.php?id=' . $id .'">View Product</a></div>'; 
}
4

1 に答える 1

0

これを作るか:

  if (strlen($details) > 7){
        $details1 = substr($details, 0, 7) . '...';
    }

また

 if (strlen($details) > 10){
        $details1 = substr($details, 0, 10) . '...';
    }
于 2013-05-18T20:25:12.780 に答える