0

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?

3 Answers 3

4

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 }?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot @hardik. I didn't know this syntax previously. This is exactly what I was looking for.
@CrescentMoon glad to helping you... :)
1

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;
}
?>

1 Comment

+1 Completely forgot about heredoc. It's another nice way to do it.
-1

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.