have I shot myself in the foot... that is the question!
Hey guys I think I've just royally shot myself in the foot. I have a website where users have the开发者_StackOverflowir own profile and can post on their walls but I'm getting to the point where I want to implement that friends can post on my wall and I can post on my friends wall.
This is what my site looks like so far:
My database design is like so:
And here is a code snippet of how I achieve posting on walls:
protected void Button1_Click(object sender, EventArgs e)
{
string theUserId = Session["UserID"].ToString();
using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=******;"))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand("INSERT INTO WallPosting (UserID, Wallpostings) VALUES (" + theUserId + ", '" + TextBox1.Text + "')", cn))
{
cmd.ExecuteNonQuery();
}
}
PopulateWallPosts(theUserId);
}
}
Now my problem is how on earth do I get friends posts on my wall AHHHHH... runs in fear!!!
Have I shot myself in the foot?
Also this is how I generate my content on the page:
private void PopulateWallPosts(string userId)
{
using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand("SELECT idWallPosting, wp.WallPostings, p.PicturePath FROM WallPosting wp LEFT JOIN User u ON u.UserID = wp.UserID LEFT JOIN Pictures p ON p.UserID = u.UserID WHERE wp.UserID=" + userId + " ORDER BY idWallPosting DESC", cn))
{
//("SELECT wp.WallPostings, p.PicturePath FROM WallPosting wp LEFT JOIN [User] u ON u.UserID = wp.UserID LEFT JOIN Pictures p ON p.UserID = u.UserID WHERE UserID=" + userId + " ORDER BY idWallPosting DESC", cn))
using (OdbcDataReader reader = cmd.ExecuteReader())
{
test1.Controls.Clear();
while (reader.Read())
{
System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
div.Attributes["class"] = "test";
div.ID = String.Format("{0}", reader.GetString(0));
string id = Convert.ToString(div.ID);
//store the div id as a string
Image img = new Image();
img.ImageUrl = String.Format("{0}", reader.GetString(2));
img.AlternateText = "Test image";
div.Controls.Add(img);
div.Controls.Add(ParseControl(String.Format("   " + "{0}", reader.GetString(1))));
div.Attributes.Add("onclick", "confirm_delete(" + id + ");");
// send the div id to javascript
div.Style["clear"] = "both";
test1.Controls.Add(div);
}
}
}
}
}
Your WallPosting table needs another userId, representing who sent it ( The first one being whose wall it is).
Then just handle those two userids in your insertion code: Who sent it, who's it for.
EDIT Here's what a sample select would look like:
SELECT wp.WallPostings, p.PicturePath
FROM WallPosting wp
INNER JOIN [User] u ON u.UserID = wp.PostedByUserID
INNER JOIN Pictures p ON p.UserID = u.UserID
WHERE wp.UserID=" + userId + "
ORDER BY idWallPosting DESC
I would also keep a record of at least what date/time the post was made and order by/display that.
I'd say you need to add another table called 'Walls', bind those to a User, and then have Wallpostings have a 'Wall Id', then just retrieve all WallPostings that have the ID equal to the ID of the user's wall..
精彩评论