1

I am working with PHPMyadmin and I have a field called The_Job, in which there is a vast description of a job. I have created a .php document where I have added a dinamic table with all the rows from my database. The_job is one of them, and what I want to do is to add only a fragment of the data from the database field The_Job into the table.

Which means that I have to somehow cut down the text to a limited "x" number of characters and add "..." at the end.

How can I do that?

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • Use https://www.w3schools.com/php/func_string_substr.asp than add "..." at the end – Richard Mar 04 '18 at 18:16
  • Possible duplicate of [MySQL Select Query - Get only first 10 characters of a value](https://stackoverflow.com/questions/14959166/mysql-select-query-get-only-first-10-characters-of-a-value) – astroanu Mar 04 '18 at 18:43

1 Answers1

2

You did not provide any data and example of what you tried, but I assume that you want a few of characters from the field and then ... The SQL function LEFT is what you are looking for:

SELECT CONCAT(SUBSTR(The_Job, x, y),'...') FROM table

This will substract characters x to y (so you would write something like SUBSTR(The_Job, 1, 10) for character 1 to 10

or

SELECT CONCAT(LEFT(The_Job, x),'...') FROM table

This will select first x characters of The_Job.

UPDATE: I changed the operator || to CONCAT.

honzajolic
  • 56
  • 4
  • Although it generally works alright right now, there is something wierd. I am using now this: substr($row['The_Job'],0,145). And it works somehow wierd, adding a strange symbol � at the end. Which means that it is not seeing my russian encoding. – Laughin Coffin Mar 04 '18 at 18:36
  • @LaughinCoffin You want to use `mb_substr` so it doesn't chop multi-byte chars. But in your next question, you really don't need to do this. – IncredibleHat Mar 05 '18 at 18:55
  • It's worth stating that 'x' cannot be 0 for SUBSTR it should be 1 to get the first character. eg: SELECT SUBSTR(columnName, 1, y) would give a string starting from the first character to 'y'. – Paul V Jan 29 '22 at 05:02