williamZanelli Posted July 7, 2008 Share Posted July 7, 2008 Hi guys, I have the following array, I want to extract the data. Array ( [0] => stdClass Object ( [category_safe_name] => Liverpool [category_name] => Liverpool ) ) In my code I have something along the lines of for ($counter=0; $counter < sizeof($array); $counter = $counter +1){ , <a href="{$array[$counter]->category_name}">{$array[$counter]->category_safe_name}</a> This prints a URL link which is called "category_safe_name" rather than the value "Liverpool". Any ideas where I'm going wrong?? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/ Share on other sites More sharing options...
rhodesa Posted July 7, 2008 Share Posted July 7, 2008 can you post the complete code for: , <a href="{$array[$counter]->category_name}">{$array[$counter]->category_safe_name}</a> since you are using double quotes and aren't escaping them, I am guessing you are printing it with single quotes. and in that case, it won't evaluate the variables. try something like: <?php for ($counter=0; $counter < sizeof($array); $counter++){ print "<a href=\"{$array[$counter]->category_name}\">{$array[$counter]->category_safe_name}</a>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-583704 Share on other sites More sharing options...
discomatt Posted July 7, 2008 Share Posted July 7, 2008 Alternately print '<a href="'. $array[$counter]->category_name .'">'. $array[$counter]->category_safe_name} .'</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-583715 Share on other sites More sharing options...
williamZanelli Posted July 7, 2008 Author Share Posted July 7, 2008 Ahh, I should have made clear, this is a .tpl file, in the Smarty Framework As you can see in the above code, {} brqackets are used to access PHP code, Still unsure as to why the name of the object field is being printed and not the object value. Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-583754 Share on other sites More sharing options...
discomatt Posted July 7, 2008 Share Posted July 7, 2008 Check smarty. PHP is probably not to blame here. Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-583756 Share on other sites More sharing options...
rhodesa Posted July 7, 2008 Share Posted July 7, 2008 ah...my work with Smarty is minimal...but try this: {foreach from=$array item=entry} <a href="{$entry->category_name}">{$entry->category_safe_name}</a> {/foreach} Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-583779 Share on other sites More sharing options...
williamZanelli Posted July 7, 2008 Author Share Posted July 7, 2008 Hey guys, thanks for your reply. This is my code (or the bit that doesnt seem to do what it should!) {if $link_field1 neq ""} <b>{$link_field1} </b> {php} $array = unserialize($link_field1); {/php} {foreach from=$array item=entry} , <a href="{$entry->category_name}">:-:-:{$entry->category_safe_name}</a> {/foreach} {/if} I know $link_field1 is not empty as it prints out something similar to a:1:{i:0;O:8:"stdClass":2:{s:18:"category_safe_name";s:9:"Liverpool";s:13:"category_name";s:9:"Liverpool";}} Which as far as my knowledge is a Serialized array, [containing one object] - why am I not getting the relvant print out, do i need to clean some cache or something? Could it be a server problem? Maybe a bug in the FrameWork? Could I possible hav ethe wrong version of PHP?? ??? ??? ??? As I stated above im using Smarty (well Pligg, which is based on Smarty) This is all in the tpl file. Its driving me insane, I've been trying to get to the bottom of this for dayyys!! ??? ??? Any help would be useful. Thanks Will Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-583960 Share on other sites More sharing options...
rhodesa Posted July 7, 2008 Share Posted July 7, 2008 the code i posted assumed the variable $array was set my a smarty function...to just run PHP code like that try: {if $link_field1 neq ""} <b>{$link_field1} </b> {php} $array = unserialize($link_field1); foreach($array as $item){ echo " , <a href=\"".$item->category_name."\">:-:-:".$entry->category_safe_name."</a>"; } {/php} {/if} Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-583989 Share on other sites More sharing options...
williamZanelli Posted July 7, 2008 Author Share Posted July 7, 2008 Hey RhodeSa Thanks for your reply. I tried the code you provided above. This is the error message I get Warning: Invalid argument supplied for foreach() in /home/fhlinux147/f/usabillly2876.com/user/htdocs/templates_c/c_7e3f5c748c2d5b3d2782c554a847d9e9.php on line 189 I folowed the cache file, line 187-192 (foreach is line 189) <?php $array = unserialize($link_field1); foreach($array as $item){ echo " , <a href=\"".$item->category_name."\">:-:-:".$item->category_safe_name."</a>"; } ?> ??? ??? I've tried in both IE7 and FF2, same problem. Any idea where I could look now? Thanks for the pointers so far!! Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-584012 Share on other sites More sharing options...
rhodesa Posted July 7, 2008 Share Posted July 7, 2008 what did you do to get it to $link_field1 to print out? my Smarty experience is with CMSMS, in which it defines a smarty variable like so: <?php while($row = mysql_fetch_assoc($result)){ $node = new stdClass(); $node->id = $row['id']; $node->field1 = $row['field1']; $node->field2 = $row['field2']; $nodelist[] = $node; } $smarty->assign_by_ref('items', $nodelist); ?> and with a template like: <table> <tbody> {foreach from=$items item=entry} <tr> <td>{$entry->id}</td> <td>{$entry->field1}</td> <td>{$entry->field2}</td> </tr> {/foreach} </tbody> </table> sorry...but that is all i got :-/ Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-584041 Share on other sites More sharing options...
williamZanelli Posted July 8, 2008 Author Share Posted July 8, 2008 to print $link_field1 all I do is the following inside the tpl file. {$link_field1} Still havent solved the problem.. I'll post the code once its done, thanks for your help. Appreciated Will Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-584339 Share on other sites More sharing options...
rhodesa Posted July 8, 2008 Share Posted July 8, 2008 i think you need to go back to the PHP code and make $link_field1 an array of objects instead of serialized Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-584341 Share on other sites More sharing options...
williamZanelli Posted July 8, 2008 Author Share Posted July 8, 2008 Ahh well see what it is, I have an array, and I need to somehow put that into the database - hence I serialise, I extract the data from the database and unserliaze, and the idea then is to just loop though the data and output it, sounds simple enough right? I've been stuck on this problem for 5 days or so.. this exactly why i dont like using frameworks!! Do you have any other suggestions, as to how I could accomplish what I want to do? i chnaged the code to the following {if $link_field1 neq ""} <b>{$link_field1} </b> {assign var=array value=$link_field1|unserialize} {foreach from=$array item=entry} , <a href="{$entry->category_name}">:-:-:{$entry->category_safe_name}</a> {/foreach} {/if} This results in the following error Catchable fatal error: Object of class stdClass could not be converted to string in /home/fhlinux147/myurl.com/htdocs/templates_c/c_7e3f5c748c2d5b3d2782c554a847d9e9.php on line 191 Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-584348 Share on other sites More sharing options...
rhodesa Posted July 8, 2008 Share Posted July 8, 2008 but where do you define the value of $link_field1? is that something you do or something that is set by the framework automatically? $link_field1 should be unserialized into the array before the template is processed Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-584360 Share on other sites More sharing options...
williamZanelli Posted July 8, 2008 Author Share Posted July 8, 2008 Hey RhodeSa I've been doing some digging round, the template I'm using is template_lite [http://templatelite.sourceforge.net/], a lighter version of Smarty. I read up on the doc and I found out why the array in $link_field1 wasnt being serialised. So I took your suggestion on board, and before you set $link_field1 I serialized it. So now when I have the following code in the tpl {$link_field1} It prints Array - so we now know the array has been passed. I've compacted the remainder of the code in the tpl file to the following {foreach from=$link_field1 item=entry} , <a href="{$entry->category_name}">:-:-:{$entry->category_safe_name}</a> {/foreach} {/if} This is throwing the following error Catchable fatal error: Object of class stdClass could not be converted to string in /home/fhlinux147/f/myurl.com/user/htdocs/templates_c/c_7e3f5c748c2d5b3d2782c554a847d9e9.php on line 190 Could it be that the array has an object (the result of a database transaction in a previous session) thats causing this error?? Thanks for your help? Will Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-584656 Share on other sites More sharing options...
rhodesa Posted July 8, 2008 Share Posted July 8, 2008 that is weird....a couple things: 1) Clear the temp files...i see the system is loading templates_c/c_7e3f5c748c2d5b3d2782c554a847d9e9.php. Are you sure that cached file is up to date? 2) Are you sure line 190 is this line: , <a href="{$entry->category_name}">:-:-:{$entry->category_safe_name}</a> ...remember to check the cached file, not the real file, since that is what it's looking at 3) Try using something simple like this and see what it prints: {foreach from=$link_field1 item=entry} Object: {$entry}<br> Name: {$entry->category_name}<br> Safe Name: {$entry->category_safe_name}<br> <hr> {/foreach} Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-584694 Share on other sites More sharing options...
williamZanelli Posted July 9, 2008 Author Share Posted July 9, 2008 Hey Rhodesa Thanks for your advice dude, I nailed the problem. Ok what I did was, set $link_field1 to some arbitary array $array = array ("a"=>"Dog","b"=>"Cat","c"=>"Horse"); I ran the code and it worked. So I realised the Array I was serializing etc. had objects, which were causing the problem. Now I'm inputing arrays with arrays on them, ($addition is another array) //somewhere in the code $addtion = array($cat_safename => $cat_name); ................. array_push($link_field1, $addtion ); So now in my tpl I have the following loop. {if $link_field1 neq ""} { foreach value=contact from=$link_field1 } { foreach key=key value=item from=$contact } , <a href="/index.php?cats={$key}"> {$item}</a> { /foreach } { /foreach } {/if} So overall I've accomplished what I wanted to. It has however lead me to some other intersting problems [namely searching inside the array in SQL] I'll post the exact query in a new thread, rather than here. Thanks for your help Will Quote Link to comment https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/#findComment-585536 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.