Reputation: 1316
Perhaps this is a very basic question, but I couldn't find any appropriate answer after Googling, and I'm quite new to this. How can I parse the value from a PHP foreach loop into a url and make it a query string?
Sounds unclear? Let me explain.
So, here's my PHP code:
<?php foreach ($results as $data){ ?>
<tr>
<td><?php echo $data->emp_id; ?></td>
<td><?php echo $data->emp_name; ?></td>
<td><?php echo $data->emp_mobile_no; ?></td>
<td><a href="<?php echo base_url(); ?>index.php/admin_logins/employee3"><button id="button_edit">Edit</button></a></td>
<td><a href="<?php echo base_url(); ?>index.php/admin_logins/employee4"><button id="button_delete">Delete</button></a></td>
</tr>
<?php }?>
Now, what I want to do is something like this:
<td><a href="<?php echo base_url(); ?>index.php/admin_logins/employee3/value of $data->emp_id"><button id="button_edit">Edit</button></a></td>
And, similarly, this as well:
<td><a href="<?php echo base_url(); ?>index.php/admin_logins/employee4/value of $data->emp_id"><button id="button_delete">Delete</button></a></td>
Now, what's the proper way to do this?
Upvotes: 0
Views: 193
Reputation: 37045
Here's a way to do it with heredoc to make it easier to look at:
<?php
foreach ($results as $data) {
$base_url = base_url();
echo <<<TABLEROW
<tr>
<td>{$data->emp_id}</td>
<td>{$data->emp_name}</td>
<td>{$data->emp_mobile_no}</td>
<td><a href="{$base_url}index.php/admin_logins/employee3/{$data->emp_id}"><button id="button_edit">Edit</button></a></td>
<td><a href="{$base_url}index.php/admin_logins/employee4/{$data->emp_id}"><button id="button_delete">Delete</button></a></td>
</tr>
TABLEROW;
}
?>
Upvotes: 1
Reputation: 388
What you want is to pass the ID
from view to your controller
. This should use help:
On your view:
<?php foreach ($results as $data){ ?>
<tr>
...
<td><a href="<?php echo base_url(); ?>index.php/admin_logins/employee/edit?id={$data->emp_id}"><button id="button_edit">Edit</button></a></td>
...
</tr>
<?php }?>
On your controller employee
:
function edit($id = NULL)
{
if(is_null($id))
exit('Invalid employee id');
$this->load->model('employee_model', 'employee');
$employee = $this->employee->read($id);
//do your stuff here
}
On your model `employee_model':
function read($id = NULL)
{
return $this->db->where('emp_id', $id)
->get('employee')->row_array();
}
Upvotes: -1
Reputation: 3195
Just use below code:
<?php foreach ($results as $data){ ?>
<tr>
<td><?php echo $data->emp_id; ?></td>
<td><?php echo $data->emp_name; ?></td>
<td><?php echo $data->emp_mobile_no; ?></td>
<td><a href="<?php echo base_url(); ?>index.php/admin_logins/employee3/<?php echo $data->emp_id; ?>"><button id="button_edit">Edit</button></a></td>
<td><a href="<?php echo base_url(); ?>index.php/admin_logins/employee4/<?php echo $data->emp_id; ?>"><button id="button_delete">Delete</button></a></td>
</tr>
<?php }?>
Upvotes: 4