Hello,

Im trying to make a feed for my website, anyway, what i want it to do is everything from table messages where the userid = the friend id, but what if there is multiple friend id's. sorry if this is a really easy thing, but im a newbie to php / mysql. then i want it to array all the results

this is what ive got so far, it might be completely wrong idno,

<?php
$uid = "$_SESSION[userid]"; // get userid 
$friendid = "SELECT * FROM friends WHERE userid='$uid'"; // find friendships
$query = "SELECT * FROM messages WHERE userid='$friendid'"; // 1.
$result = mysql_query($query);
while($r=mysql_fetch_array($result)) // 2.
{
echo "<br><table width='100%'><tr bgcolor='$title_cell_color'><td>
<img src='to do..'><b>to do..</b> posted on to do..</td></tr>
<tr bgcolor='to do..'><td>the auctull message will go here..</td></tr>
</table><br>";

}
?>

so to cap it off, i want to check the database for a list of my friend ids, then select everything from table messages where the userid in that is equal to my friends ids, then array all the results in a table

help please.. been trying to get it to work for a few days now

Thanks :)
Mackey

Recommended Answers

All 2 Replies

Take a look at this. You need two pages and so created the two page show_massages.php and read_messages.php. The show_messages.php displays all messages that a particular user has received from his friends and the read_messages.php is where the content of the message is shown when the user either clicks on the message title or on the read link.
I also created two tables friends and messages. Below are the details of each table.
friends
id: User's id
friend_id : id's of the user's friends
firstname: first name of the user's friend
lastname: last name of the user's friend

messages table
reciepient: user' id
title: title of the message
message: message content
sender: id of the friend who sent the message
sent_time: time and date when the message was sent

Here is the code for the show_messages.php

<?php
	$id = $_SESSION['userid'];
	$connection = mysql_pconnect("hostname", "username", "password") or die(mysql_error());
	mysql_select_db("test") or die(mysql_error());
	$query = "SELECT firstname, lastname,title, sent_time
	FROM friends, messages WHERE friends.id = '$id'
	AND friends.friend_id = messages.sender ORDER BY messages.sent_time";

	$result = mysql_query($query) or die(mysql_error());
	echo "<table width = '70%' align = 'center' border = '1'>";
		echo "<tr>";
			echo "<td><b>Sender</b></td>";
			echo "<td><b>Message</b></td>";
			echo "<td><b>Date</b></td>";
			echo "<td><b>Operation</b></td>";
		echo "</tr>";
		
		while($some = mysql_fetch_object($result))
		{
			echo "<tr>";
				echo "<td>".$some->lastname." ".$some->firstname."</td>";
				echo "<td><a href = 'read_message.php?read=".urlencode($some->sent_title)."&time=".urlencode($some->sent_time)."'>".$some->title."</a></td>";
				echo "<td>".$some->sent_time."</td>";
				echo "<td>";
				echo "<i><a href = 'read_message.php?read=".urlencode($some->sent_title)."&time=".urlencode($some->sent_time)."'>Read</i></a>";
				echo "</td>";
			echo "</tr>";
		}
	echo "</table>";
	
?>

and here is the code of the read_message.php page

<?php
	$connection = mysql_pconnect("hostname", "username", "password") or die(mysql_error());
	mysql_select_db("test") or die(mysql_error());
	
	$query = "SELECT firstname, lastname, message, sent_time
	FROM friends, messages WHERE messages.reciepient = '".$_SESSION['userid']."' 
	AND messages.title = '".$_GET['read']."'
	AND friends.friend_id = messages.sender 
	AND messages.sent_time = '".$_GET['time']."'LIMIT 1";
	
	$result = mysql_query($query) or die(mysql_error());
	$msg_details = mysql_fetch_object($result);
	
	echo "<table width = '50%' align = 'center' border = '1'>";
		
		echo "<tr>";
			echo "<td>From : </td><td>".$msg_details->lastname." ".$msg_details->firstname."</td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td>Date : </td><td>".$msg_details->sent_time."</td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td>Title : </td><td>".$_GET['read']."</td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td>Message : </td><td>".$msg_details->message."</td>";
		echo "</tr>";
	echo "</table>";
	
?>

Note that in codes above I have assumed you already stated a session on each page and that the session, $_SESSION, contains an ID that uniquely identified the current user.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.