Jump to content

Archived

This topic is now archived and is closed to further replies.

education

Really Really Need Your Help.. Please Please.

Recommended Posts

Hi im new to php and got a style changing script
One file s3.cfg.php has this code

[code]<?php
// vim:et ts=4 sw=4 cindent:

    $style_path = '';

    $available_styles = array(
        'red',
        'green',
        'blue'
    );

    $default_referrer = '/';

?>[/code]


The other file is called s3.php this has the following code:


[code]<?php
// vim:et ts=4 sw=4 cindent:

if (!@include('s3.cfg.php'))
    die("Unable to include config file!");

$set_cookie = true;

if (isset($_GET['style'])) {

    $style = $_GET['style'];

} elseif (isset($_POST['style'])) {

    $style = $_POST['style'];

} elseif (isset($_COOKIE['style'])) {

    $style = $_COOKIE['style'];
    $set_cookie = false;

}

// if no style supplied, or invalid supplied...
if (!isset($style)
    || !in_array($style, $available_styles)) {
    // set style to default (first available)
    $style = $available_styles[0];
}

// our cookie will last 2 years... modify
// the last argument if this isn't sufficient
if ($set_cookie)
    setcookie('style', $style,
              mktime() + 2 * 356 * 24 * 60 * 60,
              '/', '.alterior.net');

$referrer = (!empty($_SERVER['HTTP_REFERER']))
    ? $_SERVER['HTTP_REFERER']
    : $default_referrer;

header('Location: ' . $referrer);

?>[/code]


Finally this code is included in my pages in the head section   

[code]<?php

    $path = substr($_SERVER['PATH_TRANSLATED'], 0,
                   -strlen($_SERVER['SCRIPT_NAME']));

    if (@include($path . '/s3.cfg.php')) {

        $style = (isset($_COOKIE['style']))
            ? $_COOKIE['style']
            : $available_styles[0];

        printf('<style type="text/css" media="screen">@import url(%s%s.css);</style>',
               $style_path, $style);
    }

?>
[/code]
and the styles are changed with a form
[code]
  <form action="s3.php" method="get">
            <p><label for="style">Style:</label>
                <select id="style" name="style">
                <?php
                    foreach ($available_styles as $_style) {
                        printf('<option value="%s"%s>%s</option>',
                               $_style,
                               ($_style == $style)
                                   ? ' selected="selected"'
                                   : '',
                               ucfirst($_style));
                    }
                ?>
                </select>
                <input type="submit" value="switch" />
            </p>
        </form>
[/code]

all i want to do is change selecting the styles from a drop down box to a hyperlink i.e click a red square image for a red style.


PLEASE PLEASE help this is driving me nuts i've tried every little bit of code to no availe.

Share this post


Link to post
Share on other sites
Well what happens?

Also, your action is to s3.php but you say the file is s3.cfg.php - you need to make either the action the same as the file name, or the file the same as the action.

Also, can you edit your post and use the [code ][/code ] tags to highlight your php?

Share this post


Link to post
Share on other sites
s3.cfg.php and s3.php are too seperate files, the actual code works great no problems what so ever but the script generates a drop down box ie blue, red, green etc when you select a colour you have to click submit to get the script to change.
What i want to do is have a image of a red square that when the user clicks it changes to red style
i.e
<a href="#" img src="redsquare.jpg">
I really need to do this as am designing a site for work its done apart from this, it the style switch gives user accessibility features this is why its important.

Share this post


Link to post
Share on other sites
Oh i see:
[code]
<?php
                    foreach ($available_styles as $_style) {
echo "<a href='s3.php?style=$_style'><img src='$_style.jpg' /></a>";
}
?>
[/code]
then have a file clled red.jpg, blue.jpg etc

I think that should work

Share this post


Link to post
Share on other sites
Errm. Im lost right my form
[code]
        <form action="s3.php" method="get">
            <p><label for="style">Style:</label>
                <select id="style" name="style">
                <?php
                    foreach ($available_styles as $_style) {
                        printf('<option value="%s"%s>%s</option>',
                               $_style,
                               ($_style == $style)
                                   ? ' selected="selected"'
                                   : '',
                               ucfirst($_style));
                    }

                ?>
                </select>
                <input type="submit" value="switch" />
            </p>
        </form>
[/code]


when you said try

[code]
<?php
                    foreach ($available_styles as $_style) {
echo "<a href='s3.php?style=$_style'><img src='$_style.jpg' /></a>";
}
?>
[/code]

