Jump to content

[SOLVED] How to delete only certain text in a mysql row?


rcorlew

Recommended Posts

I have a table setup that has a column setup as friends and users can add friends to this column no problem.

 

What I would like to know is how to delete only certain parts of the text of the row/cell where their friends are stored.

 

The friends text is seperated by the following :

 

" , "

 

That is a space on either side and comma in the middle of the space.

 

I know how to explode the results and put into an array to display the reuslts, but is there a way to delete only one of the friends if the text is as follows.

 

, thomas , frank , irene ,

 

I think you can get the idea.

 

Is it possible to do this or should I just update the whole cell without the array[$i] result being the friend?

Link to comment
Share on other sites

OK I have worked up a little code to basically remove the item out of the array that has been created by exploding the text. I used a sutom function  that will remove the item from the array and then implode it back into a string and then it can be reinserted.

 

I do not know if this is the most efficient way of doing this, but is the only think I could think of, correct me if I am doing something wrong.

 

<?php

function array_remval($val,&$arr){
  $i=array_search($val,$arr);
  if($i===false)return false;
  $arr=array_merge(array_slice($arr, 0,$i), array_slice($arr, $i+1));
  return true;
}
?>

<?php
$remove = $_GET["remove"];

$users = ("thomas,frank,irene,john,luke,jenny");
$eu = explode(",", $users);

$ecount = count($eu);
$newcount = $ecount - 1;
$x = 0;
$y = $newcount;
for( $i = 0; $i <= $y; $i++) {
if(!isset($remove)) {
echo "<a href='project2.php?remove=1&text=$eu[$i]'>$eu[$i]</a><br />";
}
	if(isset($remove)) {
		$text = $_GET["text"];
		array_remval($text, $eu);
		}

}
if(isset($remove)) {	
$nu = implode(",", $eu);
echo "$nu";
}
echo "<br />";

echo "<br /><a href='project2.php'>Home</a>";

?> 

Link to comment
Share on other sites

Ok, if anyone would like to know how I soved this problem, I will post my code since I could not find any sort of method to delete only certain text.

 

<?php
$addto = $_GET["addto"];
if(isset($addto)) {
$result = mysql_query("UPDATE users SET friends = CONCAT(COALESCE(friends, ''),',$addto') WHERE userid='$_SESSION[userid]'",$con);	
}
function array_remval($val,&$arr){
  $i=array_search($val,$arr);
  if($i===false)return false;
  $arr=array_merge(array_slice($arr, 0,$i), array_slice($arr, $i+1));
  return true;
}
$remove = $_GET["remove"];
	echo "<table id='myForum'>
  				<tr>
    				<th align='center'>Viewing My Friends</th>
  				</tr>";
		//	echo "$user";
	$con = mysql_connect($HOST, $USER, $PASS); 
	if (!$con) { 
  			// Since the entire script depends on connection, die if connection fails 
  		die("Error connecting to MySQL database!"); 
	} 
	mysql_select_db($NAME, $con);

	$query = ("SELECT friends FROM users WHERE userid = '$_SESSION[userid]'"); 

	$result = mysql_query($query) or die;
  			echo"<tr>
    				<td valign='top'><br />";
	while($row = mysql_fetch_array($result)){


$users = $row[friends];

$eu = explode(",", $users);

$ecount = count($eu);

$newcount = $ecount - 1;

$x = 0;
$y = $newcount;
for( $i = 0; $i <= $y; $i++) {
if(!isset($remove)) {
	if($eu[$i] == null) {
		$eu[$i] = $_SESSION['userid'];
	}
echo "<a href='forum.php?function=viewuser&user=$eu[$i]'>$eu[$i]</a> 
	 Send <a href='forum.php?function=sendmessage&user=$eu[$i]'>message</a>
	      
	<a href='forum.php?function=friends&remove=1&text=$eu[$i]'>Remove</a> from friends<br /><br />";
}
	if(isset($remove)) {
		$text = $_GET["text"];
		array_remval($text, $eu);
		}

}
if(isset($remove)) {	
$nu = implode(",", $eu);
	$query = ("UPDATE f_users SET friends ='$nu' WHERE userid = '$_SESSION[userid]'"); 

	$result = mysql_query($query) or die;
echo "$nu";
?>

 

The only weird side effect by using this so far is that by concating the friends field with a comma delimeter, you end up with array[0] being null which I cam up with a very nice solution by making it say the user's name there. Until I can come up with something even fancier to repalce the concate, maybe some sort of is null or if null then do the concate otherwise just enter the first field entry.

 

But, if you would not like to have yourself as a friend, then who would like to be friends with you, lol.

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.