Jump to content

Recommended Posts

I know php can do pretty much everything, so Im trying to think up a way to set a "global message" on my homepage.

 

So far, I coded a page where I can set & edit the message. The message is stored in a database, where I "fetch" & echo on the index page. So thats up & working.

 

Now what I want to do is make it delete-able by the user. There will be an x or close link on it, & it will erase the message when hes logged in.

 

I was thinking Id have to use a cookie for this but I cant think up anyway to make it hide when the member closes it out?

 

 

Link to comment
https://forums.phpfreaks.com/topic/247183-i-need-help-planning-my-code/
Share on other sites

You might want to explain a bit more what purpose of it this message is. From the way you describe it, you will allow a normal user to delete a system message. In a way that other members won't see that message any more since it got deleted.

 

Do you maybe want a system message to be shown to every user. and ones a specific users says "okay i read it" it will not be shown any more to that specific users but will be to other users that did not read it. Describe your wish a bit more and it will probably give you the solution when you think about it.

Yeah i

You might want to explain a bit more what purpose of it this message is. From the way you describe it, you will allow a normal user to delete a system message. In a way that other members won't see that message any more since it got deleted.

 

Do you maybe want a system message to be shown to every user. and ones a specific users says "okay i read it" it will not be shown any more to that specific users but will be to other users that did not read it. Describe your wish a bit more and it will probably give you the solution when you think about it.

Yeah im sorry im terrible with words & explaining stuff.

 

I would like for the user to be able to click an x or a link that says close or whatever, so it wont show up in his account, but will still be available to other users that have not closed it out.

 

I think the way Im even going about it is shoddy.

 

Like I said, I created a page where I edit the message in HTML thru a textarea, its saved in a database table, then from there I made an include page that fetches the value from the database & echos it. I then put the include code in the index page. Make sense? lol...

 

So what would you think the best way to go about this would be?

You need two database tables:

1)  The table that holds the message, the dates it should appear, and (maybe) the number of times it needs to be dismissed before it dies forever.

 

2)  A table that contains the userID, the messageID, the date it was last shown, and how many times it was dismissed.

 

When you make a message, put it in table 1.

 

When someone views the page, find a message that's supposed to be visible that day that hasn't been dismissed by that user (or that hasn't yet been dismissed the proper number of times).

 

Since you're displaying a message, insert/update the record in table 2 for the display date of the message.

 

When the user dismisses the message, update table 2 (maybe through an ajax call) to reflect that they dismissed it.

 

-Dan

You need two database tables:

1)  The table that holds the message, the dates it should appear, and (maybe) the number of times it needs to be dismissed before it dies forever.

 

2)  A table that contains the userID, the messageID, the date it was last shown, and how many times it was dismissed.

 

When you make a message, put it in table 1.

 

When someone views the page, find a message that's supposed to be visible that day that hasn't been dismissed by that user (or that hasn't yet been dismissed the proper number of times).

 

Since you're displaying a message, insert/update the record in table 2 for the display date of the message.

 

When the user dismisses the message, update table 2 (maybe through an ajax call) to reflect that they dismissed it.

 

-Dan

Wow, thank you I appreciate the info. But I think this is way to advanced for me! I am pretty good with PHP but with SQL Im still learning. I cant even think of a way to do this. So I already have a table userinfo that has the all of the members information, userid, email, pass, etc etc. Im wondering if I can maybe add a value in that table called something like "msgdata" , with a value of 0 or 1. Default is 0, then when the user clicks the "x" in the message it will execute a query to update msgdata to 1. When its set to 1, the message dissapears.

 

Thats probably the easiest way for me. I just need to think of how to implement this with PHP.

 

I just remembered that if I issue a new message the user wont see it then if its set to 1. Now idk! Somehow implement a message ID like you suggested? Like I said, Im just having trouble thinking of how I can code all of this up.

 

 

 

Im going to try

Which part are you having trouble with?  I described the tables and the functionality of the system as it should be implemented.  There are 3 tables:

1)  The user table you already have.

2)  A "messages" table (you needed one anyway)

3)  A table that links them together.

 

