View previous topic :: View next topic |
Author |
Message |
shaung Smarty Rookie
Joined: 08 May 2003 Posts: 5
|
Posted: Thu May 08, 2003 11:01 am Post subject: array problems |
|
|
Hi
This is a dump of my array that gets passed through from the debug console.
{$news_10} Array (3)
ref => Array (4)
0 => 410wxr7c740q1u25uwpmhygcn1sb4u7ufb1pm...
1 => fhw2h8hcqc3kab8zq97evqs96t2cr3u1xra4r...
2 => w7p95wx1jbcr37rq7m8tx3mant15b89nhmhn5...
3 => 6qtx6sjhgpwcjh28cxsjex82n576hrwr16z1n...
teaser => Array (4)
0 => Preparations for the modular units ca...
1 => Mr Bisset produces new road map to ta...
2 => test
3 => test
titles => Array (4)
0 => New Term
1 => Hot news
2 => test
3 => Test
What I want to do is loop through the array and output each part.
E.g.
$news_10["titles"][0] would be output New Term which would user ref and teaser to make up a url.
I dont know how to setup Smarty to do this. I need all the different sections of array to be outputed together so I can make the links.
Please help
Cheers
Shaun Garriock |
|
Back to top |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Thu May 08, 2003 11:37 am Post subject: |
|
|
You can loop through using foreach and $news_10.ref[0] would be the first element in the ref array inside the news_10 array. _________________ "Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane |
|
Back to top |
|
shaung Smarty Rookie
Joined: 08 May 2003 Posts: 5
|
Posted: Fri May 09, 2003 2:43 pm Post subject: |
|
|
Hi
I have got this in my template file.
Code: | {section name=general_item loop=$general_news_10}
//STARTS THE SECTION WITH THE NEWS_10 ARRAY (renamed it)
<li><a href="news/?ref={$general_news_10.ref[general_item]}" title=" {$general_news_10.teaser[general_item]} " class="popup">
{$general_news_10.titles[general_item]}</a></li>
//THE ABOVE JUST PRINTS THE CONTENT
{if $smarty.section.general_item.last}
//THERE WAS A BUG WHICH MEAN THE SECTION WOULD NOT PRINT THE LAST RECORD SO I ADDED THIS
<li><a href="news/?ref={$general_news_10.ref[general_item.index_next]}" title=" {$general_news_10.teaser[general_item.index_next]} " class="popup">{$general_news_10.titles[general_item.index_next]}</a></li>
//END OF THE FIX
{/if}
{/section} |
but when I have this further down
Code: | {section name=sports_item loop=$sport_news_10}
//START OF SPORTS NEWS SECTION
{$smarty.section.sports_item.index}
//CHECKING TO SEE WHAT RECORD IT ON (just testing)
<li><a href="news/?ref={$sport_news_10.ref[sports_item]}" title=" {$sport_news_10.teaser[sports_item]} " class="popup">{$sport_news_10.titles[sports_item]}</a></li>
//JUST PRINTING ITS VALUES
{if $smarty.section.sports_item.last}
//LAST RECORD FIX
<li><a href="news/?ref={$sport_news_10.ref[sports_item.index_next]}" title=" {$sport_news_10.teaser[sports_item.index_next]} " class="popup">{$sport_news_10.titles[sports_item.index_next]}</a></li>
{/if}
{/section} |
but it dont work. It seems to loop over the same amount as the other section.
This is the array for sports news:
Code: | //DUMP OF THE SPORTS NEWS ARRAY FROM DEBUGGING CONSOLE
{$sport_news_10} Array (3)
ref => Array (1)
0 => h6b4ueexbefhx61px58k4bp0x5e34k3m44sts...
//REF IS THE ARTICLE ID
teaser => Array (1)
0 => Will Skingley sign up this year?
//JUST A TEASER LINE FOR THE ARTICLE
titles => Array (1)
0 => Sports Day
//ARTICLE TITLE |
I dont know why this is happening. When I try to use a foreach it displays nothing with following syntax
Code: | {foreach from=$sport_news_10 item=nav name=nav}
{$sport_news_10.title[nav]}
{/foreach} |
Please please help me.
Cheers
Shaun Garriock
Last edited by shaung on Fri May 09, 2003 3:11 pm; edited 2 times in total |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri May 09, 2003 2:47 pm Post subject: |
|
|
@Shaun:
I'd be willing to take a look at your code if you would be willing to edit your message so that the code is formatted. As it is, I can't follow it.
Good Luck |
|
Back to top |
|
shaung Smarty Rookie
Joined: 08 May 2003 Posts: 5
|
Posted: Fri May 09, 2003 3:11 pm Post subject: |
|
|
Try it now and tell me what you think
Cheers for the help man |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri May 09, 2003 3:46 pm Post subject: |
|
|
Thanks for updating your message
I just looked at the original data stream and I will take that to task.
1) you are passing in an array that has three related "rows". Each item in each row corresponds to the item at the same ordinal position in the other rows. Okay. Firstly, if this structure is causing a lot of problems, why not reformat it prior to sending it to Smarty, so that each row was actually the record that you want formatted? As it is, you are going to have to manually build the record in Smarty. It will be difficult to extend that in the future.
2) Okay, now lets examine what is up: I think Aztek is on the right track, but I think he left some details out. The first thing to notice is that you can't simply do a foreach through the main array. That's because you aren't interested in looping the ROWS, you are interested in looping the ROW ITEMS. You have to assume that the Smarty template will expect the ref, teaser and titles arrays to belong to the news_10 array. That means you do something like this:
Code: | {section name=sports_item loop=$sport_news_10.ref}
{$sport_news_10.ref[sports_item]}
{$sport_news_10.title[sports_item]}
{$sport_news_10.teaser[sports_item]}
{/section} |
That is, you create a loop for ONE of the row items and then you ASSUME that each of the other rows contain exactly the same number of items.
Does that work for you? |
|
Back to top |
|
shaung Smarty Rookie
Joined: 08 May 2003 Posts: 5
|
Posted: Fri May 09, 2003 4:07 pm Post subject: |
|
|
Hi
I get what you are saying now.
Cheers man. Going to reformat my arrays to have ref etc split into there own arrays.
I thought that one array would have been simplest but it was not hehe
Cheers anyway. I will let you know if I have any problems. _________________ From Shaun Garriock
Hello. I am a signature virus. Please copy me to other signatures to help me spread |
|
Back to top |
|
shaung Smarty Rookie
Joined: 08 May 2003 Posts: 5
|
Posted: Fri May 09, 2003 4:23 pm Post subject: |
|
|
Hi
Got it working. Used your code snippet. Very nice.
Cheers again _________________ From Shaun Garriock
Hello. I am a signature virus. Please copy me to other signatures to help me spread |
|
Back to top |
|
|