education Posted July 17, 2006 Share Posted July 17, 2006 Hi im new to php and got a style changing scriptOne 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 sufficientif ($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. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 17, 2006 Share Posted July 17, 2006 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? Quote Link to comment Share on other sites More sharing options...
education Posted July 17, 2006 Author Share Posted July 17, 2006 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 stylei.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. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 17, 2006 Share Posted July 17, 2006 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 etcI think that should work Quote Link to comment Share on other sites More sharing options...
education Posted July 17, 2006 Author Share Posted July 17, 2006 Gonna give it ago, if it works im gonna kiss ya! Quote Link to comment Share on other sites More sharing options...
education Posted July 17, 2006 Author Share Posted July 17, 2006 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] Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 17, 2006 Share Posted July 17, 2006 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. Quote Link to comment Share on other sites More sharing options...
education Posted July 17, 2006 Author Share Posted July 17, 2006 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]? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 17, 2006 Share Posted July 17, 2006 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. Quote Link to comment Share on other sites More sharing options...
education Posted July 17, 2006 Author Share Posted July 17, 2006 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.htmfunction getCookie(Name) { var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pairif (document.cookie.match(re)) //if cookie foundreturn document.cookie.match(re)[0].split("=")[1] //return its valuereturn null}function setCookie(name, value, days) {var expireDate = new Date()//set "expstring" to either future or past date, to set or delete cookie, respectivelyvar 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, cacheobjfor(i=0; (cacheobj=document.getElementsByTagName("link")[i]); i++) {if(cacheobj.getAttribute("rel").indexOf("style") != -1 && cacheobj.getAttribute("title")) {cacheobj.disabled = trueif(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 iif (selectedtitle!=null && (element.type==undefined || element.type=="select-one")){ //if element is a radio button or select menuvar element=(element.type=="select-one") ? element.options : elementfor (i=0; i<element.length; i++){if (element[i].value==selectedtitle){ //if match found between form element value and cookie valueif (element[i].tagName=="OPTION") //if this is a select menuelement[i].selected=trueelse //else if it's a radio buttonelement[i].checked=truebreak}}}}var selectedtitle=getCookie("mysheet")if (document.getElementById && selectedtitle!=null) //load user chosen style sheet if there is one storedsetStylesheet(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. Quote Link to comment Share on other sites More sharing options...
education Posted July 17, 2006 Author Share Posted July 17, 2006 Any ideas guys, i have to leave work in 15mins and hope i can resolve this before i go. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 17, 2006 Share Posted July 17, 2006 I dont really do javascript, sorry Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.