Website Talk > Web Programming

[PHP] Array hum-hum

(1/2) > >>

Fred K:
... or to put it more bluntly: §##Ø∆*!!! PHP!

Trying to add a theme picker in my sNews 1.7 admin area. Started with a simple line, just to make theme-switching easy:

--- Code: ---$theme = 'themename';
--- End code ---
(Placed after db variables for easy access)

Then I thought, I'll have a number of themes, why not make a theme picker instead? Right-ho...

Make an array, right?


--- Code: ---<?php
$theme = array(
'mars' => 'Mars',
'venus' => 'Venus',
'standard' => 'Standard',
);
?>
--- End code ---

Scratches head while trying to figure out how to use the array in a <select> thingy. Remembers: the interWebs! Yay!
Finds this (http://snippets.dzone.com/posts/show/4555):


--- Code: ---<?php
function selectfield($theme, $selected = '') {
echo '<option value="">Pick a theme</option>
';
$returnval = '';
foreach ($theme as $field=>$value) {
if ($field == $selected) {
$returnval .= '<option selected="selected" value="'.$field.'">'.$value.'</option>';
} else {
$returnval .= '<option value="'.$field.'">'.$value.'</option>';
}
}
return $returnval;
}
?>
--- End code ---
(Yes, I rewrote the output a little...)

In the place where the picker should appear:


--- Code: ---<?php
echo '<select>'; selectfield(); echo '</select>' (more html code here)
?>
--- End code ---

All good, right? Noo-no... (wouldn't be bothering you with this blab if it was)
Picker appears but theme names do not get pushed into it.
Why? Whyyyyyy-hy-hy? /*sniffle*/
What am I doing wrong?

Oh, doing "echo selectfield();" or "echo selectfield('some-name');" makes no difference, picker still not picking up theme names.

Rui Mendes:
Hello Fred.

I didn't try but maybe miss a global variable.

--- Quote ---function selectfield($theme, $selected = '') { global $theme;
   echo '<option value="">Pick a theme</option>
      ';
      $returnval = '';

--- End quote ---

Fred K:
Hi Rui. Thanks for rescuing me - that works just fine. Just need to put it all together now ;)

nukpana:
You can remove the function parameters too -  (theme, $selected = '') - $theme is the global and I assume the $selected value will come from a setting(?) so:


--- Code: --- $theme = array(
'mars' => 'Mars',
'venus' => 'Venus',
'standard' => 'Standard',
);

function themeSelect() {
global $theme;
echo '<select>';
foreach( $theme as $k => $v ) {
$selected = s('theme') == $k
? ' selected="selected"'
: '';
echo '<option '.$selected.' value="'.$k.'">'.$v.'</option>';
}
echo '</select>';
}

--- End code ---

Fred K:
Thanks Jason - your code is much better than the one I had cobbled together (naturally). I just can't wrap my head around usage.

Originally I put $theme = 'theme name here'; in my config file (config file contains db vars, site() function and language vars lines, and is included in snews.php after session_start). So when I started messing with the theme picker functions I kept them in config.php - I have no idea if that's the best or even proper place.

I've altered the db, yes, there's a new field in settings ('theme', 'Theme name')

As of now what I have in config.php is:

--- Code: ---<?php
// THEME ID (hook for styles – is also in snews.php's cat_listSEF and index.php's head block)
// Currently available theme id's are standard, mars, venus. Change theme by switching the id

$theme = array(
'mars' => 'Mars',
'venus' => 'Venus',
'standard' => 'Standard',
);

function themeSelect() {
global $theme;
echo '<select>';
foreach( $theme as $k => $v ) {
$selected = s('theme') == $k ? ' selected="selected"' : '';
echo '<option '.$selected.' value="'.$k.'">'.$v.'</option>';
}
echo '</select>';
}

?>
--- End code ---

Then in snews.php, function settings(), first setting panel, themeSelect is used like so

--- Code: ---<?php
'bla bla <li>Theme: '; themeSelect(); echo '</li> bla bla';
?>
--- End code ---

To get the right output I use the following call in a couple of places (index.php head and sidebar)

--- Code: ---<?php $theme = strotolower(s('theme'));?>
--- End code ---

and (example)

--- Code: ---<?php <link rel="stylesheet" type="text/css" href="theme/'.$theme.'/styles.php"> ?>
--- End code ---

The only thing that's not working is the theme select picker in the settings() function - it doesn't get populated with the entered db setting for 'theme' - or the array values - so the only way now to switch a theme is to go and manually change the theme value in the db. So there's something I'm missing when it comes to usage...

/* thinks: should it be echo themeSelect() in settings ..? */
/** answers: no it shouldn't... **/

(As a comparison, when I tried Rui's code - with the same setup as described above - the picker got populated but obviously nothing happened when picking a new theme since there was no action associated with that ... action.)

Sorry to be a nuisance but I just can't see what I'm missing. I'm sure it's obvious, like, "well you know you have to put that thing there in order for the thing thing to do its thing". It's just over my head...

It would be cool to have an autodetect feature for the theme picker, instead of entering names manually in an array I mean, like design a theme > collect stylesheet(s) and all resources in a folder named as the theme name > put that theme folder in the parent Theme folder. The autodetector would look in the Theme folder for first level subfolders and if folders take the folder name(s) and put in the pick-list. Easy-peasy, right? :D
But I'm not going to ask for that, it's more than extra curricculum.
v1 of the themes go as they are now, without theme picker (theme can still be switched easily); if there's help to be had to solve the empty picker issue I'll gladly take that for the planned theme upgrade; and that's about it.

Navigation

[0] Message Index

[#] Next page

Go to full version