Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 03/18/2023 in all areas

  1. If you want to store a path to an image in the DB, just use a VARCHAR column with some resonable maximum length. If you have control over the images file names (ie, you're making them / receiving them as an upload) then you can choose a length and just stick to it. For uploaded files, I tend to use a VARCHAR(16) and generate a random name of that length for the file. If you don't control the name (say, you're just storing a URL to an external image) then choose a maximum length that should be fine and just validate that in your form when creating a record. Either that or us a text column if you want to allow crazy long URLs. I tend to use VARCHAR(2048) for arbitrary URLs.
    1 point
  2. I think you're making things more difficult that it really needs to be? Here's an example of how you can save an image path in PHP: $image_path = "images/my_image.jpg"; In this example, the variable $image_path is set to the path of the image file "my_image.jpg" located in the "images" directory. You can then use this variable to display the image on a webpage or perform other operations with the image file. To display an image in HTML, you can use the <img> tag and set the src attribute to the path of the image file. <img src="images/my_image.jpg" alt="My Image">
    1 point
  3. 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>
    1 point
This leaderboard is set to New York/GMT-05:00
×
×
  • 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.