php / mysql members and marking function

I used the code to insert the array, my code looks like this now:

[php]$query=“SELECT id FROM db_users”;
$result2 = mysql_query($query) or die(mysql_error());
$user_id = array();
while($row = mysql_fetch_assoc($result2))
{
$user_id[] = $row;
}
$array_string=mysql_escape_string(serialize($user_id));

$sql="INSERT INTO $tbl_name(
date,
archive,
year,
message,
name,
subject,
viewed
)
VALUES(
‘$date’,
‘no’,
‘2014’,
‘$message’,
‘$name’,
‘$subject’,
‘$array_string’
)
";

$result = mysql_query($sql);[/php]

this insert a whole bunch of characters, but if I look at the structure I see a repeated serie with the same numbers as ID’s, so it looks to me this works. It inserts all ID’s to the message. I haven’t tried to use the code to get the data back from the database, as inserting was the main problem for now.

Well, the serialization process will change the look of it inside the database.
So, you really can not look at it unless you know how the serialized version would look.
(It is a coded version of the data into numbers, so it will repeat some numbers…)

So, you MUST use the pulling-out, unserialization routine to be able to see it. Just use that
routine and then print the array to see if it is correct. Should work for you!

Good luck…

tried it with this code:

[php]mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);

$query=“SELECT viewed FROM $tbl_name”;
$result = mysql_query($query) or die(mysql_error());

while($rs=mysql_fetch_assoc($result))
{
$array= unserialize($rs[‘viewed’]);
print_r($array);
}
?>[/php]

copied from the site you gave me, and it returns the same array, the same characters and values as it was inserted. So this works!

VERY NICE! Yea!

Okay, now, you have the ID’s in your array and you can alter the code when the message
is viewed. As we discussed before, you can delete the entry for that user’s ID once they read
the message. And, “UPDATE” the message’s “viewed” field.

I think this process will work nicely for what you are trying to do. Let us know how the rest goes…

Good luck !

Okee, next thing, I need to convert the array to single values, because I need them to be linked to the table beneath the message. This table displays the photos of the member (the one they can click so everybody knows he marked the message). So if one of the users clicks his photo, the ID that belongs to this person needs to be changed (deleted) from the array, so the array can be updatet.

I have this piece of code:

[php]while($rs=mysql_fetch_assoc($result))
{
$array= unserialize($rs[‘viewed’]);
}

foreach ($array as $ids) {
echo "Used ID = $ids
";
}[/php]

In my test database (this array named “viewed” which we created in the past comments) I have 16 different ID’s. So the array contains one line starting with a:16 (because of the 16 values), followed by 16 unique ID’s. The code above loads (and unserialize) these values and prints 16 lines beneath eachother, because of the FOREACH code. It creates the exact number of values, the only problem I have now; it’s not getting the number of the ID, it just prints the word “array”, not the number.

So my screen looks like:

Used ID = Array
Used ID = Array
Used ID = Array
Used ID = Array
Used ID = Array
Used ID = Array
Used ID = Array
Used ID = Array
Used ID = Array
Used ID = Array
Used ID = Array
Used ID = Array
Used ID = Array
Used ID = Array
Used ID = Array
Used ID = Array

How can I get the number, to get it linked and changed?

Well, first, the array you have been using for this part is just for NON-VIEWED member ID’s.

You would have to have another list or array that would hold all of the “LIVE” member ID’s.
Then, parse thru them all displaying the icon or picture for each. In parsing thru them, for
each one, you would check if their ID is in the viewed array. If it is display one icon if not display
the other icon.

If they click on the icon or picture for NOT-VIEWED, then, you delete their ID from the viewed array
update it back to the database table.

So, not sure as I do not have all this set up on my system, but, your code:
while($rs=mysql_fetch_assoc($result))
{
$array= unserialize($rs[‘viewed’]);
}

foreach ($array as $ids) {
echo "Used ID = $ids
";
}
would have to be more like this: ( Untested, typing it now )
[php]
// Previously, you loaded all IDs into the $user_id array
// Now grab NON-viewed IDs
$rs=mysql_fetch_assoc($result);
$viewed = unserialize($rs[‘viewed’]);

// Loop thru all IDs, checking if they are viewed or not…
foreach ($user_id as $user) {
echo “Used ID #” . $user; // show user id for this user only
if ( in_array($user, $viewed) ) {
echo " - Not Viewed Yet
"; // This is where you would insert their non-viewed pix-icon
} else {
echo " - Already Viewed
"; // Insert the already-viewed pix-icon here
}
}
[/php]
Not sure if your $user_id array and your $viewed array are set up to pull out the $user key value
this way or not. You might have to use a WHILE instead of the FOREACH… Not sure. I can test
it later on if you need further help with it. But, in this manner, you can check each of your user’s
ID to see if it is stored in the $viewed array. Should work nicely… Let us know how it goes.

