How do I apply a string function to a table column?
As part of a query result, a column with names is returned. I want to apply a function so that the order of first and last name is flipped in my $db results. What is the most efficient way to accomplish this?
There answer probably lies in using either the foreach function, array_walk or array_map but I don't know the proper syntax.
This function does the name flip:
$name = "Lastname, Firstname";
$names = explode(", ", $name);
$name = $names[1] . " " . $names[0];
The $query is similar to this:
$query0="SELECT #__1pgndata.White, #__1pgndata.Black, #__1pgndata.ECO, #__1pgndata.Result, #__1pgndata.EventDate, #__1pgndata.Id
FROM `#__1pgndata` Where #__1pgndata.Id > 155 LIMIT 30"
White and Black are columns for player names (need to be flipped) and开发者_如何转开发 correspond to the color of chess pieces. The columns in question are $ginfo->White and $ginfo->Black.
if you are speaking about the order they are put in the array , you can just select them reversed (for example SELECT first_name, last_name, .. FROM users...) but I suggest you use mysql_fetch_array, that way you will be able to access them as you wish (with $row['first_name'], $row['last_name'] in whatever order you want)
PHP has a plenty of string functions. You can use any you wish
You can attempt to replicate the functionality using the MySQL String Functions. The most likely candidate would probably a combination of LOCATE()
to find the position of the ', '
and then a pair of SUBSTR()
calls to get the portions before and after, then using CONCAT()
to put them back together.
would you provide your db code?
$query0="SELECT #__1pgndata.Black, #__1pgndata.White, #__1pgndata.ECO, #__1pgndata.Result, #__1pgndata.EventDate, #__1pgndata.Id
FROM #__1pgndata
Where #__1pgndata.Id > 155 LIMIT 30"
you can use like this also right.
精彩评论