Jump to content


exploding, removing a key, then putting it back together!


4 replies to this topic

#1 coolaid

    P2L Jedi Master

  • Members
  • PipPipPipPip
  • 1,435 posts
  • Gender:Male
  • Interests:i wonder..

Posted 15 July 2006 - 10:52 PM

this is what i have so far:
$result = mysql_query("select username, favorites from users where favorites like \"%". $_GET['tutorial'] ."%\"");
while($row = mysql_fetch_row($result) )
{
	explode("|", $row[1]);
}

i'm gettin each user tha thas the tutorial id in the field "favorites". then i explode the field so each key is a tutorial id.

now i need to find a way to remove the array key holding the same tutorial id then putting the array back together!?

so can anybody shed some light? :o

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 16 July 2006 - 01:46 AM

array_pop
array_shift
array_keys
array_search

If one of those isn't one of the functions you're looking for, well then, there are a hundred more on all of the pages that you can look through :blush:

However, i'm sure the following function, although rather obvious, would help the most with the last part of your question.
implode

p.s. The result of array_search() plus unset() may be exactly what you're looking for. But then again, it's late, i'm tired, and i probably missed something in your post.

Edited by rc69, 16 July 2006 - 01:48 AM.


#3 coolaid

    P2L Jedi Master

  • Members
  • PipPipPipPip
  • 1,435 posts
  • Gender:Male
  • Interests:i wonder..

Posted 16 July 2006 - 03:17 AM

heh, you were right! i used array_search along with unset and came up with this code (i think it's pretty efficient but i'd like to hear if anyone has any enhancements :blush:)
$result = mysql_query("select id from users where favorites like \"%". $_GET['tutorial'] ."%\"");
while($row = mysql_fetch_row($result) )
{
	$result2 = mysql_query("select favorites from users where id = '". $row[0] ."' limit 1");
	$row2 = mysql_fetch_row($result2);
						  
	$fav_list = explode("|", $row2[0]);
	$key = array_search($_GET['tutorial'], $fav_list);
				
	unset($fav_list[$key]);
	$fav_list_new = implode("|", $fav_list);
						  
	mysql_query("update users set favorites = '$fav_list_new' where id = '". $row[0] ."' limit 1");
}


#4 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 16 July 2006 - 07:16 AM

$result = @mysql_query("select id from `users` where favorites like \"%". $_GET['tutorial'] ."%\"") or die(mysql_error());
while($row = mysql_fetch_row($result) )
{
	$result2 = @mysql_query("select favorites from `users` where id = '". $row[0] ."' limit 1") or die(mysql_error());
	$row2 = mysql_fetch_row($result2);
						  
	$fav_list = explode("|", $row2[0]);
	$key = array_search($_GET['tutorial'], $fav_list);
				
	unset($fav_list[$key]);
	$fav_list_new = implode("|", $fav_list);
						  
	@mysql_query("update `users` set favorites = '$fav_list_new' where id = '". $row[0] ."' limit 1") or die(mysql_error());
}

Sorry, i just had to add he error checking :blush: :D ;)

It just bugs me lol :D Also tecnically the table should have backticks around it :)

Edited by .Matt, 16 July 2006 - 07:16 AM.


#5 coolaid

    P2L Jedi Master

  • Members
  • PipPipPipPip
  • 1,435 posts
  • Gender:Male
  • Interests:i wonder..

Posted 16 July 2006 - 10:55 AM

bah i never used backticks in my entire history of mysql.... why start now :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users