I am making an Employees CRUD application.
I have an Employees HTML table displaying users from a data base table called "users", like below (the template file is users.php):
I wanted to click the view user link at the right of the first user (that eye icon) to display his profile. What method should I use, considering I have a $get_users
array containing all the users data so that:
<pre><?php print_r ($get_users); ?></pre>
displays
Array
(
[0] => Array
(
[id] => 1
[first_name] => Alex
[last_name] => Smith
[email] => alex.smith@gmail.com
[telefon] => 0743127315
[oras] => London
[adresa] => 5th Shaftesbury Ave.
)
[1] => Array
(
[id] => 5
[first_name] => Doe
[last_name] => John
[email] => jdoe80@yahoo.com
[telefon] => 0743127315
[oras] => London
[adresa] => 5th King's Road
)
)
and of course <?php print ($get_users[0]['first_name']); ?>
displays first user's first name - Alex. But I cant just hardcode a view_user.php page for every user in the data base.
My view_user.php file (template) contains this HTML table:
<table id="single_user" class="table table-striped table-bordered">
<tbody>
<tr>
<td>First name</td>
<td><?php echo $get_users[0]['first_name']; ?></td>
</tr>
<tr>
<td>Last name</td>
<td><?php echo $get_users[0]['last_name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td><a href="mailto:<?php echo $get_users[0]['email']; ?>"><?php echo $get_users[0]['email']; ?></a></td>
</tr>
<tr>
<td>Phone</td>
<td><?php echo $get_users[0]['telefon']; ?></td>
</tr>
<tr>
<td>City</td>
<td><?php echo $get_users[0]['oras']; ?></td>
</tr>
<tr>
<td>Address</td>
<td><?php echo $get_users[0]['adresa']; ?></td>
</tr>
</tbody>
</table>
I need that $get_users
array indexes generated dynamically: $get_users[index]
where index takes values from 0 to the total number of users minus one.
So what is the fastest and safest way to do that? (AJAX?)
You can:
/view_user.php?id=3
where:
$user_id = $_GET['id'];
$sql = "SELECT * FROM `users` WHERE `user_id` = " . (int)$user_id;
EDIT:
Well you should be able to use $_GET['id'] just fine in your function.
$sql = "SELECT * FROM `users` WHERE `user_id` = " . (int)$_GET['id'];
But keep in mind that this approach is very wrong for many reasons.
You should start reading about and use a framework like Laravel which provides all the necessary tools needed to make your life easier and just to be able to concentrate on your business logic, instead of reinventing the wheel.