Page 1 of 1
Customizing a php users page
Posted: Thu Aug 26, 2010 12:21 pm
by Rajada
So, I'm designing a website (who isn't?) and I created the basic framework for a users page from a tutorial I found. Using some previous knowledge I managed to make it display a few custom fields that are defined by the user. Everything works fine as is, but now I want to do a few things to it that I have not the slightest clue how to even begin...
Here is my user page code so far... and oh yes I'm using WordPress which is why I made it check manually for page status in my Page.php file.
Code: Select all
<?php
if ( is_page('Users'))
{
echo "<ul id=\"UsersList\">";
/*
First we set how we'll want to sort the user list. You could sort them by:
------------------------
* ID - User ID number.
* user_login - User Login name.
* user_nicename - User Nice name ( nice version of login name ).
* user_email - User Email Address.
* user_url - User Website URL.
* user_registered - User Registration date.
*/
$szSort = "user_nicename";
/*
Now we build the custom query to get the ID of the users.
*/
$aUsersID = $wpdb->get_col( $wpdb->prepare("SELECT $wpdb->users.ID FROM $wpdb->users ORDER BY %s ASC", $szSort ));
/*
Once we have the IDs we loop through them with a Foreach statement.
*/
foreach ( $aUsersID as $iUserID ) :
/*
We use get_userdata() function with each ID.
*/
$user = get_userdata( $iUserID );
/*
Here we finally print the details wanted.
Check the description of the database tables linked above to see
all the fields you can retrieve.
To echo a property simply call it with $user->name_of_the_column.
*/
if($user->user_login != "Unknown") // don't show the placeholder for [unknown] author
{
echo '<a href="">' . get_avatar( $iUserID, $size = '45', $border='0') . '</a>';
echo '<li>' . ucwords( strtolower( $user->user_login ) ) . '</li>';
if($user->favorite_player != "")
{
echo '<li>' . $user->favorite_player . '</li>';
}
if($user->player_name != "")
{
echo '<li>' . $user->player_name . '</li>';
}
}
/*
The strtolower and ucwords part is to be sure
the full names will all be capitalized.
*/
endforeach; // end the users loop.
echo "</ul>";
};
?>
Problem one: This does NOT sort my name, despite the tutorial's insistence that it will. I have not even a guess as to why this is.
Problem two: I would like to either sort this list into two columns or paginate it or both but I am not sure how to do either.
Problem three: I want to insert some static text between the echo '<li>' and the . $user->player_name . '</li>'; so that it reads:
o Player Name: USER'S VARIABLE ' PLAYER NAME' HERE
Yes that 'o' is supposed to be the list item dot. I know how strings work, I just can't get my attempts to work out syntax-wise.
Any help would be greatly appreciated! Tutorials, answers, suggestions, examples, anything. The extent of my previous coding knowledge is several years of UnrealScript, so you can see why this simple thing is baffling me. Frankly I'm surprised this much of it works.