where do i insert that in the form and how do i get the individual coloured cubes to pass that color to the switcher. ie do i write the following:--

[code]
<?php
                    foreach ($available_styles as $_style) {
echo "<a href='s3.php?style=$_style'><img src='$_style.jpg' /><img src="red.jpg></a>";
}
?>

???
Im sorry im learning more about php each week but im stumped, i really do apreciate your patience with my newbie ness
[/code]

Share this post


Link to post
Share on other sites
Well your method in your form was get, so it seems safe to pass the color through the url, this bit:
?style=$_style;
So, you wont actually need your form, put that in place of the the form.

Share this post


Link to post
Share on other sites
right replaced it with that bit of code but it generates.
[quote]
Warning: Invalid argument supplied for foreach() in index.php on line 46
[/quote]

?

Share this post


Link to post
Share on other sites
Well the only thing i can assume is that you're not including your s3.cfg.php file, because i guess it cant find the array...
add:
print_r($available_styles);
on the fine before the foreach.

Share this post


Link to post
Share on other sites
Gonna give up with that one as it wont work no matter what i do.
However i have another way of doing it with one small problem.
I have a file styleswitch.js the code is:-
[code]
//Style Sheet Switcher version 1.0 Nov 9th, 2005
//Author: Dynamic Drive: http://www.dynamicdrive.com
//Usage terms: http://www.dynamicdrive.com/notice.htm

function getCookie(Name) {
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
return null
}

function setCookie(name, value, days) {
var expireDate = new Date()
//set "expstring" to either future or past date, to set or delete cookie, respectively
var expstring=(typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5)
document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
}

function deleteCookie(name){
setCookie(name, "moot")
}

function setStylesheet(title) {
var i, cacheobj
for(i=0; (cacheobj=document.getElementsByTagName("link")[i]); i++) {
if(cacheobj.getAttribute("rel").indexOf("style") != -1 && cacheobj.getAttribute("title")) {
cacheobj.disabled = true
if(cacheobj.getAttribute("title") == title)
cacheobj.disabled = false //enable chosen style sheet
}
}
}

function chooseStyle(styletitle, days){
if (document.getElementById){
setStylesheet(styletitle)
setCookie("mysheet", styletitle, days)
}
}

function indicateSelected(element){ //Optional function that shows which style sheet is currently selected within group of radio buttons or select menu
var i
if (selectedtitle!=null && (element.type==undefined || element.type=="select-one")){ //if element is a radio button or select menu
var element=(element.type=="select-one") ? element.options : element
for (i=0; i<element.length; i++){
if (element[i].value==selectedtitle){ //if match found between form element value and cookie value
if (element[i].tagName=="OPTION") //if this is a select menu
element[i].selected=true
else //else if it's a radio button
element[i].checked=true
break
}
}
}
}

var selectedtitle=getCookie("mysheet")
if (document.getElementById && selectedtitle!=null) //load user chosen style sheet if there is one stored
setStylesheet(selectedtitle)
[/code]

at the top of every page i have the following:-
[code]

<link rel="alternate stylesheet" type="text/css" media="screen" title="default-theme" href="http://www.mydomain.com/style/style.css" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="green-theme" href="http://www.mydomain.com/style/green.css" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="red-theme" href="http://www.mydomain.com/style/red.css" />
<script type="text/JavaScript" src="http://www.www.mydomain.com/scripts/styleswitch.js">
</script>

[/code]

and to change the style all i do is have

[code]
<a href="javascript:chooseStyle('default-theme', 60)"><img src="http://www.mydomain.com/images/blue_cube.gif" href="javascript:chooseStyle('default', 60)" width="12" height="12" class="cube_color"></a>
<a href="javascript:chooseStyle('green-theme', 60)"><img src="http://www.mydomain.com/images/green_cube.gif" href="javascript:chooseStyle('green', 60)" width="12" height="12" class="cube_color"></a>
<a href="javascript:chooseStyle('red-theme', 60)"><img src="http://www.mydomain.com/images/red_cube.gif" href="javascript:chooseStyle('red', 60)" width="12" height="12" class="cube_color"></a>
[/code]

on the page as well, the problem is when a user changes style on the first page the switch is perfect and when you go to another subdomain the style is remembered, however if the user changes style on this page then returns to the homepage the style is not remembered any ideas you have been a great help.

Share this post


Link to post
Share on other sites

×

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.