Jump to content

Formatting echo from database


Genesis730

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.