Xyphon Posted August 30, 2010 Share Posted August 30, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/212102-remove-part-of-my-array/ Share on other sites More sharing options...
RussellReal Posted August 30, 2010 Share Posted August 30, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/212102-remove-part-of-my-array/#findComment-1105339 Share on other sites More sharing options...
Xyphon Posted August 30, 2010 Author Share Posted August 30, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/212102-remove-part-of-my-array/#findComment-1105349 Share on other sites More sharing options...
samshel Posted August 30, 2010 Share Posted August 30, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/212102-remove-part-of-my-array/#findComment-1105365 Share on other sites More sharing options...
Xyphon Posted August 30, 2010 Author Share Posted August 30, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/212102-remove-part-of-my-array/#findComment-1105371 Share on other sites More sharing options...
RussellReal Posted August 31, 2010 Share Posted August 31, 2010 sorry buddy, I'm out of it today =\ Quote Link to comment https://forums.phpfreaks.com/topic/212102-remove-part-of-my-array/#findComment-1105418 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.