View previous topic :: View next topic |
Author |
Message |
sunadmn Smarty Rookie
Joined: 17 Aug 2005 Posts: 5
|
Posted: Thu Aug 18, 2005 3:00 pm Post subject: |
|
|
Hey boots thanks for pointing me to this article it has given me some ideas, but I need some help understanding this concept more please. Ok so if I am reading this correct it would appear that you guys are assuming that you always know the names of your arrays and elements, am I way off here? Also this doesn't seem to really work for true dynamic arrays such as the case that I have since I take a SOAP return and then do a foreach on it then assign $key $value like this:
Code: |
$items = $client->call($method, $values, $namespace);
//print_r($items);
//die;
if (!$items) {
return pnVarPrepHTMLDisplay(_pnOSSAPIFAILED);
}
$pnRender =& new pnRender('pnOSSAPI');
$pnRender->caching=false;
foreach($items as $key => $value) {
$pnRender->assign($key, $value);
}
// Return the output that has been generated by this function
return $pnRender->fetch('pnOSSAPI_user_return.htm');
}
|
Since I do the above I can only spec out to a certain level but the level I can go to will change if I return more than one, so my XML response if formed like this:
Code: |
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<discover_directory_response xmlns="http://my.domain.com/ossapi/">
<account>
<group>
<hoh>HSIparent</hoh>
<children>
<child>HSIchild1</child>
<child>HSIchild2</child>
</children>
</group>
<group>
<hoh>1234567890</hoh>
</group>
</account>
<error_code>0</error_code>
<error_message>success</error_message>
</discover_directory_response>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
|
So in the above response you will see that I can have group and then if more than one group becomes another array in numeric value here is an emaple of the return:
Code: |
Array ( [account] => Array ( [group] => Array ( [0] => Array ( [hoh] => windsoft2341 [children] => Array ( [child] => Array ( [0] => windsoft2341child1 [1] => windsoft2341child2 [2] => windsoft2341child3 [3] => windsoft2341child4 [4] => windsoft2341child5 [5] => windsoft2341child6 ) ) ) [1] => Array ( [hoh] => 7192663087 ) ) ) [error_code] => 0 [error_message] => success )
|
My array returns can get very very complex and deep so with that said does this seem like a simple issue to overcome or is this really a nasty mess??
Thanks for anytime you all put into this question, I think I am in a bit over my head right now and just need a dumbed down explanation of what I should do here or where to look for the answer I need, sorry if I am just not getting what you guys have posted here like I said I think I am just way above my level of comprehenssion here.
Thanks again all
-SUNADMN |
|
Back to top |
|
alex Smarty Rookie
Joined: 03 Sep 2005 Posts: 19 Location: Argentina
|
Posted: Sun Sep 04, 2005 11:34 pm Post subject: |
|
|
Can the compiler.defun.php function also display non associative arrays like this one:
Code: | Array
(
[0] => test1
[1] => Array
(
[0] => test1.1
)
[2] => Array
(
[0] => test1.2
[1] => Array
(
[0] => test1.2.1
[1] => test1.2.2
)
)
[3] => test2
) |
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Sep 04, 2005 11:49 pm Post subject: |
|
|
@alex: sure, it is really no different. Lets say that on the first entry into the function you know you get an array so you start a foreach on it. Then for each element in the foreach you check: is it an array or is it a scalar value? If it is an array, then recurse with that value; otherwise, print the scalar value and move on. Of course, if you are using PHP5 you may also want to check for object that implement iterators. Depending on your needs, you may also want to track and pass a depth value as well.
I should note that I have found this technique very useful for protyping functions but in the end I tend to port these back to real plugins. |
|
Back to top |
|
alex Smarty Rookie
Joined: 03 Sep 2005 Posts: 19 Location: Argentina
|
Posted: Mon Sep 05, 2005 5:08 pm Post subject: |
|
|
boots wrote: | ...then for each element in the foreach you check: is it an array or is it a scalar value? If it is an array, then recurse with that value; otherwise, print the scalar value and move on... |
Hm... is this just a template thing or do I have to change something in the compiler.defun.php?
Alex |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Sep 06, 2005 3:00 am Post subject: |
|
|
@alex: hmmm. I can't see why you would have to change anything. |
|
Back to top |
|
alex Smarty Rookie
Joined: 03 Sep 2005 Posts: 19 Location: Argentina
|
Posted: Tue Sep 06, 2005 2:26 pm Post subject: |
|
|
...but how can I check in the template whether it is an array or not? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Sep 06, 2005 2:41 pm Post subject: |
|
|
@alex: as explained in the manual -- you can use PHP functions in if statements and as modifiers.
{if is_array($foo)} ... {/if} |
|
Back to top |
|
alex Smarty Rookie
Joined: 03 Sep 2005 Posts: 19 Location: Argentina
|
Posted: Tue Sep 06, 2005 2:54 pm Post subject: |
|
|
boots wrote: | @alex: as explained in the manual -- you can use PHP functions in if statements and as modifiers.
{if is_array($foo)} ... {/if} |
OK... didn't know that. Thank you! |
|
Back to top |
|
alex Smarty Rookie
Joined: 03 Sep 2005 Posts: 19 Location: Argentina
|
Posted: Fri Sep 09, 2005 5:02 pm Post subject: |
|
|
OK - that works fine so far!
Another question: is it somehow possible to get the information about the deepness of an item like displayed below (values in brackets)?
I probably have to assign a variable and increment it at the right place... but how?
- item 1 (0)
- item 1.1 (1)
- item 1.2 (1)
- item 1.3 (1)
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Sep 09, 2005 5:14 pm Post subject: |
|
|
Hi alex.
If you read through the thread, I demonstrate using a simple level parameter. You would have to modify it so that it acted like a stack and incremented accordingly. The general principle would be the same although you would likely be passing in a more structured value than a simple level number. |
|
Back to top |
|
alex Smarty Rookie
Joined: 03 Sep 2005 Posts: 19 Location: Argentina
|
Posted: Fri Sep 09, 2005 9:00 pm Post subject: |
|
|
boots wrote: | If you read through the thread, ... |
...quite rich in content this thread yet is - think I found it on page 4. Thank you, boots! |
|
Back to top |
|
Jeremy- Smarty Rookie
Joined: 11 Feb 2005 Posts: 16
|
Posted: Fri Dec 16, 2005 4:47 am Post subject: |
|
|
This is the the structure of my array:
Code: | Array
(
[1] => Array
(
[id] => 1
[name] => General
[parent_id] => 0
[sub_forums] => Array
(
[2] => Array
(
[id] => 2
[name] => News and announcements
[parent_id] => 1
[sub_forums] => Array
(
)
)
[3] => Array
(
[id] => 3
[name] => General discussion
[parent_id] => 1
[sub_forums] => Array
(
)
)
)
)
[4] => Array
(
[id] => 4
[name] => Support
[parent_id] => 0
[sub_forums] => Array
(
[5] => Array
(
[id] => 5
[name] => Technical Support
[parent_id] => 4
[sub_forums] => Array
(
)
)
[6] => Array
(
[id] => 6
[name] => Support Tickets
[parent_id] => 4
[sub_forums] => Array
(
)
)
)
)
) |
How would I go about using this plugin to format that array to something like this:
Forum 1
Forum 2
From 3
Forum 4
Forum 5
Forum 6
And even farther, sub forums within sub forums... etc.
I'd tried:
<ul>
<% defun name="testrecursion" list=$data2 %>
<% foreach from=$data2 item=element %>
<li><% $element.id %>
<% if $element.element %>
<ul><% fun name="testrecursion" list=$element.element %></ul>
<% /if %>
</li>
<% /foreach %>
<% /defun %>
</ul>
Bt that just gives me forums 1 and 4 and no sub forums.
What am I doing wrong? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Dec 16, 2005 5:10 am Post subject: |
|
|
Hi.
Seems like you maybe want to test for the sub_forums key being a non empty array (perhaps just !empty($element.sub_forums) -- you should then pass $element.sub_forums as your list parameter) and possibly tracking depth.
That said, if you only intend on returning a fixed format (ie: html lists) I might rather suggest a dedicated plugin to handle your custom structure. I do that for lists adding parameters so that the designer can choose class names and style sheets that will be used. |
|
Back to top |
|
Jeremy- Smarty Rookie
Joined: 11 Feb 2005 Posts: 16
|
Posted: Sat Dec 17, 2005 3:27 pm Post subject: |
|
|
Do you have an example of that code I could check out? That sounds more along the lines of what I need to do. |
|
Back to top |
|
majglow Smarty n00b
Joined: 01 Mar 2006 Posts: 1
|
Posted: Wed Mar 01, 2006 8:46 am Post subject: |
|
|
messju wrote: | I updated the pluging at http://lammfellpuschen.de/compiler.defun/ and added a more robust implementation of removing the trailing '<?php '. this version should work around the tokenizer bug.
please try that and tell me if it works for you. |
This link isn't working anymore. What is the latest status of this function? I am on PHP5 & the latest smarty. |
|
Back to top |
|
|