patawic Posted May 2, 2010 Share Posted May 2, 2010 function defaultstyle($id) { $theme = mysql_query("SELECT * FROM themes WHERE id = '".$id."'"); $count = mysql_num_rows(mysql_query("SELECT * FROM themes")); for($i=0;$i<$count;$i++){ mysql_query("UPDATE themes SET enabled = '1' WHERE id = '".$i."'"); } while($style = mysql_fetch_array($theme)) { if($style[enabled] == '1') { echo "<center>This theme is already set to default</center>"; } else { echo "<center>The theme $style[name] is now the default theme for the CMS</center>"; mysql_query("UPDATE themes SET enabled = '1' WHERE id = '".$id."'"); } } } The updating part where it set's enabled to "1" works fine, but the code before it for($i=0;$i<$count;$i++){ mysql_query("UPDATE themes SET enabled = '1' WHERE id = '".$i."'"); } That code is meant to load every mysql record and set the "enabled" row to 0, but its not doing that. How can i fix this? Quote Link to comment Share on other sites More sharing options...
patawic Posted May 2, 2010 Author Share Posted May 2, 2010 wait nevermind, stupid mistake for($i=0;$i<$count;$i++){ mysql_query("UPDATE themes SET enabled = '1' WHERE id = '".$i."'"); } should of been for($i=0;$i<$count;$i++){ mysql_query("UPDATE themes SET enabled = '0' WHERE id = '".$i."'"); } Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 2, 2010 Share Posted May 2, 2010 The updating part where it set's enabled to "1" works fine, but the code before it for($i=0;$i<$count;$i++){ mysql_query("UPDATE themes SET enabled = '1' WHERE id = '".$i."'"); } That code is meant to load every mysql record and set the "enabled" row to 0, but its not doing that. How can i fix this? By learning how to do it the right way. There is absolutely no need to do a loop. Besides, that's a pretty lame attempt at trying to update all the records since it would be possible for an id number to be skipped if you were ever to delete a record. And, that just might be your problem. To update every record, simply leave off the WHERE clause UPDATE themes SET enabled = '0' Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 2, 2010 Share Posted May 2, 2010 Persoanlly, I think determining if the style has already been seleted previously has no value. Just state that the selected ID has been set. But, in any event, here is a better way to do what you are after function defaultstyle($id) { //Set new style to enabled mysql_query("UPDATE themes SET enabled = '1' WHERE id = '{$id}'"); //Set all others to disabled mysql_query("UPDATE themes SET enabled = '0' WHERE id <> '{$id}'"); //Determine if the new id was already set if(mysql_affected_rows()==0) { echo "<center>This theme is already set to default</center>"; } else { echo "<center>The theme $style[name] is now the default theme for the CMS</center>"; } } Quote Link to comment Share on other sites More sharing options...
patawic Posted May 2, 2010 Author Share Posted May 2, 2010 The updating part where it set's enabled to "1" works fine, but the code before it for($i=0;$i<$count;$i++){ mysql_query("UPDATE themes SET enabled = '1' WHERE id = '".$i."'"); } That code is meant to load every mysql record and set the "enabled" row to 0, but its not doing that. How can i fix this? By learning how to do it the right way. There is absolutely no need to do a loop. Besides, that's a pretty lame attempt at trying to update all the records since it would be possible for an id number to be skipped if you were ever to delete a record. And, that just might be your problem. To update every record, simply leave off the WHERE clause UPDATE themes SET enabled = '0' doing that didnt work. it didnt update the record. Quote Link to comment Share on other sites More sharing options...
ignace Posted May 2, 2010 Share Posted May 2, 2010 Something else you may want to consider is some sort of config table, like: configs (.., theme_id, ..) This table holds references to the default theme, template, and holds values for other configuration data. Quote Link to comment Share on other sites More sharing options...
ignace Posted May 2, 2010 Share Posted May 2, 2010 UPDATE themes SET enabled = '0' doing that didnt work. it didnt update the record. Are you sure? Have you tried it? As we all know that it does work. 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.