Welcome Guest, Not a member yet? Register   Sign In
Help with echoing values.
#1

[eluser]Frunkie[/eluser]
I have a main template file that contains everything I want. In a small area within this template, I want to echo the view that is associated with the link that the user clicks on. This is not a problem so far however, when I have to loop the data it is not so easy anymore.

If I have this as my template/view:
Code:
table
tr
td>&lt;?php echo $myContent; ?&gt;</td
/tr
/table

You can clearly see how I would be able to echo the values of the variable. Now, suppose that I wanted to show the user something that required multiple rows of data. I would then have to use a loop. So, you see, it depends on the content that I want to display that determines what construct to use within the HTML.

Can someone please help me to figure this out? I'm sure this is just me not correctly understanding how this should be done.

Basically, I want to echo the values in the same page, NOT a different page. Everything should stay with a single view.

Thanks
#2

[eluser]rogierb[/eluser]
use is_object() to see it a result object or a single variable.

If it is a object, loop, if not, echo content.
#3

[eluser]Frunkie[/eluser]
** bump. I accidentally posted two identical posts.
#4

[eluser]Frunkie[/eluser]
[quote author="Frunkie" date="1255962062"][quote author="rogierb" date="1255961004"]use is_object() to see it a result object or a single variable.

If it is a object, loop, if not, echo content.[/quote]
Thanks for the response. Ok, let me pose this scenario to you because this is where I am hung up. I already thought of doing this but I am running into a problem when I have to specify the attributes of the object. Maybe an example would help.

Code:
if(is_object($content))
{
  foreach($content as $value)
  {
    echo $value['dayOfWeek'];
  }
}
else
{
  echo $content;
}

Say that the attributes within the object are the days of the week. For *this particular* view I have hardcoded the values into the foreach loop but what about the next view that *also* uses an object, whos attributes are not days of the week but something else? I would have to know beforehand what I need to echo so that I can hardcode it into the loop. Am I missing something? I can fix this by hardcoding if/else statements into the template file abut I am trying to get away from this and keep it as dynamic as I can. Is this possible?[/quote]
#5

[eluser]rogierb[/eluser]
Code:
//use variable variables
foreach($content as $element=>$value)
  {
    echo $$element; //OR
    echo${$element};
  }
#6

[eluser]n0xie[/eluser]
It would probably be easier using partial views. You render your content in a different view and then store it into a variable, which you echo in your 'main' view. This way you don't have to put logic in your view about how, or what it should display.

If you add 'TRUE' as third parameter to your view, it won't output the data to the browser, but instead return it.
Code:
// controller
$partial['content'] = $this->load->view('somepartialview', $somedata, TRUE);
$this->load->view('maintemplate', $partial);

// view
echo $content;
#7

[eluser]Frunkie[/eluser]
[quote author="rogierb" date="1255965132"]
Code:
//use variable variables
foreach($content as $element=>$value)
  {
    echo $$element; //OR
    echo${$element};
  }
[/quote]

Thanks. I have tried this every which way and can't get this to work correctly. Arrays were never my strong point so I'm sort of struggling with this concept. I am thinking that if I have, say 5 attributes that I want to echo, I see that I can use the key/value pair but that only covers me for 2 (The key and the value). Sorry if I'm dense but I don't know how to overcome this with more than 2 attributes.
#8

[eluser]rogierb[/eluser]
Can you give an example of the array?
#9

[eluser]Frunkie[/eluser]
[quote author="rogierb" date="1255965802"]Can you give an example of the array?[/quote]

Sure, I can give you an example but one array will differ from the next array. I may have an array like:

Code:
Array
(
    [weekday] => Array
        (
            [0] => monday
            [1] => tuesday
            [2] => wednesday
            [3] => thursday
        )
)

And another like:

Code:
Array
(
    [weekday] => Array
        (
            [open] => Array
                (
                    [0] => monday
                    [1] => tuesday
                    [2] => wednesday
                    [3] => thursday
                )

        )
)

Anyhow, you get the idea. If I am using a key/value pair then how would I access the second array? Thanks for the help.
#10

[eluser]Frunkie[/eluser]
[quote author="n0xie" date="1255965246"]It would probably be easier using partial views. You render your content in a different view and then store it into a variable, which you echo in your 'main' view. This way you don't have to put logic in your view about how, or what it should display.

If you add 'TRUE' as third parameter to your view, it won't output the data to the browser, but instead return it.
Code:
// controller
$partial['content'] = $this->load->view('somepartialview', $somedata, TRUE);
$this->load->view('maintemplate', $partial);

// view
echo $content;
[/quote]
Thnaks Noxie for the help. If I can't use a generic array in my view then I will probably have to do something like this.




Theme © iAndrew 2016 - Forum software by © MyBB