bugzy Posted July 14, 2012 Share Posted July 14, 2012 Hello guys. I have problem on my first condition because the code seemed like it's not working :'( <?php if($cat_position == 0 AND $cat_visibility == 0) { $update_query1 = "Update category set cat_position=NULL where cat_id = {$edit_cat_id}"; $update_result1 = mysql_query($update_query1,$connection); $update_query2 = "Update category set cat_position=cat_position-1 where cat_position > {$edit_cat_position} AND cat_position < {$cat_position}"; $update_result2 = mysql_query($update_query2,$connection); } else if($cat_position < $edit_cat_position) { $update_query1 = "Update category set cat_position=NULL where cat_id = {$edit_cat_id}"; $update_result1 = mysql_query($update_query1,$connection); $update_query2 = "Update category set cat_position=cat_position+1 where cat_position >= {$cat_position} AND cat_position < {$edit_cat_position}"; $update_result2 = mysql_query($update_query2,$connection); } else { $update_query1 = "Update category set cat_position=NULL where cat_id = {$edit_cat_id}"; $update_result1 = mysql_query($update_query1,$connection); $update_query2 = "Update category set cat_position=cat_position-1 where cat_position > {$edit_cat_position} AND cat_position <= {$cat_position}"; $update_result2 = mysql_query($update_query2,$connection); } ?> That if statement is for category positioning.. The 1st if statement is supposed to decrement all the categories which are greater than the current category position. So let's say.. Value Name | Category Position Home | 1 About Us | 2 Contact US | 3 Terms | 4 Promos| 5 The if($cat_position == 0 AND $cat_visibility == 0) is if the user decided to hide the category. It's a boolean so 0 and 1 is the value. So let's say I hide "About Us" category, this is suppose to happen next Home | 1 Contact US | 2 Terms | 3 Promos| 4 I wonder why it is not working? Quote Link to comment https://forums.phpfreaks.com/topic/265681-problem-in-if-condition/ Share on other sites More sharing options...
gizmola Posted July 14, 2012 Share Posted July 14, 2012 Let's look at the 2nd query in your first condition: $update_query2 = "Update category set cat_position=cat_position-1 where cat_position > {$edit_cat_position} AND cat_position If you only get into this when $cat_position == 0, then that statement is never going to update any rows, is it? Quote Link to comment https://forums.phpfreaks.com/topic/265681-problem-in-if-condition/#findComment-1361544 Share on other sites More sharing options...
bugzy Posted July 14, 2012 Author Share Posted July 14, 2012 Let's look at the 2nd query in your first condition: $update_query2 = "Update category set cat_position=cat_position-1 where cat_position > {$edit_cat_position} AND cat_position < {$cat_position}"; If you only get into this when $cat_position == 0, then that statement is never going to update any rows, is it? Hey yup. That 2nd query is supposed to execute if $cat_position == 0 base on my conditions, it is supposed to update the rows or am I missing something? Quote Link to comment https://forums.phpfreaks.com/topic/265681-problem-in-if-condition/#findComment-1361545 Share on other sites More sharing options...
bugzy Posted July 14, 2012 Author Share Posted July 14, 2012 Got it my condition is wrong it is suppose to be > $cat_position and not < Thanks for giving me an idea Quote Link to comment https://forums.phpfreaks.com/topic/265681-problem-in-if-condition/#findComment-1361548 Share on other sites More sharing options...
gizmola Posted July 14, 2012 Share Posted July 14, 2012 What rows would match WHERE cat_position > ($edit_cat_position) AND cat_position (some number > 0) AND also have a cat_position I think what you're trying to do here is very conrfusing, and will be prone to bounds issues, as you aren't checking for values less than 1. Inevitably you're going to shuffle all the values down to negatives, or if that is not possible with an unsigned column, then everything shuffling to 0. I think you'd be better off with an ordering routine that takes the result set of categories and numbers them rather than trying to produce a set of if-then-else conditions and queriies. In other words, unless you can produce something that is idempotent, you are guaranteed to have issues with the way you are approaching this. Quote Link to comment https://forums.phpfreaks.com/topic/265681-problem-in-if-condition/#findComment-1361549 Share on other sites More sharing options...
bugzy Posted July 14, 2012 Author Share Posted July 14, 2012 What rows would match WHERE cat_position > ($edit_cat_position) AND cat_position < ($cat_position) ... which we've already established is 0. In other words, what rows ever have a cat_postion < 0? See the problem? This query not only will never have rows that match < 0, but they also can't possibly have a cat_position > (some number > 0) AND also have a cat_position < 0. I think what you're trying to do here is very conrfusing, and will be prone to bounds issues, as you aren't checking for values less than 1. Inevitably you're going to shuffle all the values down to negatives, or if that is not possible with an unsigned column, then everything shuffling to 0. I think you'd be better off with an ordering routine that takes the result set of categories and numbers them rather than trying to produce a set of if-then-else conditions and queriies. In other words, unless you can produce something that is idempotent, you are guaranteed to have issues with the way you are approaching this. Thanks for this. My condition is wrong as I have said on my last post. I will try your suggestion bcoz this is really so confurisng and complicated. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/265681-problem-in-if-condition/#findComment-1361550 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.