Jump to content

renaming array


dmcglone

Recommended Posts

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.tpl
page2.tpl
page3.tpl
 

but I'm sure anyone can see that I don't want the .tpl to show in the menu.

 

Blessings

David M.

Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

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'
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.  :happy-04: 

Link to comment
Share on other sites

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.