Customizing a php users page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rajada
Forum Newbie
Posts: 7
Joined: Thu Aug 26, 2010 12:08 pm

Customizing a php users page

Post 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. :P
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Customizing a php users page

Post 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.
Rajada
Forum Newbie
Posts: 7
Joined: Thu Aug 26, 2010 12:08 pm

Re: Customizing a php users page

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Customizing a php users page

Post by Jonah Bron »

Hint: Only text, and all text, goes inside of a string. <li> is text, and it's inside of a string.
Rajada
Forum Newbie
Posts: 7
Joined: Thu Aug 26, 2010 12:08 pm

Re: Customizing a php users page

Post 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).
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Customizing a php users page

Post 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.
Rajada
Forum Newbie
Posts: 7
Joined: Thu Aug 26, 2010 12:08 pm

Re: Customizing a php users page

Post 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:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Customizing a php users page

Post 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.
Rajada
Forum Newbie
Posts: 7
Joined: Thu Aug 26, 2010 12:08 pm

Re: Customizing a php users page

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Customizing a php users page

Post 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.
Rajada
Forum Newbie
Posts: 7
Joined: Thu Aug 26, 2010 12:08 pm

Re: Customizing a php users page

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Customizing a php users page

Post 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.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Customizing a php users page

Post 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"
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Customizing a php users page

Post by RobertGonzalez »

Dude, that makes sense. I was paying more attention to the query than the result fetching method. DOH! :oops:
Rajada
Forum Newbie
Posts: 7
Joined: Thu Aug 26, 2010 12:08 pm

Re: Customizing a php users page

Post 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.
Post Reply