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. Quote Link to comment 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 Quote Link to comment 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; Quote Link to comment 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; Quote Link to comment 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; ?> Quote Link to comment 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 !"; } } } ?> Quote Link to comment 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 !"; } } } ?> Quote Link to comment 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... Quote Link to comment 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 !"; } } } ?> Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.