Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Last time you told me exactly what you want there were no id elements, as in screenshot below Accordingly, I amended the code to remove them and also removed the empty children arrays (that you still keep moaning about). I posted this and asked you to verify if that was what you wanted. No reply to that request has been received. Now it appears that id elements are back in the requirements. I also notice that there is no more mention of a relationship element that was in your first post. As your requirements are changing from day to day, and I cannot get satisfactoty feedback, I am not even going to try to keep up. I guess you need to take it from here on your own.
  2. I agree a prepared query is preferred but they're a PITA with a large list of IN() values. I'm also not sure if you can do it with Sql Server without using stored procedures. Perhaps $jobs = array_filter($_POST['job'], fn($v) => ctype_alnum($v) && strlen($v) < 7 );
  3. As with all pairs of html tags (<th>..</th>, <td>..</td> etc) the closing tag contains the "/". Yet you start the page with a closing form tag. Your next line is the opening form tag but you have placed it as far as could from the rest of the form, which doen't make for great readability. You then finish the form with another opening form tag followed by the submit button. Also there is no closing </table> tag. You want something like <form action='laminator.php' method='post'> <table> <tr> .. headings .. </tr> while( ... ) { <tr> .. table content rows .. </tr> } </table> <input type='submit'> </form>
  4. Not too well. You have a bit of learning to do regarding html markup for forms.
  5. Your table needs to be inside <form> ... </form> tags and you need a submit button to send the form data. In your processing page ... if ($_SERVER['REQUEST_METHOD']=='POST') { $jobs = array_map('intval', $_POST['job'] ?? [0] ); // ensure they are all numbers $sql = "SELECT ... WHERE jmmjobid IN (" . join(',', $jobs) . ")"; }
  6. 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);
  7. 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
  8. Just as before echo $arr['OrderAddresses'][0]['ConsignmentNo']; //--> 547645363546 "- >" is for object properties not array keys
  9. The ones I'm aware of (for ajax anyway) are JSON TEXT HTML
  10. <?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'];
  11. I did. You could also try entering "curl" in the search box at the top of this page.
  12. https://www.php.net/curl
  13. 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.
  14. 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].
  15. 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.
  16. If you have old version of PHP you can replace that line with $children = isset($emps[$emp['id']]) ? $emps[$emp['id']] : [];
  17. Download the pdf and print from there.
  18. 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 ( ) ) ) ) ) )
  19. 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.
  20. 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.
  21. Why don't you json_encode the array and post it here so we can see what you are talking about.
  22. What is there not to understand? Your $data variable contains a string and not an array (as required)
  23. 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.
×
×
  • 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.