Jump to content

Count occurances of data in one table which is the same as another table


Recommended Posts

I have two tables.

 

Table 1, called tbl_maillist_lists, stores the mailing list information (ID, Listname, Desc, Status[Active,Deleted].

 

Table 2, called tbl_maillist_users, stores the users who may or may not be subscribed to any of the lists (ID, Name, Email, ListID[the ID of the List they are subscribed to], Status[Active,Pending,Deleted,A-Inactive].

 

What I want to do, if you haven't already guessed, is output the TOTAL number of subscribers for each list, whereas they are ACTIVE users.

 

Example:

Listname

Desc

Subscribers

ListA

Fun List!!!

76

 

ListA comes from the tbl_maillist_lists table, as does Desc, and Subscribers would be a count of all USERS who are subscribed to ListA, using the ID of the lists table and comparing it to the total count of all users with the same ListID in the users table. It's simple, but unfortunately, all I've been able to output is "Array"...Any help would be appreciated!

 

Current Code:

<?php
$result = mysql_query("SELECT * FROM tbl_maillist_lists ORDER BY ID")
or die(mysql_error());
$mylists = mysql_fetch_array( $result );
?>
<table width="100%" align="center" border="2">
<tr><td align="center" width="25%"><b>List Name</b></td><td align="center" width="60%"><b>Description</b></td>
<td align="center" width="15%"><b>Subscribers</b></td></tr>
<tr><td align="left"><?php echo $mylists['Listname']; ?></td><td align="left"><?php echo $mylists['Listdesc']; ?></td>
<?php
$ListsID = $mylists['ID'];
$sql = mysql_query("SELECT count(*) FROM tbl_maillist_users WHERE ListsID='$ListsID' AND Status='Active'")
or die(mysql_error());
$users = mysql_fetch_row($sql);
?>
<td align="center"><?php echo $users ?></td></tr>
?>

Here's how I did it, and I think this is necessary for how I am printing the various results:

 

<?php
//* Counts the rows and prints result *//
include ('+opendb.php');

$result = mysql_query("SELECT * FROM tbl_maillist_lists"); 
$num_rows = mysql_num_rows($result);
?>
<p>There <?php if ($num_rows == 1) { echo "is"; } else {echo "are"; } ?> currently <?php echo $num_rows ?> list<?php if ($num_rows > 1) { echo "s"; } ?> in the Mailing List database. Use the menu above to manipulate current lists, add lists, or delete lists.</p>
<?php
//* Lists the Mailing Lists *//
$listres = "SELECT * FROM tbl_maillist_lists ORDER BY ID";
$myres = mysql_query( $listres );
?>
<table width="100%" align="center" border="1">
<tr><td align="center" width="10%">ID</td><td align="center" width="20%"><b>List Name</b></td><td align="center" width="60%"><b>Description</b></td><td align="center" width="10%"><b>Subscribers</b></td></tr>
<?php
while($mylists = mysql_fetch_array($myres)){
?>
<tr><td align="center"><?php echo $mylists['ID'] ?></td><td align="left"><?php echo $mylists['Listname']; ?></td><td align="left"><?php echo $mylists['Listdesc']; ?></td>
<?php
//* Counts users subscribed to each list *//
$ListsID = $mylists['ID'];
$sql = mysql_query("SELECT * FROM tbl_maillist_users WHERE ListsID='$ListsID' AND Status='Active'");
$num_rows = mysql_num_rows($sql);
?>
<td align="center"><?php echo $num_rows ?></td></tr>
<?php }
?>

Look I'm not a PHP expert like ya'll :), so I'm amazed that I came up with this.  so like when one is learning how to read, I need everything spelled out.  I looked at the examples given, but I wasn't exactly sure WHERE to put said examples.  I tried figuring it out, but...it never did work. 

 

So if there's a better way to do it, by all means, show me...but put a bit more code up.  Like if you could include the line of code before and the line after, I could get it from there.  I just don't know exactly where to put the "$users = mysql_result($sql, 0, 0); or the "$users[0]", and how to pull from it the count.

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.