Jump to content


Photo

**SOLVED** array trouble


  • Please log in to reply
4 replies to this topic

#1 jmag

jmag
  • Members
  • PipPip
  • Member
  • 22 posts
  • LocationSweden

Posted 12 March 2006 - 11:06 AM

Hello,

I have a result I want to use on several places, not just one, so I figured I read it all into an array. For some reason though it won't take my keys I want to use, so I get the error undefined index bla bla..

this is what it looks like now:
    $q_page = mysql_query("SELECT * FROM pages");
    
    if(!$q_page)
            $message = "<p><span class=\"error\"><b>An error has occured, couldn't fetch pagenames:</b></span> " . mysql_error() . "</p>";
    else {
        while($row = mysql_fetch_array($q_page)) {
            $array_page[] = array(
                    "page_id" => $row["page_id"],
                    "page_name" => $row["page_name"]
                );
        } //end while
    } //end else

I should mention though that I first tried it another way which I would prefer, but I don't really know how to extend the array dynamically for the number of keys that my exist. In this case I knew there would only be two dimensions in the array, but I still don't think I got it right...:
    $q_page = mysql_query("SELECT * FROM pages");
    
    if(!$q_page)
            $message = "<p><span class=\"error\"><b>An error has occured, couldn't fetch pagenames:</b></span> " . mysql_error() . "</p>";
    else {
        while($row = mysql_fetch_array($q_page)) {
            foreach($row as $key => $val) {
                echo $key . " => " . $val . "<br />";
                
                $array_page[] = array(
                    $key => $val,
                    $key => $val
                );
            } //end foreach
        } //end while
    } //end else

any help is appreciated! And I'd be glad if I just got the top version to work, the other one would be a bonus ;>

#2 Barand

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

Posted 12 March 2006 - 01:20 PM

try

while($row = mysql_fetch_assoc($q_page)) {

                $array_page[] = $row;               

        } //end while

Check it with

echo '<pre>', print_r($array_page, 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

#3 jmag

jmag
  • Members
  • PipPip
  • Member
  • 22 posts
  • LocationSweden

Posted 12 March 2006 - 01:45 PM

looks like it creates an array in the array, this is what it looked like earlier too.

Array
(
    [0] => Array
        (
            [page_id] => 1
            [page_name] => Startsidan
        )

    [1] => Array
        (
            [page_id] => 2
            [page_name] => Guld
        )

    [2] => Array
        (
            [page_id] => 3
            [page_name] => Ur
        )

    [3] => Array
        (
            [page_id] => 4
            [page_name] => Bröllop
        )

    [4] => Array
        (
            [page_id] => 5
            [page_name] => Om oss
        )

    [5] => Array
        (
            [page_id] => 6
            [page_name] => Kontakt
        )

)


#4 Barand

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

Posted 12 March 2006 - 03:22 PM

Now I see what your data looks like you might want to try this

while($row = mysql_fetch_assoc($q_page)) {

                $array_page[$row['page_id'] = $row['page_name'];               

        } //end while

Then if you have the id value, you can retrieve the name. EG

$id = 2;

$page_name = $array_page[$i];

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 jmag

jmag
  • Members
  • PipPip
  • Member
  • 22 posts
  • LocationSweden

Posted 12 March 2006 - 06:19 PM

excellent! works like a charm - thank you!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users