dave
dave

Reputation: 1009

SQL query - LIMIT output text from server

I have started building a notifications system.. And when a user posts on another users feed it sends the notification_text they've posted on the other users stream. I then output this text in the notifications. But I would like to LIMIT the amount of text in the SQL query it outputs, so if the user posts a long message it doesn't push the notification right down the page.

So lets say the user posts.

Hello, my name is Dave, and I'm a developer from Derby.

I would like it to only show a snippet of that output

Hello, my name is Dave, and I'm...

I've just never wrote anything like this so could someone possibly explain to me what I need to do.

<?php 
$call="SELECT * FROM notifications WHERE notification_targetuser =".$u2." ORDER BY notification_id DESC LIMIT 5";
$chant= mysqli_query($mysqli,$call);
$num = mysqli_num_rows($chant);
while($notification = mysqli_fetch_array($chant)){
$triggeredby_name = rawfeeds_user_core::getuser($notification['notification_triggeredby']);
$targetuser_name = rawfeeds_user_core::getuser($notification['notification_targetuser']);
                                                echo"<a href='profile.php?username=".$triggeredby_name['username']."'>";
echo "<img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped".$notification['notification_triggeredby'].".jpg\" 'this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\"></a>";
echo "<a href='".$notification['notification_throughurl']."'>";
echo $notification['notification_content'];
echo"<br/>";
echo $notification['notification_text'];
echo"<br/>";
echo $notification['notification_datetime'];
echo "</a><br/><br/><hr>";
} 
?>

Upvotes: 0

Views: 557

Answers (2)

dave
dave

Reputation: 1009

Ok this was pretty simple.

I used

$str=$notification['notification_text'];

$text=substr($str, 0, 40);

echo"$text";

Upvotes: 0

Jack Gajanan
Jack Gajanan

Reputation: 1670

I don’t think there is anything in SQL syntax Just use

LEFT(field name, LIMIT) AS shortText

Upvotes: 1

Related Questions