Jump to content

Problem in if condition


bugzy

Recommended Posts

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

Link to comment
Share on other sites

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? 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.