Listing All Your Users PHP Help

First things first: before you can add delete buttons and profile pictures and worry about alignment. you need a list of all your users. This isn’t too hard; all you need is a simple SOL Query. You could do something like this:

SELECT *
FROM users;

Of course, that’s a bit of a brute force approach. There’s some refinement you can make to improve performance, make your code clearer, and generally be a good PHP and My SOL citizen. Again, first things first: you should get that Query into shape.

SELECTING What You Need (Now)

The thing about SELECT  is that it retrieves everything in a table Even worse, if you’re joining tables it retrieves everything in ail the tables that are joined In the case of the users table that’s not a particular problem because there’s not much to it Here are all the columns you’re going to grab with a SELECT

mysql describe users

my sql describe users

Before moving on take a look back again at Figure 11-1.You don’t need all this information.

Realistically you need first name last name the user id for a hyperlink to ow .s=r.cno, and the user’s email That SELECT is grabbing several unnecessary columns: facebook_url twitter_handle bio and user_pic_path. Why is this a big deal Every time you select all the entries from the users table you’re getting one more row And every column in that row is space bandwidth on your network and resources. Suppose that you have 100 users, or 1,000 users or 10,000 users each with 20-paragraph bios. Just by rot selecting  (and thereby ‘J’ selecting bio) from 15,,,,”5 you’re saving a lot of traffic and resource consumption. Getting only the information you need saves time and resources especially over the life of your application.For more detail on deciding what to select.

All you need is a few of the columns from users SELECT user_id first_name last name, email FROM users;

Look Ahead (But Not Too Far)

Here’s one of those situations for which looking ahead creates a dilemma It would be nice to add profile pictures of users to the admin page and you already know there’s a column with the path to those pictures in user_pic_path. Because you’re going to want that down the line you might be tempted to SELECT that column now. On the one hand it would be nice to have a SELECT that’s already set up for a future feature that you know you’ll want. On the other hand you’re not implementing that feature yet it’s just that you’ll have the data available when you  write that code.

In general, you should think about the implications of what you’re doing on future features but focus on code that solves current problems not future ones. Think about how slippery a slope this can become. You might start selecting the bio because one day you want to excerpt that on the admin ” page; you might go ahead and select social information to build more links to contact the user. Before you know it, you’re back to a SELECT  and grabbing far more information than you’re actually using. The good news is that you know it will be easy to add functionality (such as grabbing a user’s picture) when the time comes. It’s a simple change to your SELECTS. o,stop there and focus on writing code for existing work. Leave future work for the future. If at some point in your programming career-vou want to start

charging for your work, you’ll have to start Quoting estimates. You’ll have to consider how long (in hours or days) will it take you to implement each feature that your customer wants. You typically bill at least partly based on these estimates so this is important stuff. If you start calculating based on current and future functionality those estimates stop making much sense You end up overcharging, or worse, undercharging because you’re not doinq one thing at a time.

Building a Simple Admin Page

Now that you’ve configured a good SELECT statement it’s time to create another script Before you do that. though there’s another important decision to make: what to call this script The name might seem like a good idea because it’s for your admin page but take a moment to think through the implications of that choice Look back at the other script names you’ve used:

•create_user.php creat a new user

.show_user.php shows a user for a given user ID

.app_config.php configures your application

Each of these names describes what the script does That’s very helpful because it’s immediately clear how to use these scripts and even how they might interact For example if you were looking at these scripts for the first time you would probably conclude that creates a user and then likely hands over control to.

What does this script do Well it lists all the users Using the same naming logic as other scripts, (note the plural “users” here) is a better, more descriptive name Remember, listing and deleting users isn’t the only administrative function you’re going to have. What if you eventually need to add a form and script so that an admin can change a user’s password? You’ll need to come up with a name for that script. and admin.php still won’t be specific enough.

Start a new file call it  and begin by selecting all the users with just the information you need:

Building a Simple Admin Page

You should also go ahead and set up the shell of the HTML page (the parts that you know won’t be generated by your script):

Building a Simple Admin Page

You should also go ahead and set up the shell of the HTML page (the parts that you know won’t be generated by your script):

Building a Simple Admin Page

Building a Simple Admin Page

There’s not much to see yet. but you can still test to verify that you don’t have any errors in your PHP or HTML. Building a Simple Admin Page

Iterating Over Your Array

Now, you need to fill in a list item (<li>) for every user. You can build up the entire HTML string by using sprint f again:

Iterating Over Your Array

That’s a big string, but ultimately, it should result in something like this:

Iterating Over Your Array

Iterating Over Your Array

All you need to do now is to loop over each result from your query But that’s easy  you’ve done that before with code like this:

Iterating Over Your Array

Then, of course you can get each piece of data in the returned query with this.

Iterating Over Your Array

This statement gets a specific value-whatever is associated with 01_name from $row.

If you make that specific to your  users table and the columns that you know are being returned and then insert that into your HTML you end up with this.

Iterating Over Your Array

Iterating Over Your Array

That’s a long sprint f, but take a second look You’re simply putting a lot of things together there’s not hing here particularly tricky or difficult.

Your HTML Is Getting Dangerously Cluttered

Something is subtly happening as you write more and more complex PHP Early on, you had scripts that were all PHP and perhaps used echo to throw out a few lines of text. Then, you started writing scripts that had a block of PHP at the beginning and a bunch of PHP at the end. Then, there were scripts that inserted a little PHP here and there into the HTML at the end of the script.

NOW you have show_user.apps There’s a block of PHP some HTML.and then it gets messy You have PHP that does a good bit of HTML printing. NOW you could probably write that same
bit of output that churns out HTMLand then has lots of tiny PHP bits inserted here and there but it’s basically the same issue No matter how you cut it you’re going to end up with areal mixture of HTML and PHP.

You’ve just found one of the real dangers of PHP you’re going to end up mixing your code and your markup frequently As you start this sort of mixing the separation between your code and your view the markup that displays something to your user becomes thin if not non-existent It’s easy to just drop a big block of PHP in the middle of some HTML but in this case easy isn’t good As much as you can keep the bulk of your PHP at the beginning of your script and then just insert  data as you need it.

You’re ready to see how things look. Pull up show_user.php and verify that everything is where it belongs you what you’re going for Granted your page is still not a work of art but it’s a significant step forward Click any of the users and ensure that you’re taken to the correct show_user.php for that user.

Your HTML Is Getting Dangerously Cluttered

Your HTML Is Getting Dangerously Cluttered

 

PHP& MySQL

PHP & MySQL

 

Posted on January 13, 2016 in Listing Iterating and Administrating

Share the Story

Back to Top
Share This