Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/113600-solved-extracting-data-from-an-array/
Share on other sites

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>";
}
?>

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.

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

 

 

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}

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!!

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 :-/

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 

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

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}

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

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.