Jump to content

[SOLVED] Limit number of characters shown from mysql


phpocean

Recommended Posts

Hi,

 

What I want:

How can I limit the number of characters shown from mysql database?

 

I wanted it to incorporate it into my current query:

$query = "SELECT * FROM home ORDER BY id DESC LIMIT 5";

 

Say I called for $row[title];

 

and wanted not all the title characters to show but only about 15 characters.

 

Much appreciated.

 

Edited: by the way, is white space count as a character?

 

Ocean

Link to comment
Share on other sites

$title = substr('$row[title]',0,15);

 

the first letter of the string is 0 and yes white spaces do count

 

Can I do this:

<?php
// while there are rows to be fetched...
while ($title = mysql_fetch_assoc($result) substr('$row[title]',0,15)) {
?>

Link to comment
Share on other sites

I wanted it to incorporate it into my current query:

 

If you want it in your query, MySQL also has a substring function - SUBSTRING().

 

You should post in the appropriate section so people will give you appropriate feedback.

Link to comment
Share on other sites

$title = substr('$row[title]',0,15);

 

the first letter of the string is 0 and yes white spaces do count

 

Can I do this:

// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result) substr('$row[title]',0,15)) {
?>

 

Have you tried it?

Link to comment
Share on other sites

I have but didn't work.

 

Ok i will next time, post in right section.

 

Ok i'm stuck, here is my complete query:

 

<?php
// Connect to database.

// get the info from the db 
$sql = "SELECT * FROM home ORDER BY id DESC LIMIT 5";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

?>
<?php
// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
?>

				<tr>
					<td class="tal vam"><div style="margin:7px 0 5px 3px;"><a href="<?php print $list['link']; ?>" style="color:white">[<?php print $list['class']; ?>] <?php print $list['title']; ?></a></div></td>
					<td class="tar" style="color:white"><?php print $list['date']; ?></td>
				</tr>
                    
					<tr style="height:1px;"><td colspan="2" style="background:white;"></td></tr>
    
            <?php
		} // end while
		?>

Link to comment
Share on other sites

Ok it work like charm.

 

Just on the side, MySQL doesn't read < and > so I used < and > instead, well that kinda takes away 8 characters instead of 2, is there a way to solve this issue?

 

Also I wanted it to add ... to the end when the character is at 15, something like this:

 

012345678912345...

 

Maybe I am asking for too much but please bear with me a little while.

 

Ocean

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.