Jump to content

Remove part of my array..


Xyphon

Recommended Posts

So, I am making a system that shows if there are new posts or not. How I have it done is a singular array called "Read" will have all of the IDs of the read posts.

 

It is entered in the database like so:

$ID2= $_SESSION['UserID'];
$ID2 = mysql_real_escape_string($ID2);
$Readornot=mysql_query("SELECT * FROM forum_read WHERE Reader_ID='$ID2'");
$Readornot2=mysql_fetch_array($Readornot);
if($ID2==0)
{
}
else
{
if(empty($Readornot2))
{
$Read1= array("Read" => $Get_TopicID);
$Read2 = implode('-',$Read1);
mysql_query("INSERT INTO forum_read (Reader_ID,`Read`) values ('$ID2','$Read2')") or die(mysql_error());
}
else
{
$cr_topicid=$Readornot2['Read'];
if(strpos($cr_topicid,'-'))
{
$cr_topicid=explode('-',$cr_topicid);
if(!in_array($Get_TopicID,$cr_topicid,true))
{
$Read0=implode('-',$cr_topicid);
$Read1= array("Read" => $Read0, $Get_TopicID);
$Read2= implode('-',$Read1);
mysql_query("UPDATE forum_read SET`Read`='$Read2' WHERE Reader_ID='$ID2'") or die(mysql_error());
}
}
else
{
if($cr_topicid==$Get_TopicID)
{
}
else
{ 
$Read1= array("Read" => $cr_topicid, $Get_TopicID);
$Read2= implode('-',$Read1);
mysql_query("UPDATE forum_read SET`Read`='$Read2' WHERE Reader_ID='$ID2'") or die(mysql_error());
}
}
}
}

 

It all works fine and dandy to the point where if it's in the database an icon will be black and white, and if its not it wont, signifying read or unread.

 

What I need to do, however, is make it so when there is a new post, it takes all of the entries in the database with the topic ID out of the database, and strips the one part.

 

What I thought of at first was to use str_replace to replace

"-" . $TopicID . "-"

with

"-"

 

But of course, that won't work, because the very first entry could be  the topic ID, so it would just be $TopicID- and not -$TopicID- in the database.

 

So, how would I go about doing this?

Link to comment
Share on other sites

I don't really understand the point =\ I'd love to help but I don't know what you're trying to do..

 

from what I read you're trying to do -topicname- etc.. BUT..

 

I really think you should be using ids instead of topic names.. because mysql is a relational database, and using indexs to relate to data is alot better than using text in a non-unique index to index topics in a 'read' table..

 

ids can never be duplicated in an auto increment field :)

Link to comment
Share on other sites

I don't really understand the point =\ I'd love to help but I don't know what you're trying to do..

 

from what I read you're trying to do -topicname- etc.. BUT..

 

I really think you should be using ids instead of topic names.. because mysql is a relational database, and using indexs to relate to data is alot better than using text in a non-unique index to index topics in a 'read' table..

 

ids can never be duplicated in an auto increment field :)

I never once said Topicname, in fact every single time I said topicid(please reread my post...)

What I thought of at first was to use str_replace to replace

"-" . $TopicID . "-"

See, never said Topicname.

I have an array, for example if the user viewed topics 3, 4, and 103, it'd be 3-4-103 in the database. What I want to know is how would I go about making it so when a new post is made, it draws everyone who has the ID in their database(I've gotten that far), then REMOVE that ID from their array and update it again, so to turn 3-4-103 into 3-4 when someone makes a new post in topic 103.

Link to comment
Share on other sites

try

<?php
$string = '';
$patterns = array();
$patterns[0] = "/^".$TopicID . "-/"; // For start ids
$patterns[1] = "/-".$TopicID . "-/"; // For middle ids
$patterns[2] = "/".$TopicID . "$/"; // For end ids
$replacements = array();
$replacements[0] = '-';
$replacements[1] = '-';
$replacements[2] = '-';
echo preg_replace($patterns, $replacements, $string);
//This should handle all three cases 
// 1-
// 1-1-1
// 1-1
?>
not tested

Link to comment
Share on other sites

try

<?php
$string = '';
$patterns = array();
$patterns[0] = "/^".$TopicID . "-/"; // For start ids
$patterns[1] = "/-".$TopicID . "-/"; // For middle ids
$patterns[2] = "/".$TopicID . "$/"; // For end ids
$replacements = array();
$replacements[0] = '-';
$replacements[1] = '-';
$replacements[2] = '-';
echo preg_replace($patterns, $replacements, $string);
//This should handle all three cases 
// 1-
// 1-1-1
// 1-1
?>
not tested

I messed around with yours a bit and it seems to work. Thanks!

Link to comment
Share on other sites

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.