Membership
Main Menu
Forum Boards
Stats
- 20 tutorials
- 74,811 members
- 734,829 forum posts
- 13 blog posts
Tutorials
PHP Basic Database Handling
Views: 67577
List Everything Out
This is the part of our script where we list out the current info in our database table. We start out with a while loop. Why? Okay, remember earlier, the last thing we did before listing stuff out, was select all the info from the database table, and stored it in $result? Okay, that information is called a result source. Think of $result as a magician's hat. PHP provides several functions for us to reach inside that hat and pull out what we need. What we pull out depends on which one of those functions we use.
Somewhere in that black hat is our list of information, and there's a rabbit somewhere in that hat and he has that information, and when we reach inside the hat, he gives us what we need, one row at a time. When he hands us that row, he picks up the next row and waits for us to reach in again.
The row he hands us is stored in an array. What kind of array depends on what function we use. We used mysql_fetch_assoc which returns an associative array. We used an associative array because the keys to the array are labeled the same as our database table columns, which makes it a lot easier for me to keep my head wrapped around my code when trying to type it and read it.
So basically, our array will look like this: ('id' => 1, 'name' => 'Josh') instead of this: (0 => 1, 1 => 'Josh). If numerical indexes float your boat, you can use mysql_fetch_row instead. If for some reason you want both returned, you can use mysql_fetch_array. Be warned though, mysql_fetch_array merges row and assoc together, giving you an array twice as long in the end. As if that weren't enough overkill, mysql_fetch_array can have an optional 2nd argument to return only one or the other (numerical or associative), making it function the exact same as _assoc or _row.
Since we don't know how many rows there are at any given time, we use a while loop. Basically the loop says this: while the rabbit still has rows to hand us, assign the current row he gives us to $list (making it an associative array of our data), and use $list to display the information to our user.
Inside the loop we echo out the id in the first column. We do not make this editable, because MySQL assigns that automatically. It's the row's fingerprint. We then echo out an input text field. We assign a array name to it so we can cycle through it as a list, back up in that UPDATE code block. We assign the row's id number as the array position, so that we can pass the id value with the form, as described in that same previous code block. The value is the name in the database row, so that it shows as the "default" in the text field. The user can edit it, and the UPDATE code block will update it.
Last of all, we make a "delete" link in the last column of our html table. The link will pass the name so that it can be retrieved with the GET method up in the "DELETE" code block.
