Jump to content

Recommended Posts

I am making a bulletin script, where users can post little messages that all of their friends will see. THe problem that i'm having though is I only want the user who posts the bulliten, only their friends can see the bulletin. Right now I have it working but it's not really how I want it to be (it's very inefficent). Right now I have this code:

 

			$findfriends = "select * from friends where username = '".$_SESSION['username']."'";
			$friendsresults = $db->query($findfriends);
			$friendnumrows = mysqli_num_rows($friendsresults);
			if($friendnumrows == 0)
			{
				//echo nothing;
			}
			else if($friendnumrows != 0)
			{
				while($friendrows = $friendsresults->fetch_assoc())
				{
					$friend = $friendrows['friendname'];

					$bulletin_query = "select * from bulletin where postuser = '".$friend."'";
					$bulletin_results = $db->query($bulletin_query);
					$bulletin_rows = mysqli_num_rows($bulletin_results);
					if($bulletin_rows == 0)
					{
						//echo "<tr>Your friends have <b>not</b> posted any bulletins.</tr>";
					}
					else if($bulletin_rows != 0)
					{
						while($bulletinrow = $bulletin_results->fetch_assoc())
						{
							$bulletinposter = $bulletinrow['postuser'];
							$bulletintime = $bulletinrow['date'];
							$bulletintitle = $bulletinrow['title'];
							$bulletinid = $bulletinrow['id'];

							echo"<tr>
    <td width='20%' bgcolor='#e9e9e9'><a href='/profile.php?user=$bulletinposter'><b>$bulletinposter</b></a></td>
    <td width='35%' bgcolor='#e9e9e9'>$bulletintime</td>
    <td width='45%' bgcolor='#e9e9e9'><a href='/bulletin.php?action=readbulletin&bulletinid=$bulletinid'>$bulletintitle</a></td>
  </tr>";
						}
					}
				}

 

As you can see, I have to go through every single one of the users friends and get the bulletins they have posted. This results in not exactly what I want because if the user has 50 friends, that's 50 bulletins being posted, and if the user has posted two or more bulletins then that's 50x2. So about 150 bulletins are being posted on one page. I am trying to find a way where I can get the bulletins in one MySql table, but that would make every single user and every single friend have the same bulletin in the MySql table which can result in a waste of data. So if you think you can help me out here, please do so! Thanks!

Link to comment
https://forums.phpfreaks.com/topic/117853-simple-php-bulletin-script-help/
Share on other sites

okay i only skimmed this but you could try this.. (BTW: using UserID would be better then usernames)

**UNTESTED

<?php

			$findfriends = "select * from friends where username = '".$_SESSION['username']."'";
			$friendsresults = $db->query($findfriends);
			$friendnumrows = mysqli_num_rows($friendsresults);
			if($friendnumrows == 0)
			{
				//echo nothing;
			}
			else if($friendnumrows != 0)
			{
				$friend = "";
				while($friendrows = $friendsresults->fetch_assoc())
				{
					$friend .= $friendrows['friendname']."','"; //Build friendlist
				}
				$friend = "'".trim($friend,",")."'"; // remove last comma

				$bulletin_query = "select * from bulletin where postuser IN ($friend)";
				$bulletin_results = $db->query($bulletin_query);
				$bulletin_rows = mysqli_num_rows($bulletin_results);
				if($bulletin_rows == 0)
				{
					//echo "<tr>Your friends have <b>not</b> posted any bulletins.</tr>";
				}
				else if($bulletin_rows != 0)
				{
					while($bulletinrow = $bulletin_results->fetch_assoc())
					{
						$bulletinposter = $bulletinrow['postuser'];
						$bulletintime = $bulletinrow['date'];
						$bulletintitle = $bulletinrow['title'];
						$bulletinid = $bulletinrow['id'];

						echo"<tr>
    <td width='20%' bgcolor='#e9e9e9'><a href='/profile.php?user=$bulletinposter'><b>$bulletinposter</b></a></td>
    <td width='35%' bgcolor='#e9e9e9'>$bulletintime</td>
    <td width='45%' bgcolor='#e9e9e9'><a href='/bulletin.php?action=readbulletin&bulletinid=$bulletinid'>$bulletintitle</a></td>
  </tr>";
					}
				}
?>

 

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.