Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Author Topic: [PHP] Array hum-hum  (Read 1572 times)

Fred K

  • Still trying to learn stuff
  • ULTIMATE member
  • ******
  • Karma: 130
  • Posts: 2728
    • Personal
[PHP] Array hum-hum
« on: September 08, 2011, 06:27:34 PM »

... 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: [Select]
$theme = 'themename'; (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: [Select]
<?php
$theme 
= array(
'mars' => 'Mars',
'venus' => 'Venus',
'standard' => 'Standard',
);
?>

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: [Select]
<?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;
}
?>
(Yes, I rewrote the output a little...)

In the place where the picker should appear:

Code: [Select]
<?php
echo '<select>'selectfield(); echo '</select>' (more html code here)
?>

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.
Logged

Rui Mendes

  • Development,Testing, Support
  • Hero Member
  • *****
  • Karma: 195
  • Posts: 1009
  • sNews1.7
    • Comunidade Portuguesa
Re: [PHP] Array hum-hum
« Reply #1 on: September 08, 2011, 08:37:35 PM »

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 = '';
Logged
Need a Job on Europe. Linkdin - Facebook / Group

Fred K

  • Still trying to learn stuff
  • ULTIMATE member
  • ******
  • Karma: 130
  • Posts: 2728
    • Personal
Re: [PHP] Array hum-hum
« Reply #2 on: September 08, 2011, 10:33:40 PM »

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

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: [PHP] Array hum-hum
« Reply #3 on: September 09, 2011, 03:12:09 PM »

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: [Select]
$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>';
}
Logged

Fred K

  • Still trying to learn stuff
  • ULTIMATE member
  • ******
  • Karma: 130
  • Posts: 2728
    • Personal
Re: [PHP] Array hum-hum
« Reply #4 on: September 10, 2011, 01:33:37 PM »

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: [Select]
<?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>';
}

?>

Then in snews.php, function settings(), first setting panel, themeSelect is used like so
Code: [Select]
<?php
'bla bla <li>Theme: 'themeSelect(); echo '</li> bla bla';
?>

To get the right output I use the following call in a couple of places (index.php head and sidebar)
Code: [Select]
<?php $theme strotolower(s('theme'));?>
and (example)
Code: [Select]
<?php <link rel="stylesheet" type="text/css" href="theme/'.$theme.'/styles.php"?>
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.
« Last Edit: September 10, 2011, 01:45:01 PM by Fred K »
Logged

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: [PHP] Array hum-hum
« Reply #5 on: September 10, 2011, 02:18:59 PM »

Well if you have it in settings you need to add it to setting processing code.  You also need to name the select:

so in settings - find the non EQ lines:
Code: [Select]
<?php

// Excerpt from process

$allowed_img $_POST['allowed_img'];
// EQ
$selected_theme $_POST['theme-picker'];
$ufield = array(
'website_title' => $website_title,
// EQ
'theme' => $selected_theme,

?>


Now to solve the other problem (auto picking the folders that contain the styles):
Code: [Select]
<?php

// Edit me!!
define('_THEMEPATH''path/to/themes/');

function 
getThemes() {
$themes = array();
$dir opendir_THEMEPATH );
if ($dir) { 
while (false !== ($file readdir($dir))) {
if (
$file != '.'  && 
$file != '..' && 
file_exists(_THEMEPATH $file '/styles.php')
) {
$themes[strtolower($file)] = $file
}
}
}
return $themes;
}

function 
themeSelect() {
$themes getThemes();
echo '<select name="theme-picker">';
foreach( $themes as $k => $v ) {
$selected s('theme') == $k 
' selected="selected"' 
'';
echo '<option '.$selected.' value="'.$k.'">'.$v.'</option>';
}
echo '</select>';
}

?>


Untested and unapproved, but should work.  I modified a function from this mod: http://snewscms.com/forum/index.php?topic=8725.0 - so it should work
Logged

Fred K

  • Still trying to learn stuff
  • ULTIMATE member
  • ******
  • Karma: 130
  • Posts: 2728
    • Personal
Re: [PHP] Array hum-hum
« Reply #6 on: September 10, 2011, 09:01:09 PM »

/*slaps forehead in exasperation*/ D'oh - of course. I was thinking of processing() yesterday when fiddling with my own code but didn't stop to make a mental note about it. Silly me.

Funny thing though - position seems to be everything. Tried the selectTheme code without the autodetect, in various places (in config, in snews.php just before settings(), in separate file included through index.php) and it didn't work. On the verge of pulling hair out I put the autodetect in (separate php file) and at first it didn't work at all but as a last measure I tried putting the include at the top of snews.php, directly after session_start and include(config) ... and it works!
Works beatifully in fact and should be published as a mod on its own imo. Just need to use
Code: [Select]
$theme = s('theme');wherever theme folder name should appear. In my case I have it directly before the index.php <head> part. Example of use:

Code: [Select]
<?php $theme s('theme'); // Pulls theme folder name from settings db
echo '<head> ...
<link rel="stylesheet" type="text/css" href="path-to-themes/'
.$theme.'/styles.php" />
... </head>'
;
?>

Perhaps also underline that the main stylesheet should always have the same name (i.e "styles.css") since it's referenced in getThemes().

And it goes without saying that I'm overwhelmingly grateful for this. First-hand copies of all my new premium themes coming your way as they get done.
+ a bunch of karma points. ;)
« Last Edit: September 10, 2011, 09:05:11 PM by Fred K »
Logged

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: [PHP] Array hum-hum
« Reply #7 on: September 11, 2011, 01:42:43 AM »

NP and thanks!
Logged