0

Hallo,

How to trim content? I would like to show only the first 30 letters of this content.

This is what I have so far:

include('include/con_database.php');

$result = mysql_query("SELECT * FROM dynamic_content WHERE id='124'");

while ($data = mysql_fetch_array($result)){

echo '<div id="text">'.$data['content'].'</div>'.'<br>';

}

Thanks before.

4
Contributors
9
Replies
94
Views
3 Years
Discussion Span
Last Post by davy_yg
0

Hello,

I try this:

    include('include/con_database.php');

    $result = mysql_query("SELECT * FROM dynamic_content WHERE id='124'");

    while ($data = mysql_fetch_array($result)){

        echo '<div id="text">'.substr($data['content'], 0, 30).'</div>'.'<br>';
    }

I do not see the content appears.

0

I tested your above code in my local pc. It works fine.Make sure that "$data['content']" having contents or check your database table structure.

Edited by Bachu: correction

0

This works fine:

echo '<div id="text">'.$data['content'].'</div>'.'<br>';

It just does not cut the text into the first 30 letters. but this:

echo '<div id="text">'.substr($data['content'], 0, 30).'</div>'.'<br>';

does not works. I wonder why?

1

You'll probably need to elaborate some more on "does not work", does it give an error back, or just the full string?

That function I posted before was something I wrote quick because I couldn't find the other function that I had wrote for trimming on character or word.

Here is the full function that will trim a string on character or word.

function stringTrim($string, $length, $split = 'word', $append = ' ...'){

    if($split == 'word'){

        $string_array = explode(' ', $string);

        if(count($string_array) > $length && $length > 0){
          $string = implode(' ', array_slice($string_array, 0, $length)) . $append;
          return $string;
        }
    }

    elseif($split == 'char'){
        $string = (strlen($string) > $length) ? substr($string,0,$length) . $append : $string;
        return $string;
    }

    else{
        return $string;
    }

}

Then you would just call it like stringTrim($string, 30, 'char') for trimming the string by the character. If you leave out the 'char' in the function, it will default to trimming by the word.

0

This is what I have now:

index.php

<div id="news">LATEST NEWS</div>

<?php

include('include/con_database.php');

$result = mysql_query("SELECT * FROM dynamic_content WHERE id='124'");

while ($data = mysql_fetch_array($result)){

echo '<div id="banner">';
echo '<div id="title">';
echo '<h1>'.'<a href="http://localhost/IndonusaCMS/events.php">'.$data['title'].'</a>'.'</h1>';
echo '</div>';

echo '<div id="desc">';
echo '<td>'.'<div id="images">'.'<img src="images/events/thumb/' . $data['images'] . '">'.'</div>'.'</td>'.'<br>';

echo '<div id="text">'.stringTrim($data['content'], 30, 'char').'</div>'.'<br>';

/*
echo '<div id="text">'.substr($data['content'], 0, 30).'</div>'.'<br>';
*/
echo '</div>';
echo '</div>';

}

?>

<?php 



    function stringTrim($string, $length, $split = 'word', $append = ' ...'){
    if($split == 'word'){
    $string_array = explode(' ', $string);
    if(count($string_array) > $length && $length > 0){
    $string = implode(' ', array_slice($string_array, 0, $length)) . $append;
    return $string;
    }
    }
    elseif($split == 'char'){
    $string = (strlen($string) > $length) ? substr($string,0,$length) . $append : $string;
    return $string;
    }
    else{
    return $string;
    }
    }

?>

Now, nothing appears on the front page ( I mean the content does not appears on the front page ).

0

note: I copy and paste the function exactly as it is and use it directly.

0

This is also does not work, I wonder why?

index.php

<?php

include('include/con_database.php');

$result = mysql_query("SELECT * FROM dynamic_content WHERE CatID='4'");

while ($data = mysql_fetch_array($result)){

$string = $data['content'];

echo '<div id="banner">';
echo '<div id="title">';
echo '<h1>'.'<a href="http://localhost/IndonusaCMS/events.php?id='.$data['ID'].'">'.$data['title'].'</a>'.'</h1>';
echo '</div>';

echo '<div id="images">'.'<img src="images/events/thumb/' . $data['images'] . '">'.'</div>'.'<br>';

echo '<div id="desc">';
/* echo '<div id="text">'.stringTrim($data['content'], 30, 'char').'</div>'.'<br>'; */
echo substr($string, 0, 30); 

/*
echo '<div id="text">'.substr($data['content'], 0, 30).'</div>'.'<br>';
*/
echo '</div>';
echo '</div>';

}

?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.