jmag Posted March 12, 2006 Share Posted March 12, 2006 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 ;> Quote Link to comment https://forums.phpfreaks.com/topic/4741-solved-array-trouble/ Share on other sites More sharing options...
Barand Posted March 12, 2006 Share Posted March 12, 2006 try[code]while($row = mysql_fetch_assoc($q_page)) { $array_page[] = $row; } //end while[/code]Check it with[code]echo '<pre>', print_r($array_page, true), '</pre>';[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4741-solved-array-trouble/#findComment-16660 Share on other sites More sharing options...
jmag Posted March 12, 2006 Author Share Posted March 12, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/4741-solved-array-trouble/#findComment-16664 Share on other sites More sharing options...
Barand Posted March 12, 2006 Share Posted March 12, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/4741-solved-array-trouble/#findComment-16678 Share on other sites More sharing options...
jmag Posted March 12, 2006 Author Share Posted March 12, 2006 excellent! works like a charm - thank you! Quote Link to comment https://forums.phpfreaks.com/topic/4741-solved-array-trouble/#findComment-16727 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.