Hello, guys.. I am actually working on a project. I can't figure out how to limit the displayed content on a table of a particular data.

I have a table (tblservices) that have fields (service_id, service_name, service_content, date_created).. the service_name is the title of the service, service_content is the whole description and guideline of the service. Now, I want to display all the data on tblservices (SELECT * FROM tblservices) in a table. I succesfully displayed all the data on the table, but on the column of service_content, I just want to display a short part of the service_content not the whole data of the service_content.

Please, help me on how to do it. I really appreciate any help. Thank you. Merry Christmas.

Hi ;
if you write your code it will be easer but any way you can use condtion
where statment or you can use store it in array and then remove what you dont want to show by loop .

Member Avatar for diafol

There are two main ways to 'limit' your results, you can use a filter with the WHERE clause or limit to a certain section of the resultset with the LIMIT clause. Or you could use a combination of both. Examples:

SELECT field1, field2 FROM table WHERE fieldx = 'something'
SELECT field1, field2 FROM table WHERE field_id < 100
SELECT field1, field2 FROM table WHERE field_id BETWEEN 100 AND 200
SELECT field1, field2 FROM table WHERE fieldx LIKE '%something%'
SELECT field1, field2 FROM table LIMIT 100
SELECT field1, field2 FROM table LIMIT 20, 10
SELECT field1, field2 FROM table WHERE fieldx LIKE 'A%' LIMIT 20

What I am trying to say is to lessen the data of the particular field that will be displayed on the table.. Here is my code:

<h2>Services</h2>
  <table width="1000" height="104" bordercolor="#666666" border="0">

   <tr bordercolor="#666666">
  <center>
    <td width="80" height="33" bgcolor=" #d4d6f8" align="center"><b>Service Name</b></td>
    <td width="50" bgcolor="#d4d6f8" align="center"><b>Service Content</b></td>
    <td width="60" bgcolor=" #d4d6f8" align="center"><b>preview</b></td>
    <td width="60" bgcolor=" #e1e2f7" align="center"><b>edit</b></td>
    <td width="60" bgcolor=" #e1e2f7" align="center"><b>delete</b></td>

  </center>
  </tr>
<?php
$result=mysql_query("SELECT * FROM tblservices");

$row = mysql_fetch_array($result, MYSQLI_ASSOC);

if(! $row) {
    echo "<p>No description</p>";
}
while($row = mysql_fetch_array($result))
{

  ?>


  <tr>
     <td height="35" align="center"><?php echo $row['service_name']; ?></td>
    <td ><?php echo $row['service_content']; ?></td>
     <td align="center"><a href="contentmod.php?id=<?php echo $row['service_id']?>">Preview</a></td>
     <td align="center"><a href="edit_content.php?id=<?php echo $row['service_id']?>">EDIT</a></td>
     <td align="center"><a href="delete.php?id=<?php echo $row['service_id']?>">DELETE</a></td>

  </tr>

<?php
} 

?>

the service_content is actually have a long data.. Its somewhat like an article.. I just want to displayed it on the table the little part of the of the service_content. On the PREVIEW, it will show the whole content of the article to another page.

Please help me..

Decide on how many characters you can display. Then change line 30 from:

<td ><?php echo $row['service_content']; ?></td>

to:

<td >
<?php 
$contentLength = strlen($row['service_content']);
// check if the length of the content exceeds max number of chars e.g. 120
if($contentLength > 120) {
    echo substr($row['service_content'], 0, 120) . '...';
} else {
    echo $row['service_content'];
}
?>
</td>
commented: You read it correctly, I didn't ;) +14
Member Avatar for diafol

Ahh. Sorry misread.

Alternatively, you could use the mb_* function:

echo mb_strimwidth($row['service_content'], 0, 120, "...");

That way you don't need to check before including the 'ellipsis'. BUT the fourth parameter is included in the character count, so the above would give 117 characters from the string and tag on three periods.

Thank You guys. It worked. :))) Thanks for helping me. :DD Happy New Year guys.. :DDD

You are welcome. Hopefully you used the diafol's solution since it caters for multibyte strings. Quite important if you plan to deal with non-english language characters.

Happy coding in 2014.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.