Welcome Guest, Not a member yet? Register   Sign In
How can I pass variables in a foreach loop into a view?
#9

[eluser]RobertSF[/eluser]
I think I see the problem.

In your view, you have:
Code:
<?php echo anchor("main/request_details/$r->Submitted", 'View'); ?>

And in your controller, you have:
Code:
public function request_details() {

Your function definition needs a parameter to receive the argument you're sending it. It should look something like this:
Code:
public function request_details($r_Submitted) {

Then, deeper into your controller code, if you want to pass that value to the model when you do your query, instead of:
Code:
$data['rows'] = $this->model_data->getDetail($submitTime);

you would have:
Code:
$data['rows'] = $this->model_data->getDetail($r_Subtmitted);

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.

Code:
<?php
public function square($george)
{
  return $george * $george;
}

$x = 33;
$y = 2;
echo square($x);
echo square($y);
echo square(20);

One last thing. In your model function getDetail(), you have:

Code:
$q = $this->db->query("SELECT etc, etc.
                LIMIT 0,100");
if($q->num_rows() > 0){
    foreach($q->result() as $rows){
        $data[] = $rows;
    }
    return $data;
}

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.

Also, that whole if statement and the foreach inside can be replaced with Codeigniters' result_array(), like this:

Code:
$q = $this->db->query("SELECT etc, etc.
                LIMIT 0,100")->result_array();
return $q
}

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.


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



Theme © iAndrew 2016 - Forum software by © MyBB