Adamhumbug Posted August 18, 2023 Share Posted August 18, 2023 (edited) I have a function that lists out all items that should appear on a quote - i know the sql is correct as when i was using a whille($stmt -> fetch()...... everything worked fine. I am now building an array so that i can display it by a certain value but i am only getting 2 results output and there should be 16. $row = $stmt -> fetchAll(); foreach($row as $item){ if(isset($data[$item['sectionName']])){ $data[$item['sectionName']] = []; } $data[$item['sectionName']][]= ["itemName" =>$item['name'], "quantity" => $item['quantity']]; } foreach($data as $section => $items){ $out.= "<br/>".$section."<br/>"; foreach($items as $i){ $out.= "-".$i['itemName']; } } Below are the rows i am expecting to see. This is what is being output using $out = "<pre>".print_r($data,1)."</pre>"; Edited August 18, 2023 by Adamhumbug Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted August 18, 2023 Solution Share Posted August 18, 2023 11 minutes ago, Adamhumbug said: if(isset($data[$item['sectionName']])){ Shouldn't that be if(!isset($data[$item['sectionName']])){ ^ What is the relevance of the data you posted to the code? Quote Link to comment Share on other sites More sharing options...
Adamhumbug Posted August 18, 2023 Author Share Posted August 18, 2023 7 minutes ago, Barand said: Shouldn't that be if(!isset($data[$item['sectionName']])){ ^ What is the relevance of the data you posted to the code? Yes, yes it should!! Thank You Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 18, 2023 Share Posted August 18, 2023 1 hour ago, Adamhumbug said: foreach($row as $item){ if(isset($data[$item['sectionName']])){ $data[$item['sectionName']] = []; } $data[$item['sectionName']][]= ["itemName" =>$item['name'], "quantity" => $item['quantity']]; } this is just indexing/pivoting the data by a column. PDO has a fetch mode that will 'automatically' do that for you, assuming that the sectionName is the first column in the SELECT ... list - $data = $stmt -> fetchAll(PDO::FETCH_GROUP); if for some reason you require the name values to be referenced as itemName in the fetched data, assign it that name as an alias in the sql query. Quote Link to comment Share on other sites More sharing options...
Adamhumbug Posted August 21, 2023 Author Share Posted August 21, 2023 Hi All, I took on board your feedback here and basically rewrote the whole statement. I would share my resultant code but the whole query is now very different. I appreiciate your pointers and assistance as always. Quote Link to comment 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.