Re: Customizing a php users page
Posted: Fri Aug 27, 2010 11:46 am
by RobertGonzalez
Answer 1: Your select query is pulling IDs from the user table, ordered by the users user_nicename field. That is the sort. The IDs are in order of user_nicename.
Answer 2: Two columns would mean knowing your halfway limit and, during the loop, checking sure to stay within that halfway limit for one column and closing the column and opening another one for the second half of the limit. This is, of course, if you want half the data in one column and the other half in another column, in order. Otherwise you could just make two HTML elements side by side and write to them for however many users you have.
Answer 3: Where ever you want your static text to appear, write it there. Try some things, play around a little and have some fun developing. Then, when it is all the way you want, send those changes to your production server.
Re: Customizing a php users page
Posted: Fri Aug 27, 2010 12:44 pm
by Rajada
RobertGonzalez wrote:Answer 1: Your select query is pulling IDs from the user table, ordered by the users user_nicename field. That is the sort. The IDs are in order of user_nicename.
Yes, they should be in that order... but they're not, that's kind of the problem...
RobertGonzalez wrote:Answer 2: Two columns would mean knowing your halfway limit and, during the loop, checking sure to stay within that halfway limit for one column and closing the column and opening another one for the second half of the limit. This is, of course, if you want half the data in one column and the other half in another column, in order. Otherwise you could just make two HTML elements side by side and write to them for however many users you have.
Complicated, I'd rather paginate them.
RobertGonzalez wrote:Answer 3: Where ever you want your static text to appear, write it there. Try some things, play around a little and have some fun developing. Then, when it is all the way you want, send those changes to your production server.
The issue is syntax in this case... if i just write:
echo '<li>' Favorite Player: $user->favorite_player . '</li>';
I get the error on my page:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/nabcommunity/nerfarena.net/wp-content/themes/andyblue/page.php on line 66
I've tried just about every way of fitting that in there but it totally fails on me every time.
Re: Customizing a php users page
Posted: Fri Aug 27, 2010 12:57 pm
by Jonah Bron
Hint: Only text, and all text, goes inside of a string. <li> is text, and it's inside of a string.
Re: Customizing a php users page
Posted: Fri Aug 27, 2010 1:12 pm
by Rajada
Jonah Bron wrote:Hint: Only text, and all text, goes inside of a string. <li> is text, and it's inside of a string.
Thank you, finally an answer that makes sense, that fixed problem 3 perfectly. I am still extremely confused about problem 1 though, the fact that this doesn't sort by user name (nicename is fine, that's what I want it to sort by).
Re: Customizing a php users page
Posted: Fri Aug 27, 2010 1:21 pm
by RobertGonzalez
Just for grins could you do this and post back your results:
Code: Select all
$aUsersID = $wpdb->get_col( $wpdb->prepare("SELECT $wpdb->users.user_nicename FROM $wpdb->users ORDER BY %s ASC", $szSort ));
var_dump($aUsersId); exit;
Note: This is going to render your query results to the screen and halt processing of the script from that point. If this is something that is visible to your users then perhaps you could just run the query in your database client and capture the results that way.
Re: Customizing a php users page
Posted: Fri Aug 27, 2010 3:18 pm
by Rajada
My results are with all that code I get NULL
without the var dump and exit i get a bunch of blank results... like
[BLANK AVATAR]
USER:
Re: Customizing a php users page
Posted: Fri Aug 27, 2010 6:30 pm
by RobertGonzalez
Sorry, the var_dump should be var_dump($aUsersID) not var_dump($aUsersId). I used a small "d" when I should have used a big "D". Try it again.
Re: Customizing a php users page
Posted: Sat Aug 28, 2010 2:33 pm
by Rajada
I get:
array(12) { [0]=> string(6) "b-ball" [1]=> string(6) "boofer" [2]=> string(4) "g-mo" [3]=> string(6) "jaf630" [4]=> string(3) "jay" [5]=> string(5) "jwill" [6]=> string(8) "karatana" [7]=> string(6) "nobody" [8]=> string(12) "qwertykeyman" [9]=> string(6) "rajada" [10]=> string(7) "unknown" [11]=> string(14) "usmc-mmm-davey" }
So in other words it gave me all their user names... I assume this test was to see if the variable existed, right? Oh, and that is in alphabetical order... so why doesn't my way work?
Re: Customizing a php users page
Posted: Sun Aug 29, 2010 9:10 am
by RobertGonzalez
Now do this:
Code: Select all
$aUsersID = $wpdb->get_col( $wpdb->prepare("SELECT $wpdb->users.ID, $wpdb->users.user_nicename FROM $wpdb->users ORDER BY %s ASC", $szSort ));
var_dump($aUsersID); exit;
Where $szSort is still 'user_nicename'. Post back the results.
Re: Customizing a php users page
Posted: Sun Aug 29, 2010 1:32 pm
by Rajada
I get:
array(12) { [0]=> string(1) "2" [1]=> string(1) "6" [2]=> string(1) "4" [3]=> string(1) "5" [4]=> string(1) "7" [5]=> string(1) "9" [6]=> string(2) "10" [7]=> string(2) "11" [8]=> string(2) "12" [9]=> string(2) "13" [10]=> string(2) "14" [11]=> string(2) "15" }
Which means somewhere along the lines its still grabbing user id number and not the nicename?
Re: Customizing a php users page
Posted: Mon Aug 30, 2010 11:14 am
by RobertGonzalez
Something's wrong there. You should have two results per row of returned data since you are selected both the ID and user_nicename. Hmmm, that is an interesting result. I wouldn't expect their query parser to do something odd like that.
Re: Customizing a php users page
Posted: Mon Aug 30, 2010 11:53 am
by mikosiko
if my memory doesn't fail the result is correct because the method $wpdb->get_col only return one specific column ... if you query select more than one column the result is cached.. only shows the first column (In this case the ID).. you can get the second (or remaining) column using an offset.
alternative for $wpdb->get_col is $wpdb->get_results to show more than 1 column.
Ramada: Which order are you expecting because according to the query/results posted here you have this now:
ID nice_name
"2" "b-ball"
"6" "boofer"
"4" "g-mo"
"5" "jaf630"
"7" "jay"
"9" "jwill"
"10" "karatana"
"11" "nobody"
"12" "qwertykeyman"
"13" "rajada"
"14" "unknown"
"15" "usmc-mmm-davey"
Re: Customizing a php users page
Posted: Mon Aug 30, 2010 12:46 pm
by RobertGonzalez
Dude, that makes sense. I was paying more attention to the query than the result fetching method. DOH!

