Hi Marek,

I came up with this hack, which seems to work:

$query = "SELECT name, value FROM config WHERE site = '$site' ORDER BY name,
seq";
$result = mysql_query($query)
   or die("Query failed : " . mysql_error());

$conf = array ();
$last = "";

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

   if ($line['name'] == $last) {

      array_push($conf[$last], $line['value']);

   } else {

      $last = $line['name'];
      $conf[$last] = array ($line['value']);


   }

}

mysql_free_result($result);


Your's seems more elegant, so I'll give it a try.

Thanks,
Cameron


-----Original Message-----
From: Marek Kilimajer [mailto:[EMAIL PROTECTED]
Sent: Friday, January 09, 2004 11:06 AM
To: Cameron B. Prince
Cc: [EMAIL PROTECTED]
Subject: Re: [PHP] Dynamically Created Arrays


This should be what you need:

if(!is_array($conf[$name])) $conf[$name] = array($val);
else $conf[$name][] = $val;

Cameron B. Prince wrote:

> Hey guys,
>
> I'm making progress on my perl to PHP conversion project but I need some
> help...
>
> I have a configuration table in MySQL made up like this:
>
>
> CREATE TABLE config (
>   code mediumint(9) NOT NULL auto_increment,
>   site varchar(32) NOT NULL default '',
>   name varchar(64) NOT NULL default '',
>   value varchar(128) NOT NULL default '',
>   seq smallint(4) default NULL,
>   PRIMARY KEY  (code)
> ) TYPE=MyISAM;
>
>
> It has data like this:
>
> | 1  | site1 | homedir   | /var/www/htdocs/site1 | NULL |
> | 2  | site1 | groupname | Group1                | 1    |
> | 3  | site1 | groupname | Group2                | 2    |
>
>
> I use this query to get the configuration data:
>
> SELECT name, value
> FROM config
> WHERE site = '$site'
> ORDER BY name, seq
>
>
> What I want to create is an array of arrays like this:
>
> $conf[$name][] = $val;
>
> or
>
> array_push($conf[$name], $val);
>
>
> So I end up with something like:
>
> $conf[homedir][0] = /var/www/htdocs/site1
> $conf[groupname][0] = Group1
> $conf[groupname][1] = Group2
>
>
>
> If I were still in Perl-land, I'd use a hash of arrays and push things
into
> it, but I don't seem to have that option now and I keep getting "Use of
> undefined constant name" errors.
>
> Am I going about this in the right way? Any help would be greatly
> appreciated.
>
> Thanks,
> Cameron
>



!DSPAM:3ffede81271881305314632!

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

Reply via email to