1 (edited by scottywz 2005-06-15 20:46)

Topic: Change Style

Read me first

Okay, now that you've read that, let me say this:

I want to allow a user to change their preferred style via a drop-down box. When a user selects the style from the drop-down box (which is in a PHP file called styler.php in the root directory which is included into the main.tpl file) and hits "Change" (or whatever the Submit button says), it would set a cookie containing the preferred style. The header.php file would load the preferred style from the cookie instead of the user profile. If the style doesn't exist, or if no style is set, it would set a cookie containing the default style (which is already set in the forum's config). I'm not too good at PHP (yet, anyways), so any suggestions would be nice.

Edit: (Edited for clarity.)

Re: Change Style

Would that be for registered users and guests or only guests?
Once I get this question answered, I'll be able to make it.

Re: Change Style

Both.

4 (edited by ultime 2005-06-15 21:48)

Re: Change Style

Alright, so create a file, dunno, setstyle.php or something.. it will set up a cookie.
      <OPTION value="green">Style1</OPTION>
      <OPTION value="pink">Style2</OPTION>
set those options to whatever you want, add some what ever..

<?php
$action = isset($_GET['action']);

if (!$action) {

$action = "";

}

else {

$action = $_GET['action'];

}

if (!$action) {
?>
<FORM action="<?php print ($PHP_SELF); ?>?action=setstyle" method="post">
   <P>
   <SELECT name="styledrop">
      <OPTION value="green">Style1</OPTION>
      <OPTION value="pink">Style2</OPTION>
   </SELECT>
   <INPUT type="submit" value="Send">
   </P>
</FORM>
<?php
}

if ($action) {
$style = $_GET['styledrop'];
}

if ($action == 'setstyle') {
$now = time();
setcookie(style, $now + 31536000, $style); //expires in a year..
echo 'Thank you, your design has been chosen!';
}

if ($action && $action != 'setstyle') {
echo 'Invalid Action!';
}
?>

Then, in header.php:

<?php
//Default Style:
if (!$_COOKIE["style"]) {
$styletest = "black";
}
//If cookie is set, get information:
if ($_COOKIE["style"]) {
$styletest = $_COOKIE["style"];
}
?>

then well.. <style bla bla
<?php echo $styletest.'.css'; ?>
</style>


Now, it should work, haven't tested the script heavily though it should work. You might need to do a little modifications though.

Re: Change Style

Ok, thanks!

Re: Change Style

Ok, now let's say I have the names of each style in my DB. Instead of having

      <OPTION value="green">Style1</OPTION>
      <OPTION value="pink">Style2</OPTION>

in there static, I want it to be generated dynamically from the list of names.

Example:

swzc_styles
========name========
Green
Pink

(End DB Table row listing)

<option value="**name**">**name**</option>

where **name** is the contents of field name on the row in table swzc_styles. Please note I'm using MySQL.

Re: Change Style

Also, your HTML isn;t valid XHTML 1.0 strict: element names are case-sensitive (i.e. <option> and <OPTION> are 2 different things). Sorry about the triple post.

8 (edited by ultime 2005-06-15 22:57)

Re: Change Style

It's a simple database query..
Didn't test it, should work..

<FORM action="<?php print ($PHP_SELF); ?>?action=setstyle" method="post">
<SELECT name="styledrop">
<?php
$result = $db->query('SELECT * FROM '.$db->prefix.'table swzc_styles') or error('ERROR..', __FILE__, __LINE__, $db->error());

while ($styles = $db->fetch_assoc($result))
{
    echo '<option value="'.$styles['name'].'">'.$styles['name'].'</option>';
}
?>
  </SELECT>
<INPUT type="submit" value="Send">
</FORM>

Re: Change Style

scottywz wrote:

Also, your HTML isn;t valid XHTML 1.0 strict: element names are case-sensitive (i.e. <option> and <OPTION> are 2 different things). Sorry about the triple post.

I don't really care if the html is right or wrong, as long as it works smile

You can fix it if you want..

10 (edited by scottywz 2005-06-16 00:08)

Re: Change Style

my modded header.php

//Default Style:
if (!$_COOKIE["swzc_style"]) {
$styletest = "Brown";
}
//If cookie is set, get information:
if ($_COOKIE["swzc_style"]) {
$styletest = $_COOKIE["swzc_style"];
}

*********************the tpl loader script and then some other stuff is here*********************

<link rel="stylesheet" type="text/css" href="style/<?php echo $styletest.'.css'; ?>" />

that put this somewhere with the little sql thingy

<?php 

$action = isset($_GET['action']); 

if (!$action) { 

$action = ""; 

} 

else { 

$action = $_GET['action']; 

} 

if (!$action) { 
?> 
<FORM action="<?php print ($PHP_SELF); ?>?action=setstyle" method="post"> 
<SELECT name="styledrop"> 
<?php 
$result = $db->query('SELECT * FROM '.$db->prefix.'table swzc_styles') or error('ERROR..', __FILE__, __LINE__, $db->error()); 

while ($styles = $db->fetch_assoc($result)) 
{ 
    echo '<option value="'.$styles['name'].'">'.$styles['name'].'</option>'; 
} 
?> 
  </SELECT> 
<INPUT type="submit" value="Send"> 
</FORM> 

<?php 
} 

