dmcglone Posted August 7, 2013 Share Posted August 7, 2013 Thought I was pretty good with this stuff until now.What I'm doing here is practising with smarty and I'm trying to take these array's and assign them a name such as page 1, page 2, page 3 for the purpose of displaying them in a menu, but I can't change the numbers in the array because they are used in the URL to pull the correct data from a db for that specific page. $menu = array ( '1' => 'page1.tpl', '2' => 'page2.tpl', '3' => 'page3.tpl' ); I've tried some variations but failed miserably.So, with this current code my menu on the page looks like this:page1.tplpage2.tplpage3.tpl but I'm sure anyone can see that I don't want the .tpl to show in the menu. Blessings David M. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 7, 2013 Share Posted August 7, 2013 (edited) The problem is not the array, but how you are using it. There are many different solutions, but without knowing exactly how the array is used I can't really provide a best case solution. Here are a few options. 1. Change how the value in the array is used based upon the context. For example, you could remove the .tpl from the vaues, then when iterating over the array use the base value as the text to display and then append .tpl for the purpose of what file to load. 2. You could change the array so the key is one value and the value another. For example: $menu = array ( 'Page 1' => 'page1.tpl', 'Page 2' => 'page2.tpl', 'Page 3' => 'page3.tpl' ); Now you can iterate over the array using foreach($menu as $pageName => $pageTemplate) and be able to use both values. 3. If you find you need more than the two linked values you could use a multi-dimensional array. That allows you to store multiple parameters $menu = array ( array('page_name' => 'Page 1', 'title'=>'My first page', 'template' => 'page1.tpl'), array('page_name' => 'Page 2', 'title'=>'My second page', 'template' => 'page2.tpl'), array('page_name' => 'Page 3', 'title'=>'My third page', 'template' => 'page3.tpl') ); Edited August 7, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
dmcglone Posted August 7, 2013 Author Share Posted August 7, 2013 Thanks psycho :-) Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 7, 2013 Share Posted August 7, 2013 Thought I was pretty good with this stuff until now. What I'm doing here is practising with smarty and I'm trying to take these array's and assign them a name such as page 1, page 2, page 3 for the purpose of displaying them in a menu, but I can't change the numbers in the array because they are used in the URL to pull the correct data from a db for that specific page. Since you are already using a DB, maybe have a pages table: page_id page_name page_title page_template 1 'Page 1' 'My first page' 'page1.tpl' Quote Link to comment Share on other sites More sharing options...
dmcglone Posted August 7, 2013 Author Share Posted August 7, 2013 Thanks abracadaver, This is just practice using smarty and I think I've completely messed this one up. My earlier question turned out to be completely the wrong question and the reason I haven't slapped this stuff into a db is because of my lack of knowledge using smarty. Once I familiarized myself better with smarty I was then going to adapt it to a db and see how far I could get.I'm also burnt out, so that's not helping much right now. :-/ Quote Link to comment Share on other sites More sharing options...
dmcglone Posted August 8, 2013 Author Share Posted August 8, 2013 I took a break and played a couple hours of basketball and came back and got things working the way I intended. It was the way I was working with my arrays like psycho said. So thank for pointing me in the right direction. Now I'm going to try my hand at putting them into a db as AbraCadaver suggested. Quote Link to comment 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.