views:

44

answers:

4

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.

+1  A: 

PHP has a plenty of string functions. You can use any you wish

Col. Shrapnel
See string manipulation code in my edited question. I want to apply it to an entire column or as a foreach loop. I can't get it to work.
ggg
@ggg put this code inside of foreach loop.
Col. Shrapnel
There answer probably lies in using either the foreach function, array_walk or array_map but I don't know the proper syntax.
ggg
@ggg well I bet you re using this query result somehow. Using `while` loop I suppose. So, put this code inside of while loop. You already know every bit of the solution. Try to put it together. Try to think!
Col. Shrapnel
A: 

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.

Karthik
See question update with code added.
ggg
+2  A: 

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)

matei
Both first and last are in the same column. See string manipulation code in my edited question.
ggg
you can't apply the function directly on a db resultset, you need to either fetch the resutlt into an array, and apply the function while you are doing that (so $row = mysql_fetch_array($result); flip($row); - where flip is your function and $row is passed by reference) or apply it after you've feched it with array_walk($rows, 'flip')
matei
If my resulting query is called $ginfo and the column in question is $ginfo->White, what is the syntax for array_walk that will work for me?
ggg
It depends what you mean by $ginfo and how you store your database info in PHP . I was telling you earlier about a case where you store all your rows in an array , and each row is an array. but I can see you are using objects, so I can't tell for your case. If you want an sql solution (probably this is the best way to do it): SELECT SUBSTRING(name,1, LOCATE(",",name)-1 ) AS firstname, SUBSTRING(name, LOCATE(",",name)+1, LENGTH(name)-LOCATE(",",name)) AS lastname FROM table should do the trick
matei
$ginfo is a mysql database query result.
ggg
$ginfo doesn't have the row data, it's merely a link to a query result stored on the server. To actually get the row data as an array, you need to loop over one of the various `fetch` functions to grab each row of the result set.
Amber
A: 

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.

Amber
I have the function, see my question. The problem is coming up with the syntax so that the function applies to the entire column.
ggg