if ($action) { 
$style = $_GET['styledrop']; 
} 

if ($action == 'setstyle') { 
$now = time(); 
setcookie(swzc_style, $now + 31536000, $style); //expires in a year.. 
echo 'Thank you, your design has been chosen!'; 
} 

if ($action && $action != 'setstyle') { 
echo 'Invalid Action!'; 
} 
?>

Provided I have styler.php included in the main.tpl (and even if I don't), it spits out a blank page! What's wrong? And, yes, I do have a style called Brown on my installation.

11 (edited by scottywz 2005-06-16 20:37)

Re: Change Style

My gosh! The problem was with neither; it was with a LANGUAGE file, of all things! mad lol smile

I'm so sorry my site was down for over 12 hours over a LANGUAGE file. smile

12 (edited by scottywz 2005-06-21 02:06)

Re: Change Style

I got everything fixed, but there's one problem: When I try to change the style, it doesn't set a cookie. And, the page that sets the cookie (supposedly, anyway) redirectsa back to the HTTP_REFERER.

For example, I am on http://www.scottywz.com/scottywz.com/viewtopic.php?id=11 (my Links page). I click the Submit button under Change Style, and it (supposedly) sets the cookie and (certainly) redirects back to http://www.scottywz.com/scottywz.com/viewtopic.php?id=11. Neat, huh? smile

Re: Change Style

Any suggestions?

14

Re: Change Style

see this reply to your other thread... http://punbb.org/forums/viewtopic.php?id=7676&p=2

15

Re: Change Style

well in the second peice of code you posted in #10 you are geting the selected style from $_GET, a get is from url try $_POST

16 (edited by scottywz 2005-06-28 19:54)

Re: Change Style

YAY!

That worked! Better put it in the news.

Now, why do pages take so long to load after this big ordeal?

Re: Change Style

Now, I have:

<?php 

...

/* Start App */ 

$action = isset($_GET['setstyle']); 

if (!$action) { 

$action = ""; 

} 

else { 

$action = $_GET['setstyle']; 

} 

if (!$action) { 
?> 
<form action="<?php print ($PHP_SELF); ?>?setstyle=setstyle" method="post"> 
<div align="center"><ul><li><select name="styledrop" style="width: 90%"> 
<?php 
$result = $db->query('SELECT * FROM '.$db->prefix.'styles') or error('ERROR..', __FILE__, __LINE__, $db->error()); 

echo '<option value="'.$styletest.'">Current ('.$styletest.')</option>'; 
echo '<optgroup label="Select a Style" />'; 
  echo '<option value="'.pun_htmlspecialchars($pun_config['o_default_style']).'">Default ('.pun_htmlspecialchars($pun_config['o_default_style']).')</option>'; 
/* while ($styles = $db->fetch_assoc($result)) 
{ 
    echo '<option value="'.$styles['name'].'">'.$styles['name'].'</option>'; 
} 
*/ 

        $styles = array(); 
        $d = dir(PUN_ROOT.'style'); 
        while (($entry = $d->read()) !== false) 
        { 
            if (substr($entry, strlen($entry)-4) == '.css') 
                $styles[] = substr($entry, 0, strlen($entry)-4); 
        } 
        $d->close(); 

            while (list(, $temp) = @each($styles)) 
            { 

                echo "\t\t\t\t\t\t\t\t".'<option value="'.$temp.'">'.str_replace('_', ' ', $temp).'</option>'."\n"; 
            } 

?> 
                            </select> 

  </select></li></ul> 
<ul><li><input type="submit" value="Submit"><!-- <input type="submit" value="Preview"> --></li></ul></div> 
</form> 

<?php 
} 

if ($action) { 
$style = $_POST['styledrop']; 
} 

if ($action == 'setstyle') { 
$now = time(); 
setcookie($style_cookie_name, $_POST['styledrop'], time()+60*60*24*31536000, $cookie_path, $cookie_domain, $cookie_secure); // When does it expire? 
// header("Location: http://www.scottywz.com/scottywz.com/index.php") // redirect 
header("Location: ".$_SERVER['HTTP_REFERER']); // redirect 
} 

if ($action && $action != 'setstyle') { 
echo 'Invalid Action!'; 
} 

/* End App */ 

?>

(The ... part wasn't anything but a giant comment.) Now, I want to know how I can sort the styles alphabetically. I changed it from taking the styles from the database to taking them from the filenames.

18

Re: Change Style

add sort($styles)  right befor the while

19 (edited by scottywz 2005-06-28 23:07)

Re: Change Style

That worked, except that "phpBB" comes after "Wintergreen". Can I get "phpBB" under "Oxygen" instead?

Edit: Oh, and I modded so that a user can change the style for all 3 PunBB-powered aspects of my site (main site, forums, and another thing) all at once.