Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Did you use gw1500se's link to foreach() in the manual? There is no sign of in the code, so what did you try?
  2. May work, may not. From the manual...
  3. In this case, as it seems you want to replace the array with the value of the first element in the array, then $u = current($u); which would have been easier prior to v8 too.
  4. What about the Dewey Decimal System used by libraries?
  5. You tell us. You used it in one of the first sets of code that you posted. You used $pdo and I told you the error was because it wasn't defined in the function and, therefore, you needed to pass it to the function. The problems you are currently having is nothing to do with PHP version or database access. It is a lack of basic knowledge by you on how to use use php functions and variables (variable scope)
  6. I told you what you need to do
  7. Except fot the variable name ($pdo -> $conn) this is exactly the same problem that I answered earlier. If you are going to keep repeating the same question then I am going to close this thread.
  8. You've been told how to fix that. If you ignore the information given to you then no there is no point in your posting or our answering
  9. See https://www.php.net/supported-versions.php
  10. The variable $pdo has not been defined inside the functioon. You need to pass it to the function when you call it. function Contracts($pdo) { // connectDB(); $qList = $pdo->query("SELECT * FROM EC_event WHERE Date = '2020-10-20 17:55:00'"); return $qList; // Calling routine uses while ($row = $qList->fetch()) { // echo $row['name']."<br />\n"; } } $Result = Contracts($pdo); Don't connect every time you perform a query. Connect once at the top of the script, storing the connection in $pdo.
  11. I was hoping for code and examples of your input arrays.
  12. try https://phpdelusions.net/pdo_examples
  13. What have you tried so far?
  14. The biggest change will be to your database access. If you use mysql_* functions you will find they no longer exist in version 7. You will need to rewrite all your db code to use either mysqli, or PDO As you re going to have to change and relearn I would recommend you take the PDO path. The name "mysqli" may look like "mysql" but they are different animals and there is more to recoding than just adding "i".
  15. I went with the original data that you posted as you had provided the expected results for that dataset which made checking easier. I also used approver ID instead of name and removed the redundant approver2 column. So my data is TABLE: user +----+--------------+-------------------+--------+------------+ | id | user_type | user_name | branch | supervisor | +----+--------------+-------------------+--------+------------+ | 1 | Manager | Manager | 123 | NULL | | 2 | ASM | Assistant Manager | 123 | 1 | | 3 | Team Leader | Team Leader 1 | 123 | 2 | | 4 | Team Leader | Team Leader 2 | 123 | 2 | | 5 | Group Leader | Group Leader 1 | 123 | 3 | | 6 | Group Leader | Group Leader 2 | 123 | 3 | | 7 | Group Leader | Group Leader 3 | 123 | 4 | | 8 | Group Leader | Group Leader 4 | 123 | 4 | | 9 | Engineer | Engineer 1 | 123 | 5 | | 10 | Engineer | Engineer 2 | 123 | 5 | | 11 | Engineer | Engineer 3 | 123 | 5 | | 12 | Engineer | Engineer 4 | 123 | 5 | | 13 | Engineer | Engineer 5 | 123 | 6 | | 14 | Engineer | Engineer 6 | 123 | 6 | | 15 | Engineer | Engineer 7 | 123 | 6 | | 16 | Engineer | Engineer 8 | 123 | 6 | | 17 | Engineer | Engineer 9 | 123 | 7 | | 18 | Engineer | Engineer 10 | 123 | 7 | | 19 | Engineer | Engineer 11 | 123 | 7 | | 20 | Engineer | Engineer 12 | 123 | 7 | | 21 | Engineer | Engineer 13 | 123 | 8 | | 22 | Engineer | Engineer 14 | 123 | 8 | | 23 | Engineer | Engineer 15 | 123 | 8 | | 24 | Engineer | Engineer 16 | 123 | 8 | +----+--------------+-------------------+--------+------------+ Below is a small script whch allows you to select an id and view the results <?php include 'db_inc.php'; // USE YOUR OWN $pdo = pdoConnect('db2'); // DB CONNECTION CODE $login_id = $_GET['id'] ?? 1; // default to manager's id $res = $pdo->prepare("SELECT u1.user_type , u1.user_name , u1.branch , u2.user_name as approver1 , u3.user_name as approver2 FROM user u1 LEFT JOIN user u2 ON u1.supervisor = u2.id LEFT JOIN user u3 ON u2.supervisor = u3.id WHERE CASE WHEN 1 < ? THEN ? IN (u1.id, u1.supervisor, u2.supervisor, u3.supervisor) -- show you and your minions ELSE u1.id IS NOT NULL -- show all records if manager is logged in END ORDER BY FIELD(u1.User_Type, 'Manager', 'ASM', 'Team Leader', 'Group Leader', 'Engineer'), u1.id "); $res->execute([ $login_id, $login_id ]); $tdata = ''; foreach ($res as $r) { $tdata .= "<tr><td>" . join('</td><td>', $r) . "</td></tr>\n"; } function idOptions($pdo, $current) { $opts = ''; $res = $pdo->query("SELECT id , user_name , user_type FROM user ORDER BY id "); foreach ($res as $r) { $sel = ($r['id']==$current) ? 'selected' : ''; $opts .= "<option $sel value='{$r['id']}'>{$r['user_name']} ({$r['user_type']})</option>\n"; } return $opts; } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Example</title> <meta charset="utf-8"> <style type='text/css'> body { font-family: verdana, arial, sans-serif; } table { border-collapse: collapse; width: 80%; margin: 50px auto; } th { background-color: black; color: white; padding: 8px 4px; } td { padding: 4px; } .header { padding: 16px; } </style> </head> <body> <form class='header'> <h1>Sample</h1> Logged in user: <select name='id'> <?= idOptions($pdo, $login_id) ?> </select> <input type='submit'> </form> <hr> <table border='1'> <tr> <th>User Type</th> <th>User Name</th> <th>Branch</th> <th>Approver 1</th> <th>Approver 2</th> </tr> <?= $tdata ?> </table> </body> </html>
  16. There are some discrepancies in your data Karthik M appears to have 4 different employee numbers Naveen Kumar P appears to have 2 different employee numbers but #321 and #1088 actually have different names... so by doubling-up and storing id(emp no) and name of the approver you now have 2 versions of the truth.
  17. Just to clarify... A connection is to a server, not a table. The database name, when connecting, is just the default name to use if no db name is specified in the query. In this case, both connections are to "localhost" so a single connection will suffice. If your tables were on different servers then you would need 2 connections and you would have to query the tables in separate queries,
  18. Hmm. I see there is some resemblance to the structure that you posted.
  19. My first observation is that the column "Approve2" in the table is redundant. If you know their supervisor (Approve1) then you know that Approve2 is that supervisor's supervisor. This can be found in the query with a join. My second is that it would be more efficient to store Approve1 as the id of the supervisor and not the name. I would have written the query for you but pictures of data are notoriously difficult to load into a table for testing purposes.
  20. I would also add this line in addition to the 2 above display_startup_errors = On
  21. What error are you gettiing?
  22. Having read you next post where you say that lids can have multiple emails or phonenumbers, how is that going to work if ... Primary keys have to be unique which will allow only one of each per lidnummer. You need to rethink your data model.
  23. If you are happy to display the multiple numbers and emails as, say, comma-separated lists, then you can use GROUP_CONCAT on those columns and GROUP BY Lidnummer. SELECT lid.Lidnummer , lid.Naam , lid.Voornaam , lid.Huisnummer , lid.Postcode , postcode.Adres , postcode.Woonplaats , GROUP_CONCAT(telefoonnummers.Telefoonnummer SEPARATOR ', ') as Telefoonnummer , GROUP_CONCAT(email.Emailadres SEPARATOR ', ') as Emailadres FROM lid INNER JOIN postcode ON lid.Postcode = postcode.Postcode INNER JOIN telefoonnummers lid.Lidnummer = telefoonnummers.Lidnummer INNER JOIN email ON lid.Lidnummer = email.Lidnummer GROUP BY lid.Lidnummer; Use explicit joins and not "FROM A, B, C WHERE..." When posting code in the forum, use the <> button to create a code block and specify the code type.
  24. The JQuery usage is irrelevant - the signifcant bit is getting the day from the date object...
×
×
  • 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.