Genesis730 Posted January 11, 2010 Share Posted January 11, 2010 So I have a database that stores First and last names, then echos them back to a website, as of now the entire first and last name echos back (John Smith) I want the last name to just display the last letter and a . (John S.) here's my code for echoing the database <?PHP $res = mysql_query("SELECT id, DATE_FORMAT(date, '%M %D %Y') as dt, firstname, lastname, testimonial FROM testimonials ORDER BY date DESC LIMIT 10"); while ($row = mysql_fetch_assoc($res)) { echo "<p align=\"right\"><i>{$row['firstname']} {$row['lastname']}<br />{$row['dt']}</i></p><br /><br />{$row['testimonial']}<br /><hr width=\"80%\" color=\"#000000\"><br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/188011-formatting-echo-from-database/ Share on other sites More sharing options...
oni-kun Posted January 11, 2010 Share Posted January 11, 2010 So I have a database that stores First and last names, then echos them back to a website, as of now the entire first and last name echos back (John Smith) I want the last name to just display the last letter and a . (John S.) here's my code for echoing the database <?PHP $res = mysql_query("SELECT id, DATE_FORMAT(date, '%M %D %Y') as dt, firstname, lastname, testimonial FROM testimonials ORDER BY date DESC LIMIT 10"); while ($row = mysql_fetch_assoc($res)) { echo "<p align=\"right\"><i>{$row['firstname']} {$row['lastname']}<br />{$row['dt']}</i></p><br /><br />{$row['testimonial']}<br /><hr width=\"80%\" color=\"#000000\"><br />"; } ?> The most un-taxing and efficient method would be to use substr. echo $row['firstname'] . substr($row['lastname'], 0 , 1) . "."; //John S. Link to comment https://forums.phpfreaks.com/topic/188011-formatting-echo-from-database/#findComment-992593 Share on other sites More sharing options...
trq Posted January 11, 2010 Share Posted January 11, 2010 Better still, format your data within your query. SELECT id, DATE_FORMAT(date, '%M %D %Y') as dt, firstname, CONCAT(SUBSTR(lastname, 0, 1),'.') AS formatted_lastname, testimonial FROM testimonials ORDER BY date DESC LIMIT 10 Link to comment https://forums.phpfreaks.com/topic/188011-formatting-echo-from-database/#findComment-992597 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.