views:

48

answers:

3

I'm using the codeigniter framework, I'm retrieving data from the database in the form of an array but when i try to use the foreach function to display the data i get an error

 Message: Object of class stdClass could not be converted to string  

this is the array

Array  
(
[0] => stdClass Object
    (
        [id] => 1
        [title] => title
        [data] => data
    )


)

*The foreach is

foreach($data as $r) echo $r  
A: 

The problem is in echo $r. $r is of type stdClass, which can not be printed like that. Try print_r($r).

Sjoerd
+2  A: 

If you try it like this it should work:

<?php foreach($data as $r): ?>
    <?php echo $r->id; ?><br>
    <?php echo $r->title; ?><br>
    <?php echo $r->data; ?><br>
<?php endforeach; ?>
captaintokyo
Oh my gosh! What purpose does this code have? I guess most of the users prefer not to put every single command inside <?php ?>. It's considered as bad coding style.
Jan.
This assumes you are in the view where you usually do most of your iterating over database records.
captaintokyo
IMHO this assumes way too much: Not every piece of code is meant to be outputted in HTML. And even if so, then why would you seperate the first line from the first echo? IT's more work for the interpreter and it's not easy readable for humans. Just my 2cents
Jan.
thats perfect ! except you should use the shorthand <?php foreach() : ?> i think it makes it look nicer . but thanks
andrei
Usually I use short tags, which makes it more readable, but I cannot assume TS has that enabled on his server.
captaintokyo
@andrei: thanks. @Jan: OK suppose the first line is wrapped in `<p>`'s, what would you write? `echo "<p>$r->id;</p>";`? It's my personal preference to write `<p><?php echo $r->id; ?></p>` or actually usually I just write `<p><?= $r->id ?></p>`. Anyway, my answer is correct. The fact that you have different preferences doesn't mean you have to vote me down...
captaintokyo
Calm down captain :) Voting system is for subjective opinions, not impartial assessments. Anyone is free to vote in any direction and answer votes has very little in common with it's real value anyway. @Jan as a matter of fact it's widely used solution, state-of-art in HTML output nowadays
Col. Shrapnel
Actually the voting system is made to show your preference of different approaches. And I dislike the mixture of the rendering layer and php code - which is quite common in the world of OOP. but sure - continue to write a system which is not future-oriented. It's your choice.
Jan.
@Jan using PHP code in HTML like this is totally acceptable IMO and I can't see how this is less future-oriented than using a full-blown templating system (that replicates what PHP does anyway) - as long as you stick to variables and simple control structures. PHP *operations* and *calculations* inside HTML is what's evil. (I am a happy user of template engines in many projects by the way, but they're not always needed.)
Pekka
It doesn't mean that there's no benefit if you can't see one. because I like arguing on the net I add the following: A template system does not replicate what you see here. Also a template engine must not be "full-blown" - I have no idea what you're talking of.. take a look at fxl_template for example. A good template engine is lightweight and at best offers caching mechanisms and alike. Smarty even offers control structures. Did you ever updated such a mixture of PHP+HTML from.. say HTML 3.x to valid xhtml strict?! Have fun dude!
Jan.
+2  A: 

Hi there

Look close! You are putting the outter Array into the foreach loop. Every "$r" is then one Object with the properties id, title, data.

Try

foreach ($data as $k => $r) {
    echo $r->id;
}
Jan.