You can remove some of the more complex bits of my plan, like the "dismissal count" and stuff like that.  Keep it basic.  If there's a link between the user and the message, don't show it.  Otherwise, show it.  When they click the X, insert the record that links the user to a message.

 

-Dan

Try something like this:

script

<?php

$user = 1; //link to the user id.

if(isset($_GET['read'])) {
$id = (int)$_GET['read'];
$query = "INSERT INTO messages_read (message_id,user_id) VALUES ('$id','$user')";
mysql_query($query) or trigger_error(mysql_error());
}

$sql = "SELECT m.* FROM messages AS m WHERE m.id NOT IN (SELECT message_id FROM messages_read WHERE user_id = '$user')";

$result = mysql_query($sql) or trigger_error(mysql_error());
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)) {
	echo '<a href="?read=' . $row['id'] . '">X</a><div>' . $row['message'] . '</div>';
}
}
else {
echo 'There was no messages to show!';
}

?>

 

database tables

--
-- Table structure for table `messages_read`
--

CREATE TABLE IF NOT EXISTS `messages_read` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `message_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--
-- Table structure for table `messages`
--

CREATE TABLE IF NOT EXISTS `messages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `message` text NOT NULL,
  `author` int(11) NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

NOTE that you should be using your current user table, and linking with the user_id from that user table.  This is just a working example (un-tested) so you can see what Dan was talking about.

I'm going to provide an alternative to this. My solution will be less flexible, but more efficient. It assumes you'll only want to have one message in the system at one time

 

Add a new column to the user's table - assuming you store users in a database. Call this column 'message_viewed' or something similarly descriptive. Make it a boolean type, that defaults to 0

 

When a user logs in, check that column. If it's 0, then display the message. When the user clicks the little x, set that flag to 1. Now it will no longer show when the user logs in/views that site. Any time you update the message, simply update all rows in the users table and set that column to 0. This is a simple SQL: UPDATE `table` SET `message_viewed` = 0. By leaving out the WHERE clause, it will update every row.

 

Hope this helps. It won't teach you good fundamentals like the above solutions, but will work nicely and be quite efficient at doing this.

I'm going to provide an alternative to this. My solution will be less flexible, but more efficient. It assumes you'll only want to have one message in the system at one time

 

Add a new column to the user's table - assuming you store users in a database. Call this column 'message_viewed' or something similarly descriptive. Make it a boolean type, that defaults to 0

 

When a user logs in, check that column. If it's 0, then display the message. When the user clicks the little x, set that flag to 1. Now it will no longer show when the user logs in/views that site. Any time you update the message, simply update all rows in the users table and set that column to 0. This is a simple SQL: UPDATE `table` SET `message_viewed` = 0. By leaving out the WHERE clause, it will update every row.

 

Hope this helps. It won't teach you good fundamentals like the above solutions, but will work nicely and be quite efficient at doing this.

Ah! You got it. That works for me. Thank you guys alot for the help.

 

Try something like this:

script

<?php

$user = 1; //link to the user id.

if(isset($_GET['read'])) {
$id = (int)$_GET['read'];
$query = "INSERT INTO messages_read (message_id,user_id) VALUES ('$id','$user')";
mysql_query($query) or trigger_error(mysql_error());
}

$sql = "SELECT m.* FROM messages AS m WHERE m.id NOT IN (SELECT message_id FROM messages_read WHERE user_id = '$user')";

$result = mysql_query($sql) or trigger_error(mysql_error());
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)) {
	echo '<a href="?read=' . $row['id'] . '">X</a><div>' . $row['message'] . '</div>';
}
}
else {
echo 'There was no messages to show!';
}

?>

 

database tables

--
-- Table structure for table `messages_read`
--

CREATE TABLE IF NOT EXISTS `messages_read` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `message_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--
-- Table structure for table `messages`
--

CREATE TABLE IF NOT EXISTS `messages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `message` text NOT NULL,
  `author` int(11) NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

NOTE that you should be using your current user table, and linking with the user_id from that user table.  This is just a working example (un-tested) so you can see what Dan was talking about.

 

This is also good for me. Thank you for your code & taking the time to create this. I incorporated everything you guys suggested & I have it up & working now!!

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.