0

I'm trying to return only the first 200 characters (if there are more than that) from a body field in a Drupal custom module. I've found the general php/mysql solution here:

https://stackoverflow.com/questions/2080860/mysql-sql-retrieve-first-40-characters-of-a-text-field

But when I try to implement it with my SQL query in my custom module it doesn't work (ie. it still returns over 200 characters). Here's my code (see the field I used LEFT() on is LEFT(fdb.body_value, 200) on line 6):

$result = db_query(
"SELECT n.title, n.nid, n.status, 
fdfty.field_type_value, fdfi.entity_id, 
fdfi.field_icon_fid, fm.uri, fm.fid, ua.pid, 
ua.alias, el.field_experience_level_value, 
**LEFT(fdb.body_value, 200)**, 
fdfr.field_role_value, fdft.field_topics_value, 
fdfed.field_event_dates_value, 
fdfed.field_event_dates_value2
FROM {node} AS n 
LEFT JOIN {field_data_field_type} AS fdfty 
ON n.nid=fdfty.entity_id 
LEFT JOIN {field_data_field_icon} AS fdfi 
ON n.nid=fdfi.entity_id 
LEFT JOIN {file_managed} AS fm 
ON fdfi.field_icon_fid=fm.fid 
LEFT JOIN {url_alias} AS ua 
ON n.nid=ua.pid 
LEFT JOIN {field_data_field_experience_level} AS el 
ON n.nid=el.entity_id 
LEFT JOIN {field_data_body} AS fdb 
ON n.nid=fdb.entity_id 
LEFT JOIN {field_data_field_role} AS fdfr
ON n.nid=fdfr.entity_id 
LEFT JOIN {field_data_field_topics} AS fdft 
ON n.nid=fdft.entity_id 
LEFT JOIN {field_data_field_event_dates} AS fdfed 
ON n.nid=fdfed.entity_id 
WHERE n.status=:status AND n.type=:resource 
AND (fdfty.field_type_value=:webinar OR fdfty.field_type_value=:conference) AND el.field_experience_level_value=:level 
AND fdfr.field_role_value=:role 
AND fdft.field_topics_value=:topic 
AND ((fdfed.field_event_dates_value IS NULL) 
OR (fdfed.field_event_dates_value >= CURRENT_DATE())) 
GROUP BY n.nid, n.title 
ORDER BY fdfed.field_event_dates_value", 
array(':status' => '1', ':resource' => 'resource',
':webinar' => 'Webinar', ':conference' => 'Conference', ':level' => $level, ':role' =>
$role, ':topic' => $topic));

1 Answer 1

1

why not use substr() let the SQL statement bring it all then use this function in your result

would be something like this:

<?php

$result = db_query('Your SQL statement');

print substr($result, 0 , 199);

?> 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.