0

Thanks to stackoverflow and its great solution, I have found a way to limit the characters in a table but it doesn't work for me. I tried a lot but with no success.

This is my table

<?php


$result = mysqli_query($conn,"SELECT * FROM library ORDER BY `CreatedTime` DESC");

echo "<table class='table-fill' border='0' cellpadding='0' cellspacing='0'>
<tr>
<th position='fixed' overflow='hidden' width='10%'>Book Name</th> 
<th width='5%'></th>


</tr>";

while($row = mysqli_fetch_array($result) ) {

echo "<tr>";
echo "<td colspan='2' style='padding-bottom: 0;'><a href='library.details.php?id=". $row['id']."' target='content' class='positiontitle-link'><font style='text-shadow: none; font-weight: 800;'>" . $row['bookname']. "</td>";
echo  "</tr>";
echo "<tr style='border-top-width: 0; padding-top: 0;'>";
echo '<td style="max-height: 10px;">' . $str . '</td>';

echo "<td style=' padding-top: 0; padding-left: 15px; width: 40%;'> <font color='gray'>Author :</font> " .($row['authorname'] ). "</td>";
echo "<td  width='5%' style=' padding-top: 0;'> <font color='gray'>Year Published </font>" . $row['yearpublished'] . "</td>";
echo "</tr>";

if (strlen($row['bookname']) > 1) $str = substr($row['bookname'], 0, 1) . "..."; 

}
echo"</table>";
?>

This is how it looks like

https://i.stack.imgur.com/wfIYq.jpg

Any help will be appreciated.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
AHMAD
  • 5
  • 1
  • 7

1 Answers1

0

I'm doing this out of my head so forgive me any format issues and such...

  • Move the string length check to just under the while.
  • Overwrite $row['bookname'] instead of creating $str.
  • Remove the line with:

    echo '<td style="max-height: 10px;">' . $str . '</td>';
    

Result:

while($row = mysqli_fetch_array($result) ) {
if (strlen($row['bookname']) > 9) $row['bookname'] = substr($row['bookname'], 0, 9) . "..."; 

echo "<tr>";
echo "<td colspan='2' style='padding-bottom: 0;'><a href='library.details.php?id=". $row['id']."' target='content' class='positiontitle-link'><font style='text-shadow: none; font-weight: 800;'>" . $row['bookname']. "</td>";
echo  "</tr>";
echo "<tr style='border-top-width: 0; padding-top: 0;'>";


echo "<td style=' padding-top: 0; padding-left: 15px; width: 40%;'> <font color='gray'>Author :</font> " .($row['authorname'] ). "</td>";
echo "<td  width='5%' style=' padding-top: 0;'> <font color='gray'>Year Published </font>" . $row['yearpublished'] . "</td>";
echo "</tr>";
}
DigiLive
  • 1,093
  • 1
  • 11
  • 28
  • can you please explain me the code that what does first 9 means and the second 9. – AHMAD Jul 15 '14 at 13:45
  • I set it to 9 to keep the first 9 chars of the book name. You've chosen 1 in your code. Change it to the number of chars you wish to keep. – DigiLive Jul 15 '14 at 14:02