traversing array, each troubles

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
9902468
Forum Commoner
Posts: 89
Joined: Thu Jun 06, 2002 6:39 am
Location: Europe

traversing array, each troubles

Post 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;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
9902468
Forum Commoner
Posts: 89
Joined: Thu Jun 06, 2002 6:39 am
Location: Europe

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

her not him :roll: and can I have some water please...

Mac
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

what have you done with your last tumbler?
hmmm well - answer a dozen more questions and we will see :twisted:

:D
User avatar
9902468
Forum Commoner
Posts: 89
Joined: Thu Jun 06, 2002 6:39 am
Location: Europe

Post by 9902468 »

twigletmac wrote:Like doing it. Can do it.

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