mysql - multi-array result in PHP object / Sql query -
i have object called index
has property called _fletter
(in case private property).
i have sql query fetches distinct values table using following query:
select distinct fletter books order fletter asc
the way call query through private function database related stuff , returns variable:
function getfirstletter() { // database stuff omitted $stmt->execute(); $res = $stmt->fetchall(pdo::fetch_assoc); return $res; }
the way give result _fletter
property follows:
$this->_fletter = $this->getfirstletter();
what ends happening property of $this->_fletter
associative array when print_r
gives this:
[0] => array ( [fletter] => ) // etc.
i want use foreach
statement such iterates through every _fletter[x]
x corresponds letter in question , run following sql statement
select booktitle, isbn, pages books booktitle = ':booktitle'
i want return result in previous example:
$this->_fletter = $this->getbooktitles();
and, idea want results of new associate array added _fletter
array first letter corresponds new result set, along lines of:
[0] => array ( [fletter] => [a] ( [1] => 'a time kill' [2] => 'almost famous' ) ) //etc - note may not have written syntax correct
so, new query has take value of '_fletter', run query , return result in same place or under same array entry of first letter. want fletter new key or whatnot of new result array
if add new result _fletter
array append existing array , not use value of [fletter]
rather add new entries array:
[15] => array ( [fletter] => w ) [a] => array ( [0] => array ( [booktitle] => time kill [isbn] => 1234567890 )
any ideas how can achieved? want avoid placing php in html portion of page, however, if there no alternative, might have that.
it's hard understand want. think need 1 query 1 foreach
select fletter, booktitle, isbn, pages books <?php $fletters = array(); while ($row = $stmt->fetch(pdo::fetch_assoc)) { $index = $row['fletter']; unset($row['fletter']); $fletters[$index][] = $row; }
Comments
Post a Comment