Antonella Posted April 26, 2007 Share Posted April 26, 2007 Hi! I have a little problem with a script which should manage the themes of my site. The script is meant to retrieve the info of the layouts from a mysql table, write a cookie and check if the cookie exists. If it exists it must use the layout from the cookie, if it doesn't exist it must use a default layout. So far so good, but... when I select a new layout, nothing happens until I refresh the page and sometimes, if no cookie is set yet you get no layout until you refresh! this is the code: include_once("db_theme.php"); $num_temi=mysql_query ("SELECT * FROM $temi"); $total_skins=mysql_num_rows($num_temi); /*$query="SELECT * FROM $temi WHERE predefinito='1'"; $result=mysql_query($query); if($row=mysql_fetch_array($result)){ $default_skin= $row['nr_tema'] ;} else {?> non è stato definito alcun tema di default <? }*/ $skin_cookie=$_COOKIE['skin']; $new_skin=mysql_query("SELECT * FROM $temi WHERE nr_tema='$skin_cookie'"); $skin_request=$_REQUEST['newskin']; $new_skin_request=mysql_query("SELECT * FROM $temi WHERE nr_tema='$skin_request'"); if (isset($_REQUEST['newskin'])) { $newskin=(int)$_REQUEST['newskin']; if (mysql_num_rows($new_skin_request) <= 0) $newskin=$default_skin; } elseif (isset($_COOKIE['skin'])) { $newskin=(int)$skin; if (mysql_num_rows($new_skin) <= 0) $newskin=$default_skin; } else $newskin=$default_skin; $skin=$newskin; setcookie ($cookie, "", time() - 3600); setcookie($cookie,$newskin,time()+(86400*365),$dir,$path); setcookie($cookie,$newskin,time()+(86400*365),$dir,$path); $skin=$newskin; $mytheme=mysql_query("SELECT * FROM $temi WHERE nr_tema='$newskin'"); if($theme=mysql_fetch_array($mytheme)){ $tema=$theme['header']; $footer=$theme['footer'];} else {?> errore <? } $num_temi=mysql_query ("SELECT * FROM $temi"); $total_skins=mysql_num_rows($num_temi); /*$query="SELECT * FROM $temi WHERE predefinito='1'"; $result=mysql_query($query); if($row=mysql_fetch_array($result)){ $default_skin= $row['nr_tema'] ;} else {?> non è stato definito alcun tema di default <? }*/ if (isset($_REQUEST['newskin'])) { $newskin=(int)$_REQUEST['newskin']; if (mysql_num_rows($new_skin_request) <= 0) $newskin=$default_skin; } elseif (isset($_COOKIE['skin'])) { $newskin=(int)$skin; if (mysql_num_rows($new_skin) <= 0) $newskin=$default_skin; } else $newskin=$default_skin; $skin=$newskin; setcookie ($cookie, "", time() - 3600); setcookie($cookie,$newskin,time()+(86400*365),$dir,$path); setcookie($cookie,$newskin,time()+(86400*365),$dir,$path); $skin=$newskin; $mytheme=mysql_query("SELECT * FROM $temi WHERE nr_tema='$newskin'"); if($theme=mysql_fetch_array($mytheme)){ $tema=$theme['header']; $footer=$theme['footer'];} else {?> errore <? } ?> and this is the table `id` int(11) NOT NULL auto_increment, `nr_tema` int(11) NOT NULL default '0', `nome` varchar(255) default NULL, `descrizione` text, `info` text, `header` varchar(255) NOT NULL default '', `footer` varchar(255) NOT NULL default '', `predefinito` tinyint(1) NOT NULL default '0', `cap_small` varchar(255) default NULL, `cap_big` varchar(255) default NULL, `date_added` int(11) NOT NULL default '0', PRIMARY KEY (`id`) what I need is that the layout shows immediately this is the site http://www.nakuru-club.com/index.php?newskin=3 (here in the box "skins" you have the possibility to switch layouts, in the "current layout there isn't this possibility, but you can do it by adding ?newskin=3, and ?newskin=1 for the "green" layout which is the current default one) thanks for any help. Quote Link to comment Share on other sites More sharing options...
kalivos Posted April 27, 2007 Share Posted April 27, 2007 Try it out with sessions instead of cookies. Quote Link to comment Share on other sites More sharing options...
btherl Posted April 27, 2007 Share Posted April 27, 2007 You have a lot of repeated code there.. for example, you call setcookie() 3 times for the same cookie. And the entire script is repeated twice. As for debugging it, I suggest printing out variables throughout the script, such as the values of $newskin, $_COOKIE and $_REQUEST Quote Link to comment Share on other sites More sharing options...
Antonella Posted April 27, 2007 Author Share Posted April 27, 2007 Thanks for the answers Yes I know the code is repeated twice, but for some reason if you write it only once nothing works :/ I've tried printing out the variables, they work correctly. I really can't figure out the mistake I'm making. I'll try again deleting the repeated part of the code however . As to the sessions, I need cookies because I want the chosen layout to be set "permanently", so that the user doesn't have to choose a layout every time they visit the site. Quote Link to comment Share on other sites More sharing options...
Antonella Posted April 27, 2007 Author Share Posted April 27, 2007 I've changed the code, but the result is the same. You call a skin, the page refreshes, but the layout remains the same until you refresh again, and if you have no cookie set you get the page without any layout until you refresh. ??? here is tthe code <?php include('db_theme.php'); //echo("$dir_c $path_c" ); $predefinito_query=mysql_query("SELECT * FROM $temi WHERE predefinito='1'"); $row_predefinito=mysql_fetch_assoc($predefinito_query); $num_temi=mysql_query("SELECT * FROM $temi WHERE attivo='1'"); $total_skins = mysql_num_rows($num_temi); $default_skin = $row_predefinito['nr_tema']; $temi_attivi=mysql_fetch_assoc($num_temi); if (isset($_REQUEST['newskin'])) { $new_skin=$_REQUEST['newskin']; $skin_query=mysql_query("SELECT * FROM $temi WHERE nr_tema=$new_skin"); $skin_request=mysql_fetch_assoc($skin_query); if ($skin_request['attivo'] == '1') { $newskin = $skin_request['nr_tema']; } else { $newskin = $default_skin; } } elseif (isset($_COOKIE['skin'])) { $cookie_skin=$_COOKIE['skin']; $cookie_request=mysql_query("SELECT * FROM $temi WHERE nr_tema=$cookie_skin"); $cookieskin=mysql_fetch_assoc($cookie_request); if ($cookieskin['attivo'] == '1') { $newskin = $cookieskin['nr_tema']; } else { $newskin = $default_skin; } } elseif (!isset($_COOKIE['skin'])|| ($_REQUEST['newskin'])) { $newskin = $default_skin; } setcookie ('skin', "", time() - 3600); setcookie('skin',$newskin,time()+(86400*365),"$dir_c"); setcookie('skin',$newskin,time()+(86400*365),"$dir_c","$path_c"); $mytheme=mysql_query("SELECT * FROM $temi WHERE nr_tema='$skin'"); if($theme=mysql_fetch_array($mytheme)){ $tema=$theme['header']; $footer=$theme['footer'];} else {?> <br> <br> If the skin you've chosen doesn't show try to refresh the page by hitting F5 on your keyboard or the Refresh button on your browser. If the problem persists please contact me. <? } ?> It's the first time I use cookies, I probably don't know how to use them in the right way. I've checked the cookie too: is it normal that it shows Saturday April 26th as an expiry date? Thanks for any help Quote Link to comment Share on other sites More sharing options...
Antonella Posted April 27, 2007 Author Share Posted April 27, 2007 Anyone? the problem,briefly, is that you have to refresh twice, before the right layout is loaded. Is it possible that the firstly the script writes the cookie and on the second refresh it reads the cookie and displays the layout? ??? 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.