How can I pass variables in a foreach loop into a view?

#5
[eluser]RobertSF[/eluser]
Oops, sorry, now that I look closer at the code, are you sure it's working except for the wrong $data? I ask because according to the link you have there
Code:
<td  vertical-align: middle;">
    <div class="button"><a href="#"><img src="assets/images/search20.png"/></a></div>
    &lt;?php $this->load->view('popup', $data); ?&gt;
</td>
nothing at all should happen when you click on it. The load->view you're doing would happen when the page is rendered, not when the link is clicked. In other words, the code loads the view into the table cell. It wouldn't seem to open it when you clicked the view. Are you using some Javascript that makes it work?

Ok, if I needed to have a custom link for each row in a table that opened a window and passed it different data, here's how I would do it.

First, I'd setup a controller called "popup"

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Class Popup extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        if (isset($_GET['subtime']))
        {
            $data = array('subtime' => $_GET['subtime']);
            $this->load->view('popup', $data);
        }
    }
}

Then, in the code that builds the table, I'd do this:

Code:
$this->load->helper('url'); // in the constructor for this class

&lt;?php
    $popup_vars = array(
        'width' => '800',
        'height' => '600',
        'scrollbars' => 'yes',
        'status' => 'yes',
        'resizable' => 'yes',
        'screenx' => '0',
        'screeny' => '0');

foreach ($rows as $r)
{
?&gt;
    <tr>
        <td  vertical-align: middle;">
            <div class="button">
            &lt;?php echo anchor_popup(
                'popup?subtime=' . $r-&gt;Submitted,
                '<img src="assets/images/search20.png"/>',
                $popup_vars); ?&gt;
            </div>
        </td>
        <td>&lt;?php echo date("F j, Y, g:i a", $r->Submitted); ?&gt;</td>
        <td>&lt;?php echo $r->firstName; ?&gt;</td>
        <td>&lt;?php echo $r->lastName; ?&gt;</td>
        <td>&lt;?php echo $r->email; ?&gt;</td>
        <td>&lt;?php echo $r->phone; ?&gt;</td>
        <td ">&lt;input type="checkbox" name="check" value="check"&gt;&lt;/td>
    </tr>

That anchor_popup() is a function in the URL helper. It creates a popup link. The link is to a page (controller) called popup, and it includes the GET variable subtime=whatever-subtime-is. When you click on the link, it calls the popup controller, which should open in a new window. The popup controller looks for the GET variable, puts it into the $data array, and loads the popup view (which is separate from the popup controller).

I know it looks kind of complicated. If someone else knows of a simpler way, I hope they post it.


Messages In This Thread
How can I pass variables in a foreach loop into a view? - by El Forum - 10-03-2014, 08:14 AM
How can I pass variables in a foreach loop into a view? - by El Forum - 10-03-2014, 03:19 PM
How can I pass variables in a foreach loop into a view? - by El Forum - 10-03-2014, 07:28 PM
How can I pass variables in a foreach loop into a view? - by El Forum - 10-03-2014, 08:17 PM
How can I pass variables in a foreach loop into a view? - by El Forum - 10-03-2014, 09:56 PM
How can I pass variables in a foreach loop into a view? - by El Forum - 10-06-2014, 08:16 AM
How can I pass variables in a foreach loop into a view? - by El Forum - 10-06-2014, 05:00 PM
How can I pass variables in a foreach loop into a view? - by El Forum - 10-14-2014, 03:31 PM
How can I pass variables in a foreach loop into a view? - by El Forum - 10-15-2014, 06:29 AM
How can I pass variables in a foreach loop into a view? - by El Forum - 10-16-2014, 09:31 AM
How can I pass variables in a foreach loop into a view? - by El Forum - 10-16-2014, 05:48 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.