CodeIgniter Forums - How can I pass variables in a foreach loop into a view?
CodeIgniter Forums > CodeIgniter Archived (from EllisLab) > Archived General Discussion > How can I pass variables in a foreach loop into a view?
Full Version: How can I pass variables in a foreach loop into a view?
I have a table populated with a foreach loop. I'm trying to make it so when you click on the search20.png image it opens the popup view, and passes the corresponding $subtime for that row into the popup view to use.
You can't pass that data as a variable because, when you say $this->load->view('popup', $data);, $data is not what it was when you built each row in the table. It's whatever it is at the moment.
That way, the $r->Submitted specific to each table row gets embedded in the array you pass to the view.
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"
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.
I dont know if its even possible to pass a value into a window like that?
I'm trying to create a little more control over what the user can do. These windows will have the editable details of our product sheet requests so I'm trying to steer clear of making another browser window open unless its some kind of dialogue window that wont allow you to do anything else until you close that window. Do you know of any way I can achieve something like this?
You're welcome! Just so you know, I've since corrected a couple of errors in the source I provided. One is the missing parenthesis in if (isset($_GET['subtime']). There is an old programming language called LISP, and many witticisms arose from its use of parenthesis. The second error was that anchor_popup() must be echoed. It only generates the strong; it doesn't send it to the HTML source. I've since tested what I posted, so if you ever need a non-modal popup window, that's a way of doing it in Codeigniter.
You could pass it the value of $r->Submitted, but that value would have to be embedded in the HTML code for each row in your table, and I think you'd also need to add onclick="$.modal(....)" to the img tag in each row.
If you've done any development in Visual Studio/Visual Basic, this will seem very different because the web operates in a stateless manner. The application runs and ends with each page load, unlike in desktop development, where the application is always running in memory. I'm pretty new at it too, and it sure is confusing sometimes!
hm that sounds a little over my head at the moment. I just started using CodeIgniter.
Maybe I'll just start with passing the value to another view and try doing it with a popup later on.
WHERE `form_name` = 'PSRequestForm' AND submit_time = ".$submitTime."
I don't know what $submitTime is. Generally, variables declared or used in one function are not available in other functions. You can make variables global, so they're available anywhere in your code or project, but in most cases, that's considered poor practice.
A note about names, too. There is no connection between the name of the variable you pass as an argument and the name of the parameter in your function definition. You could call the parameter $george, as long as you referred to it as $george in your function.
$q = $this->db->query("SELECT etc, etc.
The function has no return in case of zero rows. A function should have a return even if it's just to return zero or false.
Then, back at the ranch, you can test if $q (which will be an array) has any elements in it.
Anyway, I hope putting in the missing parameter is all you need to make it basically work. Does your editor have a debugger? The debugger for PHP is called XDebug, and many editors designed for PHP integrate it into their functions. I know Aptana, Code Lobster, and PHPEdit have it. Even Notepad++ has an XDebug plug in. It's invaluable to be able to execute your code line by line and see what's happening.