Jump to content

**SOLVED** array trouble


jmag

Recommended Posts

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:
[code]    $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[/code]

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...:
[code]    $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[/code]

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 ;>
Link to comment
Share on other sites

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

[code]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
        )

)[/code]
Link to comment
Share on other sites

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

[code]while($row = mysql_fetch_assoc($q_page)) {

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

        } //end while[/code]

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

[code]$id = 2;

$page_name = $array_page[$i];[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.