OK, obviously I wasn't awake last night ... figured out what I was actually doing here ... specifying the index for the 2nd array instead of the value in the first ... *sigh* *blushes* ... how do you say "tanketorsk" in English ??? .... anyway, sorry for wasting bandwidth, I'll go hide in the corner now :P


At 06:46 15-09-2004, -{ Rene Brehmer }- wrote:
This is some experimental code I did to try and find a way to recycle the same query multiple times as efficient as possible. With the purpose of efficiently comparing the results of 2 queries where the resultset from both queries won't match in the size (in this sample test it coincidently does, but I've tried with a variant where it doesn't, and it works just as beautiful). Although it's nice that it actually works like intended, I'm a little baffled to exactly WHY it works (this being why the 2 dimensional array I build actually works like intended). I tried looking through the manual to find how PHP build the associate arrays when no key is specified, but came out empty. So if anyone can explain how this come to function like intended despite immediate logic dictating it shouldn't, I'd really appreciate it ... I hate when I get the code to work, but don't get why it works...

This is the code (pieced together from much larger script ... that does lots more than this in between:

<?php
// global data pull of base configuration
$config_query = "SELECT setting,value FROM hf_config";
$config = mysql_query($config_query) or die('Unable to get base configuration<br>'.mysql_error());


while ($config_data = mysql_fetch_array($config)) {
  if ($config_data['setting'] == 'admin_level') {
    $lvl_admin = $config_data['value'];
  } elseif ($config_data['setting'] == 'new_member_level') {
    $lvl_new = $config_data['value'];
  } elseif ($config_data['setting'] == 'guest_level') {
    $lvl_guest = $config_data['value'];
  }
}

// load levels and build array
$level_query = "SELECT levelID,levelname,description FROM hf_levels ORDER BY `levelorder` ASC";
$levels = mysql_query($level_query);
while ($leveldata = mysql_fetch_array($levels)) {
$arrlevels[$leveldata['levelID']] = array('levelname' => $leveldata['levelname'],
'description' => $leveldata['description']);
}
?>


<table>
<tr>
<td colspan="3" class="adm_title">General configuration</td>
</tr><tr>
<td class="adm_subtitle">Administrator level</td>
<td class="adm_regular"><?php echo($lvl_admin.' - '.$arrlevels[$lvl_admin]['levelname']); ?></td>
<td></td>
</tr><tr>
<td class="adm_subtitle">New member level</td>
<td class="adm_regular"><?php echo($lvl_new.' - '.$arrlevels[$lvl_new]['levelname']); ?></td>
</tr><tr>
<td class="adm_subtitle">Guest level</td>
<td class="adm_regular"><?php echo($lvl_guest.' - '.$arrlevels[$lvl_guest]['levelname']); ?></td>
</tr>
</table>


The query results look like this:

mysql> SELECT setting,value FROM hf_config;
+------------------+-------+
| setting          | value |
+------------------+-------+
| admin_level      | 1     |
| new_member_level | 50    |
| guest_level      | 99    |
+------------------+-------+
3 rows in set (0.00 sec)

mysql> SELECT levelID,levelname,description FROM hf_levels ORDER BY `levelorder`
ASC;
+---------+------------+-----------------------+
| levelID | levelname | description |
+---------+------------+-----------------------+
| 1 | Admin | System administrators |
| 50 | New member | new members |
| 99 | Guest | Guest users |
+---------+------------+-----------------------+
3 rows in set (0.00 sec)


Output of the script looks like this:

General configuration
Administrator level 1 - Admin
New member level 50 - New member
Guest level 99 - Guest

Unless I misunderstand how PHP make unspecified arrays (and I probably do since this works), when you have an array of 3 elements on the first dimenstion like I do, and then ask for $arrlevels[$lvl_guest]['levelname'], which in this case actually means it asks for $arrlevels[99]['levelname'] .... how come it pick the correct element, and not error out that element 99 don't exist ??

My only conclusion (based on the fact that this actually works) is that PHP makes the key the same as the value if the key isn't specified. But is this actually correct ???? Or is there something "going on" that I don't know about ???

I've got another sample, that uses the same "query recycling" method, but with much, much more complex database queries, and it works just as perfectly well....

I really just wanna understand why this actually work, and how ... it can be rather confusing to stumble across a useful functionality and solution when you're still learning how to do the more complex things in PHP.

-- Rene Brehmer aka Metalbunny

If your life was a dream, would you wake up from a nightmare, dripping of sweat, hoping it was over? Or would you wake up happy and pleased, ready to take on the day with a smile?

http://metalbunny.net/
References, tools, and other useful stuff...
Check out the new Metalbunny forums at http://forums.metalbunny.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to