Germaris Posted July 24, 2007 Share Posted July 24, 2007 Hi there ! I manage a directory of former students. Among many functions, users can perform a search which returns the full identity of a friend. In the MySQL table there's a row nammed firstName which can contain up to four names. How can I modify my script in order to retrieve only the first name in the row i.e. the first word before a space? Many thanks in advance for your help! Best regards. Link to comment https://forums.phpfreaks.com/topic/61582-solved-only-the-first-word/ Share on other sites More sharing options...
per1os Posted July 24, 2007 Share Posted July 24, 2007 www.php.net/substr www.php.net/explode Link to comment https://forums.phpfreaks.com/topic/61582-solved-only-the-first-word/#findComment-306492 Share on other sites More sharing options...
The Little Guy Posted July 24, 2007 Share Posted July 24, 2007 list($first) = explode(" ",$row['firstName']); echo $first; Link to comment https://forums.phpfreaks.com/topic/61582-solved-only-the-first-word/#findComment-306494 Share on other sites More sharing options...
trq Posted July 24, 2007 Share Posted July 24, 2007 Best to do it in your query. SELECT SUBSTRING_INDEX(fld,' ',1) FROM tbl; Link to comment https://forums.phpfreaks.com/topic/61582-solved-only-the-first-word/#findComment-306495 Share on other sites More sharing options...
redarrow Posted July 24, 2007 Share Posted July 24, 2007 <?php $x=array("john petter paul chris"); $t=implode(' ',$x); $r=explode(' ',$t); $name1=$r[0]; echo $name1; ?> Link to comment https://forums.phpfreaks.com/topic/61582-solved-only-the-first-word/#findComment-306498 Share on other sites More sharing options...
Germaris Posted July 24, 2007 Author Share Posted July 24, 2007 Ouf !!! I didn't expect so much replies !!! Thank you, thank you, thank you ! BUT : I don't understand to use this treasure inside my script of which you'll find the interesting part below... <?php function hello($unlock,$username,$userpass) { if ($unlock < 1) print "Acces interdit."; else { GLOBAL $db,$table; $username = trim($username); $password = md5(trim($userpass)); $query = mysql_query("SELECT * FROM $table WHERE userName = '$username' AND userPassword = '$password'") or die("&erreur=Connexion impossible avec la base de données de l'Annuaire."); if ( mysql_num_rows( $query ) > 0) { while ($row =mysql_fetch_array ($query ) ) { $flashstr .= "Bonjour ".$row ["firstName" ]." !"; } print "&prenomutilisateur=".$flashstr; } else { print "&prenomutilisateur=Bonjour, cher camarade !"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/61582-solved-only-the-first-word/#findComment-306516 Share on other sites More sharing options...
The Little Guy Posted July 24, 2007 Share Posted July 24, 2007 <?php function hello($unlock,$username,$userpass) { if ($unlock < 1) print "Acces interdit."; else { GLOBAL $db,$table; $username = trim($username); $password = md5(trim($userpass)); $query = mysql_query("SELECT * FROM $table WHERE userName = '$username' AND userPassword = '$password'") or die("&erreur=Connexion impossible avec la base de données de l'Annuaire."); if ( mysql_num_rows( $query ) > 0) { while ($row =mysql_fetch_array ($query ) ) { list($first) = explode(" ",$row['firstName']); $flashstr .= "Bonjour ".$first." !"; } print "&prenomutilisateur=".$flashstr; } else { print "&prenomutilisateur=Bonjour, cher camarade !"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/61582-solved-only-the-first-word/#findComment-306522 Share on other sites More sharing options...
Germaris Posted July 24, 2007 Author Share Posted July 24, 2007 Crystal clear, Little Guy, but it doesn't work... :-( There's a mistake before the first print instruction but I can't find it... Link to comment https://forums.phpfreaks.com/topic/61582-solved-only-the-first-word/#findComment-306544 Share on other sites More sharing options...
The Little Guy Posted July 24, 2007 Share Posted July 24, 2007 <?php function hello($unlock,$username,$userpass) { if($unlock < 1) { print "Acces interdit."; } else { GLOBAL $db,$table; $username = trim($username); $password = md5(trim($userpass)); $query = mysql_query("SELECT * FROM $table WHERE userName = '$username' AND userPassword = '$password'") or die("&erreur=Connexion impossible avec la base de données de l'Annuaire."); if ( mysql_num_rows( $query ) > 0) { while ($row =mysql_fetch_array ($query ) ) { list($first) = explode(" ",$row['firstName']); $flashstr .= "Bonjour ".$first." !"; } print "&prenomutilisateur=".$flashstr; } else { print "&prenomutilisateur=Bonjour, cher camarade !"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/61582-solved-only-the-first-word/#findComment-306548 Share on other sites More sharing options...
Germaris Posted July 24, 2007 Author Share Posted July 24, 2007 This very last version works perfectly! Congratulations and many thanks for your valuable time and help! Best regards. Link to comment https://forums.phpfreaks.com/topic/61582-solved-only-the-first-word/#findComment-306604 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.