Jump to content


Photo

Really Really Need Your Help.. Please Please.


  • Please log in to reply
11 replies to this topic

#1 education

education
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 17 July 2006 - 09:53 AM

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

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

    $style_path = '';

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

    $default_referrer = '/';

?>


The other file is called s3.php this has the following 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);

?>


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

<?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);
    }

?>
and the styles are changed with a form
  <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>

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.
Emotions="inactive"

#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 July 2006 - 09:55 AM

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?

#3 education

education
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 17 July 2006 - 10:00 AM

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.
Emotions="inactive"

#4 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 July 2006 - 10:06 AM

Oh i see:
<?php
                    foreach ($available_styles as $_style) {
echo "<a href='s3.php?style=$_style'><img src='$_style.jpg' /></a>";
}
?>
then have a file clled red.jpg, blue.jpg etc

I think that should work

#5 education

education
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 17 July 2006 - 10:09 AM

Gonna give it ago, if it works im gonna kiss ya!
Emotions="inactive"

#6 education

education
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 17 July 2006 - 10:21 AM

Errm. Im lost right my form
        <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>


when you said try

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

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

<?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

Emotions="inactive"

#7 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 July 2006 - 10:23 AM

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.

#8 education

education
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 17 July 2006 - 10:26 AM

right replaced it with that bit of code but it generates.

Warning: Invalid argument supplied for foreach() in index.php on line 46


?
Emotions="inactive"

#9 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 July 2006 - 10:31 AM

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.

#10 education

education
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 17 July 2006 - 10:47 AM

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

at the top of every page i have the following:-

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


and to change the style all i do is have

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

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.
Emotions="inactive"

#11 education

education
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 17 July 2006 - 11:08 AM

Any ideas guys, i have to leave work in 15mins and hope i can resolve this before i go.
Emotions="inactive"

#12 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 July 2006 - 11:13 AM

I dont really do javascript, sorry




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users