Jump to content


Photo

multi-dimensional array problem


  • Please log in to reply
8 replies to this topic

#1 Levinax

Levinax
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 28 March 2006 - 12:45 AM

ok, so im trying to input the result of a query into a multidimensional array. i know that the query returns the results i want, but im having problems inputting it into the array correctly.

theoretically, what should come out would be an array equal to the following:
$myarray = array ("location" => array ("CHEVROLET", "OLYMPIA", "TACOMA"),
                 "store" => array ("CHEVROLET", "NISSAN", "SUBARU", "MITSUBISHI", "CHRYSLER"),
                 "dept" => array ("BODY SHOP", "FINANCE", "OFFICE", "SALES", "SERVICE", "SHOP"));

currently, i am inputing the code into an array in the following fashion:
$qryres = pg_query($db, $lsd);
$myarray = array();
$lsdres = pg_fetch_array($qryres);

while ($lsdres = pg_fetch_array($qryres)) {

     $myarray[] = $lsdres;

}

this inputs all the information, but not the correct way. the result of doing it this way returns the follwing:
Array ( 
[0] => Array ( [0] => OLYMPIA [location] => OLYMPIA ) 
[1] => Array ( [0] => TACOMA [location] => TACOMA ) 
[2] => Array ( [0] => CHEVROLET [location] => CHEVROLET ) 

[3] => Array ( [0] => CHRYSLER [location] => CHRYSLER ) 
[4] => Array ( [0] => MITSUBISHI [location] => MITSUBISHI ) 
[5] => Array ( [0] => NISSAN [location] => NISSAN ) 
[6] => Array ( [0] => SUBARU [location] => SUBARU ) 

[7] => Array ( [0] => BODY SHOP [location] => BODY SHOP ) 
[8] => Array ( [0] => FINANCE [location] => FINANCE ) 
[9] => Array ( [0] => OFFICE [location] => OFFICE ) 
[10] => Array ( [0] => PARTS [location] => PARTS ) 
[11] => Array ( [0] => RECEPTION [location] => RECEPTION ) 
[12] => Array ( [0] => SALES [location] => SALES ) 
[13] => Array ( [0] => SERVICE [location] => SERVICE ) 
[14] => Array ( [0] => SHOP [location] => ))

if anyone could point me in the right direction, i would greatly appreciate it.


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 March 2006 - 11:11 AM

if you run this code, what do you get?

$qryres = pg_query($db, $lsd);
$myarray = array();
$lsdres = pg_fetch_array($qryres);

echo '<pre>', print_r($lsdres , true), '</pre>';

In other words, what does the array $lsdres look like?


BTW, that pg_fetch_array() before the while loop will prevent the first record from being included in $myarray.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Levinax

Levinax
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 28 March 2006 - 05:46 PM

when i use that code, it outputs the following...

Array
(
    [0] => CHEVROLET
    [location] => CHEVROLET
)



#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 March 2006 - 05:53 PM

Where do store and dept come from?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 Levinax

Levinax
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 28 March 2006 - 06:02 PM

hopefully from the database, they are table names.

the SQL looks like this:

$lsd = "SELECT DISTINCT location FROM info
UNION ALL
SELECT DISTINCT store FROM info
UNION ALL
SELECT DISTINCT dept FROM info";

this is the only way i could get the query to return the proper results... now getting it to return the proper results in the right format will be the tricky part...

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 28 March 2006 - 06:10 PM

Use mysql_fetch_assoc() instead of mysql_fetch_array(). That should help.

Ken

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 March 2006 - 06:16 PM

try

$lsd = "SELECT DISTINCT 'location' as keyval, location as val FROM info
UNION ALL
SELECT DISTINCT 'store' as keyval, store as val FROM info
UNION ALL
SELECT DISTINCT 'dept' as keyval, dept as val FROM info";

$qryres = pg_query($db, $lsd);
$myarray = array();

while ($lsdres = pg_fetch_array($qryres)) {

       $myarray[$lsdres['keyval'][] = $lsdres['val'];
}

echo '<pre>', print_r($myarray , true), '</pre>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 Levinax

Levinax
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 28 March 2006 - 06:28 PM

when i use that code, the query fails...

[Tue Mar 28 10:17:51 2006] [error] PHP Warning:  pg_query(): Query failed: ERROR:  Unable to identify an ordering operator '&lt;' for type 'unknown'
        Use an explicit ordering operator or modify the query
. in /home/test/public_html/test/test4.php on line 258
[Tue Mar 28 10:17:51 2006] [error] PHP Warning:  pg_fetch_array(): supplied argument is not a valid PostgreSQL result resource in /home/test/public_html/test/test4.php on line 261
[Tue Mar 28 10:17:51 2006] [error] PHP Warning:  pg_num_rows(): supplied argument is not a valid PostgreSQL result resource in /home/test/public_html/test/test4.php on line 273


#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 March 2006 - 06:38 PM

Is there some esoteric syntax in postgresql for including a string literal in a query?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users