Ok, the way I get data from the DB now is much like this:
function db_select($table, $fields, $where) {
...
}
This function creates a select statement based on the criteria I pass
in and returns a single row which I can access like this in PHP:
<?
$row = db_select('users', 'first_name,last_name,phone', 'user_id=21');
?>
User: <?=$row['last_name']?> , <?=$row['first_name']?><br>
Phone Number: <?=$row['phone']?>
I use another function to retrieve multiple rows:
function db_select_list($table, $rows, $key, $value, $where, $limit,
$order) {
...
}
Like db_select(), it creates the SQL query for me. But when it receives
data back from the database it loops through it and creates a
multi-dimensional array for me. If I wanted an array of 10 users' names
and phone numbers, ordered and indexed by their id # I'd do this:
$users = db_select_list('users', "user_id,phone,CONCAT('first_name','
','last_name) AS name", 'user_id', '', '10', 'user_id');
now I can do this:
<table>
<tr>
<th> </th>
<th>Name</th>
<th>Phone Number</th>
</tr>
<?
foreach( $users as $user_id => $user_data )
{
?>
<tr>
<td><a href="/edit_user.php?user_id=<?=$user_id?>">Edit</a></td>
<td><?=$user_data['name']?></td>
<td><?=$user_data['phone']?></td>
</tr>
<?
}
?>
</table>
One thing I've thought of is just using lists to handle this with a
custom API. So, I'd have a list where index 0 contains a list of the
key/field names for the rows, and 1-N would contain the rows. Then I
could just use the API like this:
set users [db_select_list ...]
foreach { row } { [my_array rows $users] } {
set name [lindex [lindex $users $row] [my_array getkeyindex "name"]]
...
}
It's a bit kludgey, but I suppose that would work.
- Gabriel
On Thursday, November 7, 2002, at 10:30 PM, Michael A. Cleverly wrote:
On Thu, 7 Nov 2002, Gabriel Ricard wrote:
Wonderful!
Now my only gripe is the multi-dimensional array issue, hehe. At least
there are lots of solutions for that problem available.
I'm not at all familiar with PHP. Could you describe (possibly
included
actual PHP code snipets or other pseudo-code) what it is you accomplish
with multi-dimensional arrays? Then we should be able to help flush out
which of the many solutions would be most apropriate.
Michael