Jump to content

[SOLVED] Is there a good way to do this?


chantown

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) . "')";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.