1

I've received this data from an app. My parameter list:

  1. catids = 1, 2, 3
  2. subscriptions = 1, 1, 0 [ 0 = keep subscribed and 1 = unsubscribe for catids in this order]
  3. appuser_id = XXX

Now I have to update my subscription table according to data received. The schema is as follows:

--cat_id--appuser_id
  1     |     10    |
  2     |     10    |
  3     |     10    |   
  4     |     20    |
  5     |     20    |
---------------------

So how can I do this neatly considering query performance? The approach I could think of is:

  1. Get all subscriptions whose value is 1.
  2. Find out respective catids for values found in step 1.
  3. Delete those entries from table.

I can reach up to step 1 only. I can't think of a way to match entries found in step 1 with their respective catid.

$cat = '1,2,3';
$subs = '1,1,0';

$catA = explode(',',$cat);
$subsA = explode(',',$subs);

$deleteSubList = array();
foreach($subsA as $v){
    if($v == 1){
        array_push($deleteSubList,$v);
    }
}

print_r($deleteSubList);

This gives me Array ( [0] => 1 [1] => 1 ). How do I know that it belongs to one of the categories from $catA array?

3 Answers 3

1

You need to remember your keys:

foreach($subsA as $key => $v){
        if($v == 1){
            $deleteSublist[$key] = $v;
        }
    }

Then $deleteSublist will be an array which has as keys all the ids of your array.

foreach($deleteSublist as $key => $value) {
   // Delete $cat[$key]
}
Sign up to request clarification or add additional context in comments.

6 Comments

ahh.. that easy! thanks man. I got the 1 and 2 catid out of $catA array now. That's what I needed.
another question, how to delete both of the records now? I don't think doing it in loop is best way since length of catA array can be anything. any help regarding that?
I don't believe this will work. You're just getting the key of subsA which he's not looking for. He wanted the category id (catA). Check my solution down below.
@Gimali One option is to use $deleteSublist[$key] = $cat[$key]; so you already have all the values you want to delete in your array. Then use that array for a mysql query
@Gimali I'd recommend you checking my answer. This solution will not work for you.
|
1
$cat = '1,3,3';
$subs = '1,1,0';

$catA = explode(',', $cat);
$subsA = explode(',', $subs);

for ($i = 0; $i <= count($subsA); $i++) {
    if ($subsA[$i] == 1) {
        $deleteSubList[$catA[$i]] = $subsA[$i];
    }
}
print_r($deleteSubList);

And to remove:

foreach($deleteSubList as $key => $value) {
    mysql_query("DELETE FROM tbl WHERE cat_id = " . $key);
}

2 Comments

I tried your code with following data $cat = '1,2,3,'; $subs = '1,1,0'; and I got Array ( [0] => 1 [1] => 1 [2] => 1 ) as output. Now how will I come to know which category to delete out of this?
Sorry, I had to accept @Galen answer as it's nearly best and quickest way both in terms of finding catids and forming the query avoiding it to run in loops. Thank you very much for support.
0

Here's an easier way to do it

$deleteSubList = array_filter( array_combine( $catA, $subsA ) );

$sql = sprintf( "delete from table where cat_id in(%s) and appuser_id=%s", implode( ',' array_keys( $deleteSubList ) ), $appuser_id );

2 Comments

Wonderful! I tried your code, for $cat = '1,2,3'; $subs = '1,1,0'; and it gave me this sql, delete from table where cat_id in(12) and appuser_id=10 . Instead it should be like - delete from table where cat_id in(1,2) and appuser_id=10 right ? How to get this now ? I'm all set to accept this answer now.
yea.. changed implode(array_keys( $deleteSubList ) ) to implode( ',', array_keys( $deleteSubList ) ) to get correct query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.