Page 1 of 1

traversing array, each troubles

Posted: Tue Aug 06, 2002 2:24 am
by 9902468
Hello! I'm having trouble going through the array that contains one row of the sql query that was applied. Each doubles all my results, except the first one ie. If I get from db columns name, address this code outputs name, address, address. (As many rows as query returned.) this is part of the listing function that is used in the system. The full sql query I used for testing is:

select rakennukset.nimi, tilat.nimi from tilat, rakennukset where rakennukset.rakennus_id = tilat.rakennus_id order by rakennukset.nimi, tilat.nimi asc

I tried to use
if ( !is_int ( $key ) ) before adding td, but then I'm not getting the first (name) listed attall

Any thoughts? code is below, if u want to take a look:

<update>
The column that gives me headache seems to be the only column that each is not doubling, how this can be? I know that each outputs 1 dimension 4 cell array that contains associative and numeric keys to the key and value. But are there any conditions that each returns associative or numeric keys only?
</update>



Code: Select all

require_once 'all_db_login.inc';

                $list_result = pg_query($all_link, $sql);

                $return = "<table class="listaus">";

                /* Go through all rows, construct td by td....*/
                $row = 0;
                while ($data = @pg_fetch_object ($list_result, $row)) &#123;

                        $return .= "<tr>";


                                while(list($key, $val) = each($data))&#123;

                                        $return .= "<td class ="listaus">".$val."</td>";

                                &#125;



                        $return .="</tr>";

                $row++;
                &#125;

                $return .= "</table>";
                return $return;
&#125;

Posted: Tue Aug 06, 2002 2:38 am
by twigletmac
Hi does this work any better?

Code: Select all

<?php
require_once 'all_db_login.inc'; 

$list_result = pg_query($all_link, $sql); 

$return = '<table class="listaus">'; 

/* Go through all rows, construct td by td....*/ 
while ($data = @pg_fetch_array($list_result, 0, PGSQL_NUM)) &#123; 
	$return .= "<tr>"; 
	foreach ($data as $val) &#123;
		$return .= '<td class ="listaus">'.$val.'</td>'; 
	&#125;
	$return .= '</tr>'; 
&#125;
$return .= '</table>'; 
return $return; 
&#125; 
?>
The main change is to use pg_fetch_array() instead of pg_fetch_object().

If you put:

Code: Select all

echo '<pre>';
print_r($data);
echo '</pre>';
into the while loop that will show you exactly what the row of data contains.

Mac

Posted: Tue Aug 06, 2002 2:53 am
by 9902468
Thanks twigletmac, (again) works beautifully now, didn't know that you can turn associative keys off... one question though; how can you possibly have the time to answer to all these questions? I'm happy that someone does, and usually you know the right answers but, ummm, just wondering.. nevermind.

Thanks

-9902468

Posted: Tue Aug 06, 2002 2:57 am
by twigletmac
Like doing it. Can do it. Spend my whole day programming PHP so this just forms part of it. I also learn tons of new stuff by helping other people so my own code benefits too.

Mac

Posted: Tue Aug 06, 2002 11:05 am
by EricS
Actually mac is kept locked up in dark room in the basement and this is the only way we can let him socialize.

Posted: Tue Aug 06, 2002 12:20 pm
by twigletmac
her not him :roll: and can I have some water please...

Mac

Posted: Tue Aug 06, 2002 1:18 pm
by volka
what have you done with your last tumbler?
hmmm well - answer a dozen more questions and we will see :twisted:

:D

Posted: Wed Aug 07, 2002 3:39 am
by 9902468
twigletmac wrote:Like doing it. Can do it.

Mac
Wow. I allways get :oops: when I see women say that ;)