firelior Posted September 21, 2006 Share Posted September 21, 2006 Hi,I got this code:[code]$result = mysql_query("select title from content");$row = mysql_fetch_object($result);while($row = mysql_fetch_object($result)){ foreach ($row as $key => $val){ $elements=array($key => $row->$key); }} [/code]And all of the time it returns only the last one.for example if I got this table:[table][tr][td]title[/td][/tr][tr][td]home[/td][/tr][tr][td]news[/td][/tr][tr][td]about us[/td][/tr][/table]it will return only "about us"I want it to return all of them like this:[code]array(array('name' => 'home),array('name' => 'news'),array('name' => 'about us'))[/code] Quote Link to comment https://forums.phpfreaks.com/topic/21566-array-problem/ Share on other sites More sharing options...
Daniel0 Posted September 21, 2006 Share Posted September 21, 2006 Change [code]$elements=array($key => $row->$key);[/code] to [code]$elements[]=array($key => $row->$key);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/21566-array-problem/#findComment-96252 Share on other sites More sharing options...
Barand Posted September 21, 2006 Share Posted September 21, 2006 Instead of the complication of an array of single-element arrays, would it not be simpler, as you only select a single column to create an array like array ('home', 'news', 'about us')with[code]<?php$result = mysql_query("select title from content");$row = mysql_fetch_object($result); // EDIT remove this linewhile($row = mysql_fetch_object($result)) { $elements[] = $row->title; }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/21566-array-problem/#findComment-96257 Share on other sites More sharing options...
Daniel0 Posted September 21, 2006 Share Posted September 21, 2006 One thing I do not understand why you are doing is this: [code]$row = mysql_fetch_object($result);while($row = mysql_fetch_object($result)) {[/code]What is the point in that? Quote Link to comment https://forums.phpfreaks.com/topic/21566-array-problem/#findComment-96266 Share on other sites More sharing options...
Barand Posted September 21, 2006 Share Posted September 21, 2006 Good eyes, missed that. That will just throw the first record away so it doesn't get processed. Editted my post. Quote Link to comment https://forums.phpfreaks.com/topic/21566-array-problem/#findComment-96276 Share on other sites More sharing options...
firelior Posted September 21, 2006 Author Share Posted September 21, 2006 thanks for your fast answer,i did a multi dimentional array because i want more things other then titlefor example[code]$result = mysql_query("select id,title from content");while($row = mysql_fetch_object($result)){ foreach ($row as $key => $val){ $elements[]=array($key => $row->$key); }} [/code]now there is a problem.I want it to write something like this:[code]elements=array(array('title' => 'home','id' => 1),array('title' => 'news','id' => 2));[/code]but it doesn't..any help?thanks! Quote Link to comment https://forums.phpfreaks.com/topic/21566-array-problem/#findComment-96277 Share on other sites More sharing options...
Daniel0 Posted September 21, 2006 Share Posted September 21, 2006 Try: [code]<?php$result = mysql_query("select title from content");while($row = mysql_fetch_object($result)){ $elements[] = array('title'=>$row->title,'id'=>$row->id);}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/21566-array-problem/#findComment-96280 Share on other sites More sharing options...
Barand Posted September 21, 2006 Share Posted September 21, 2006 or, simpler[code]<?php$result = mysql_query("select id, title from content");while($row = mysql_fetch_assoc($result)){ $elements[] = $row;}//check resultsecho '<pre>', print_r($elements, true), '</pre>';?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/21566-array-problem/#findComment-96284 Share on other sites More sharing options...
firelior Posted September 21, 2006 Author Share Posted September 21, 2006 wow!thanks! :Dgreat forum.I am staying here 100%.Thanks Barand,Daniel0 Quote Link to comment https://forums.phpfreaks.com/topic/21566-array-problem/#findComment-96289 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.