I have two fields in database table, title and description. I am displaying tha data in php while loop.
My Code :
$sql = "select * from sightseeing";
$i = 1;
$res = mysql_query($sql, $conn);
if( mysql_num_rows($res) > 0) {
while($row = mysql_fetch_array($res))
{
echo "<tr>
<td>".$i++."</td>
<td>".$row["title"]."</td>
<td>".$row["description"]."</td>
</tr>";
}
}
I want to show only first 50 characters from description field. How to do that?
using strlen ( string $comment ) : int php $comment = 'Note: We convert a string into an array with explode function. I do not use explode function then the output will be a string as shown in below example. '; $comment = (strlen($comment) > 50)? substr($comment,0,25).
$small = substr($big, 0, 100);
To fetch the first alphabet from the strings, use LEFT(). This method allows you to return characters from the left of the string.
try this
$sql = "select * from sightseeing";
$i = 1;
$res = mysql_query($sql, $conn);
if( mysql_num_rows($res) > 0) {
while($row = mysql_fetch_array($res))
{
echo "<tr>
<td>".$i++."</td>
<td>".$row["title"]."</td>
<td>".substr($row['description'], 0, 50)."</td>
</tr>";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With