Jump to content

Barand

Moderators
  • Posts

    24,606
  • Joined

  • Last visited

  • Days Won

    831

Everything posted by Barand

  1. I ran the code you posted and my json output is ... {"id":5,"name":"bashful","title":"General Manager","children":[{"id":1,"name":"happy","title":"Sales Manager","children":[{"id":2,"name":"comet","title":"Sales Rep","children":[{"id":12,"name":"cupid","title":"Sales Assistant","children":[]}]},{"id":11,"name":"vixen","title":"Sales Rep","children":[]}]},{"id":3,"name":"grumpy","title":"Accountant","children":[{"id":6,"name":"dancer","title":"Accounts clerk","children":[{"id":4,"name":"prancer","title":"Secretary","children":[{"id":13,"name":"donner","title":"Secretarial Assistant","children":[]}]}]}]},{"id":7,"name":"doc","title":"Operations Manager","children":[{"id":9,"name":"dasher","title":"Technician","children":[]}]},{"id":10,"name":"rudolph","title":"Personal Assistant","children":[{"id":8,"name":"blitzen","title":"HR Clerk","children":[]}]}]} As you see, none of those extra items, but it looks like your problem is your default PD fetch mode. When you connect, set to PDO::FETCH_ASSOC. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); It looks like yours is set to PDO::FETCH_BOTH which returns an array containing numeric keys and associative (column name) keys, therefore everything is duplicated. If you don't want to change your default (although I can see no reason why anyone would want FETCH_BOTH) you can change $emps = $res->fetchAll(PDO::FETCH_GROUP); // to $emps = $res->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
  2. Is the sample output I posted (link below) in the correct format? https://forums.phpfreaks.com/topic/315202-push-key-value-in-multidimensionnal-array-in-loop-php/?do=findComment&comment=1599626
  3. Just as before echo $arr['OrderAddresses'][0]['ConsignmentNo']; //--> 547645363546 "- >" is for object properties not array keys
  4. The ones I'm aware of (for ajax anyway) are JSON TEXT HTML
  5. <?php $jsn = '[{"OrgID":"1234567890987654321","OrderNumber":"M123456","OrigOrderNumber":"","Placed":"2022-08-17T14:04:47.653","PostCode":"AB1 2CD","BackOfficeOrderNumber":"12345678","CustomerName":"My Name","OutletName":"My Name Two","OrderStatus":"Delivered","OrderRef":"O-0012345678","AddressCount":1}]'; //convert to an array $arr = json_decode($jsn, 1); // output the array to view structure echo '<pre>' . print_r($arr, 1) . '</pre>'; ?> The resulting array is and you then access you required value by following the array keys (indicated), so you want $order_number = $arr[0]['OrderNumber'];
  6. I did. You could also try entering "curl" in the search box at the top of this page.
  7. https://www.php.net/curl
  8. The only difference I can see is that your employee elements don't have ids. Leave out the id and find another way of matching employee to parent. When I do that my json output, with very little change to the code, is { "name":"bashful","title":"General Manager", "children":[ {"name":"happy","title":"Sales Manager", "children":[ {"name":"comet","title":"Sales Rep", "children":[ {"name":"cupid","title":"Sales Assistant"} ] }, {"name":"vixen","title":"Sales Rep"} ] }, {"name":"grumpy","title":"Accountant", "children":[ {"name":"dancer","title":"Accounts clerk", "children":[ {"name":"prancer","title":"Secretary", "children":[ {"name":"donner","title":"Secretarial Assistant"} ] } ] } ] }, {"name":"doc","title":"Operations Manager", "children":[ {"name":"dasher","title":"Technician"} ] }, {"name":"rudolph","title":"Personal Assistant", "children":[ {"name":"blitzen","title":"HR Clerk"} ] } ] } [PS] If that output is as required, let me know and I'll post the code used to produce it.
  9. Change your $res = $pdo->query("SELECT id, name, title, parent FROM main1"); to $res = $pdo->query("SELECT parent, id, name, title FROM main1"); When using FETCH_GROUP the column you want to group by has to be selected first. You are grouping by id so there is no $emps[0].
  10. I don't get that error so either your code is different or your data is different from mine (or both). Can you post the code you are using and a dump of your test data if different.
  11. If you have old version of PHP you can replace that line with $children = isset($emps[$emp['id']]) ? $emps[$emp['id']] : [];
  12. Download the pdf and print from there.
  13. This calls out for a recursive solution $res = $pdo->query("SELECT parent , id , name , title FROM main "); $emps = $res->fetchAll(PDO::FETCH_GROUP); // results array grouped by parent getChildren($emps[0], $emps); $chart = $emps[0][0]; // view result echo '<pre>' . print_r($chart, 1) . '</pre>'; /** * recursive function to build hierarchical array * * @param array $kids array of current children * @param array $emps orig array of all employees */ function getChildren(&$kids, &$emps) { foreach ($kids as $k => &$emp) { $children = $emps[$emp['id']] ?? []; getChildren($children, $emps); $emp['children'] = $children; } } ?> <!-- DATA ---------------------------------------------------------------------- CREATE TABLE `main` ( `id` int(11) NOT NULL, `name` varchar(45) DEFAULT NULL, `title` varchar(45) DEFAULT NULL, `parent` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; '1', 'happy', 'Sales Manager', '5' '2', 'comet', 'Sales Rep', '1' '3', 'grumpy', 'Accountant', '5' '4', 'prancer', 'Secretary', '6' '5', 'bashful', 'General Manager', '0' '6', 'dancer', 'Accounts clerk', '3' '7', 'doc', 'Operations Manager', '5' '8', 'blitzen', 'HR Clerk', '10' '9', 'dasher', 'Technician', '7' '10', 'rudolph', 'Personal Assistant', '5' '11', 'vixen', 'Sales Rep', '1' '12', 'cupid', 'Sales Assistant', '2' '13', 'donner', 'Secretarial Assistant', '4' -------------------------------------------------------------------------------> Results Array ( [id] => 5 [name] => bashful [title] => General Manager [children] => Array ( [0] => Array ( [id] => 1 [name] => happy [title] => Sales Manager [children] => Array ( [0] => Array ( [id] => 2 [name] => comet [title] => Sales Rep [children] => Array ( [0] => Array ( [id] => 12 [name] => cupid [title] => Sales Assistant [children] => Array ( ) ) ) ) [1] => Array ( [id] => 11 [name] => vixen [title] => Sales Rep [children] => Array ( ) ) ) ) [1] => Array ( [id] => 3 [name] => grumpy [title] => Accountant [children] => Array ( [0] => Array ( [id] => 6 [name] => dancer [title] => Accounts clerk [children] => Array ( [0] => Array ( [id] => 4 [name] => prancer [title] => Secretary [children] => Array ( [0] => Array ( [id] => 13 [name] => donner [title] => Secretarial Assistant [children] => Array ( ) ) ) ) ) ) ) ) [2] => Array ( [id] => 7 [name] => doc [title] => Operations Manager [children] => Array ( [0] => Array ( [id] => 9 [name] => dasher [title] => Technician [children] => Array ( ) ) ) ) [3] => Array ( [id] => 10 [name] => rudolph [title] => Personal Assistant [children] => Array ( [0] => Array ( [id] => 8 [name] => blitzen [title] => HR Clerk [children] => Array ( ) ) ) ) ) )
  14. If I want to create something that needs to printed (report, invoice etc) I create it with PHP as a PDF file, download, then print.
  15. Barand

    MR

    i don't think SQL will like those /* ... */ embedded in the query. Your dependent subquery is going to slow down the processing. Replace it with a LEFT JOIN.
  16. Why don't you json_encode the array and post it here so we can see what you are talking about.
  17. What is there not to understand? Your $data variable contains a string and not an array (as required)
  18. That probably requires some explanation. If you have foreach ($test as $k => $rec) { then a copy of the array is passed in $rec. Any changes to $rec are made only to that copy. However, with foreach ($test as $k => &$rec) { the address of the original array is passed (ie by reference) so any changes are made to the original array. My main sources of learning have been the manual at php.net, tips picked up in these forums and via Google and lots of practice/experimentation. I often see phpdelusions.net cited as a good source.
  19. There is no code there to process the id that was selected, so all you are doing is repeatedly loading the same page and expecting different results by magic. You need to get id value from $_GET['id'] and use that to retrieve the data you want to display then display it.
  20. When you click the "View" button you go to "newdisplay.php". What is the value of the id in address bar at the top? Does it match the one you clicked? What are doing with that id value that is passed to the page? (The code?) [edit...] After a closer look at you link I see it contains a submit input. WHY? Is that taking precedence and just reloading the page? What happens if the link is <a href='newdisplay.php?id=$result[id]'>View<a/>
  21. Why are you suppressing error reporting then asking us what's wrong? The reporting is there to help you find errors. Use the code button when posting code.
  22. If I reverse the array (to give the same order that your sort is giving - DIFFERENT DEPO before TEST DEPO) I get Array ( [0] => Array ( [supplier] => TEST2 DEPO [rolanID] => Array ( [0] => 456 [1] => 188 [2] => 200 [3] => 123 ) [itemCount] => 4 ) [1] => Array ( [supplier] => DIFFERENT DEPO [rolanID] => Array ( [0] => 897 [1] => 487 [2] => 100 ) [itemCount] => 3 ) [2] => Array ( [supplier] => TEST DEPO [rolanID] => Array ( [1] => 234 ) [itemCount] => 1 ) )
×
×
  • 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.