PS: Is this part of the message dynamically loading section in your other thread?

Looks logical :smiley: What I did right now:

I have two tables:

  1. users (ID - Name - Address - etc)
  2. messages (name - subject - viewed - live)

The VIEWED is the serialized array of all user ID’s, which where grabbed from table1 at the moment the message was saved. So basicly, all users whom are registered at that moment.

The LIVE is the serialized array of all user ID’s, not regarding the ACTIVE-state of a user. So if a user has left (not gonna use the messageboard anymore), his details are still stored in table 1. A simple value of “yes/no” in the ACTIVE row will tell mysql whether an user’s still active or not. I need this so if a new user registers, he’ll still be able to check (and mark) messages which where posted before his registration. So, the names may be confusing, I know, but the LIVE holds all user ever registered, the VIEWED holds just the users who’re still active. This row can be changed I guess, because these are the values which will tell if the user has marked the message.

I hope I explained it clearly? Are you still following me what I’m trying to say? :wink:

Then,

I used your untested code and modified it to this:

[php]$query=“SELECT viewed FROM $tbl_name”;
$result = mysql_query($query) or die(mysql_error());

$query3=“SELECT live FROM $tbl_name”;
$result3 = mysql_query($query3) or die(mysql_error());

while($rs3=mysql_fetch_assoc($result3))
{
$user_id= unserialize($rs3[‘viewed’]);
}

// Previously, you loaded all IDs into the $user_id array
// Now grab NON-viewed IDs
$rs=mysql_fetch_assoc($result);
$viewed = unserialize($rs[‘viewed’]);

 //  Loop thru all IDs, checking if they are viewed or not...
 foreach ($user_id as $user) {
    echo "Used ID #" . $user;   // show user id for this user only
   if ( in_array($user, $viewed) ) {
      echo " - Not Viewed Yet<br>";   //  This is where you would insert their non-viewed pix-icon
   } else {
      echo " - Already Viewed<br>";   //  Insert the already-viewed pix-icon here
   }
}[/php]

But this results in 16 lines of errors:

Used ID #Array
Warning: in_array() expects parameter 2 to be array, boolean given in /nfs/home/deb22994/domains/entertainyou.nl/public_html/whatsup/FunEntertainmentLGN/unserialize_array.php on line 33

  • Already Viewed

line 33:[php]if ( in_array($user, $viewed) ) {[/php]

And yes, this is party of the other thread, but I think I’m not gonna continue with that problem, it’s causing too many problems. I’ll keep that the old (working) way. This thread is the main problem.

Well, I think it is the way that the $viewed array was created.
So, let’s debug it a bit. Let’s comment out all of these first and see if it priints the list of id’s.
if ( in_array($user, $viewed) ) {
echo " - Not Viewed Yet
"; // This is where you would insert their non-viewed pix-icon
} else {
echo " - Already Viewed
"; // Insert the already-viewed pix-icon here
}

So, basically, it should just display the list of user id’s. And, then, if that works correctly we need to see
what exactly is inside the $viewed array. I did find a note online that talks about checking the insides
of multidimensional arrays. But, it might be easier to just save the id’s instead of all of the array.
So, before it goes into this section, please do a print_r($viewed); command to see what is in the
array after it is pulled out of the database…

Let me know if what the results is of these two tests…

So, back again after 2 weeks of holiday and 2 weeks of very hard working… Sorry I couldn’t get back to you earlier.

I think I’ve found a solution, a work-around, for the problem. I’ve created an extra table, which holds 3 id’s and a img src.

1 - it’s own unique ID
2 - the ID of the message
3 - the ID of the user
4 - the src of the image (i.e. image.png)

I didn’t need to change the page too much to get this thing working, thankfully :D. Basicly, everytime a user enters a new message, it inserts the ID of this newly created message in this extra table for each user that is active. So, if I have for example 6 active users, it inserts 6 rows with the same msgID, only the user ID is different. The source of the image is connected to the user ID.

So after message with ID 103 is inserted, the table looks like this:

ID | userID | msgID | src
1 | 1 | 103 | user_1.png
2 | 2 | 103 | user_2.png
3 | 3 | 103 | user_3.png
4 | 4 | 103 | user_4.png
5 | 5 | 103 | user_5.png
6 | 6 | 103 | user_6.png

if someone clicks on his photo, the same page as I’ve already had changes the src to the other image.

Seems to work perfectly. Thanks for your patience and many, many answers! I really appreciate your help, the fact that you took much time and effort in order to help me. Can’t thank you enough for that! I’ll let you know when everything’s ready 8)

Thanks!

Great! We are here to help. Thanks for the Karma, Too… I don’t expect that as I just like to help.
But, nice to get thanked, also.

So we will mark this one solved. When you run into a snag with the next issue, let us know.

Congrats!

Sponsor our Newsletter | Privacy Policy | Terms of Service