Jump to content

Recommended Posts

Hi,

 

On my website, users will subscribe to "groups".  And each group has their own "News"

 

Is there anyway to make a good SELECT statement for this?

 

For example: I'm subscribed to groups 44,22,41, and 66

 

I want to have the news of all 4 groups.  Is there any easy method to do this with PHP+mysql?

 

SELECT * FROM news WHERE group = 44 AND group = 22 AND group = 41...is this the only way? (do i generate this Query through PHP)?

 

:) thanks!

Link to comment
https://forums.phpfreaks.com/topic/73164-solved-is-there-a-good-way-to-do-this/
Share on other sites

Could do something like:

 

$sql = "SELECT * FROM `users_groups` WHERE `user_id`='1234'";
$res = mysql_query($sql) or die(mysql_error());
$num = mysql_num_rows($res);

$x=1;
while($row = mysql_fetch_assoc($res)){
   if($x == $num){
   $c = "";
   }else {
   $c = ",";
   }
$group .= "'" . $row['group_id'] . "'" . $c;
}

$sql2 = "SELECT * FROM `news` WHERE `group` IN($group)";
$res2 = mysql_query($sql2) or die(mysql_error());

Or better still...

 

<?php

  $sql = "SELECT group FROM users_groups WHERE user_id = '1234'";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($res)) {
      while ($row = mysql_fetch_assoc($res)) {
        $tmp[] = $row['group'];
      }
    }
  }

  $sql = "SELECT * FROM `news` WHERE `group` IN('" . implode("','",$tmp) . "')";

?>

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.