Re: Customizing a php users page
Posted: Mon Aug 30, 2010 2:10 pm
by Rajada
mikosiko wrote:if my memory doesn't fail the result is correct because the method $wpdb->get_col only return one specific column ... if you query select more than one column the result is cached.. only shows the first column (In this case the ID).. you can get the second (or remaining) column using an offset.
alternative for $wpdb->get_col is $wpdb->get_results to show more than 1 column.
Ramada: Which order are you expecting because according to the query/results posted here you have this now:
ID nice_name
"2" "b-ball"
"6" "boofer"
"4" "g-mo"
"5" "jaf630"
"7" "jay"
"9" "jwill"
"10" "karatana"
"11" "nobody"
"12" "qwertykeyman"
"13" "rajada"
"14" "unknown"
"15" "usmc-mmm-davey"
Using get_results returns a bunch of blank users again.
The result of the dump looks right, but it sorts wrong visually...
using
...
Code: Select all
$szSort = 'user_nicename';
$aUsersID = $wpdb->get_results( $wpdb->prepare("SELECT $wpdb->users.ID, $wpdb->users.user_nicename FROM $wpdb->users ORDER BY %s ASC", $szSort ));
var_dump($aUsersID); exit;
...
I get:
array(12) { [0]=> object(stdClass)#38 (2) { ["ID"]=> string(1) "2" ["user_nicename"]=> string(3) "jay" } [1]=> object(stdClass)#37 (2) { ["ID"]=> string(1) "6" ["user_nicename"]=> string(6) "jaf630" } [2]=> object(stdClass)#36 (2) { ["ID"]=> string(1) "4" ["user_nicename"]=> string(6) "rajada" } [3]=> object(stdClass)#10 (2) { ["ID"]=> string(1) "5" ["user_nicename"]=> string(6) "boofer" } [4]=> object(stdClass)#11 (2) { ["ID"]=> string(1) "7" ["user_nicename"]=> string(12) "qwertykeyman" } [5]=> object(stdClass)#12 (2) { ["ID"]=> string(1) "9" ["user_nicename"]=> string(6) "nobody" } [6]=> object(stdClass)#45 (2) { ["ID"]=> string(2) "10" ["user_nicename"]=> string(7) "unknown" } [7]=> object(stdClass)#46 (2) { ["ID"]=> string(2) "11" ["user_nicename"]=> string(4) "g-mo" } [8]=> object(stdClass)#49 (2) { ["ID"]=> string(2) "12" ["user_nicename"]=> string(6) "b-ball" } [9]=> object(stdClass)#47 (2) { ["ID"]=> string(2) "13" ["user_nicename"]=> string(14) "usmc-mmm-davey" } [10]=> object(stdClass)#51 (2) { ["ID"]=> string(2) "14" ["user_nicename"]=> string(8) "karatana" } [11]=> object(stdClass)#50 (2) { ["ID"]=> string(2) "15" ["user_nicename"]=> string(5) "jwill" } }
Which is in fact the bad, messed up order.
If I remove the dump command, I get that blank users crud.