Page 1 of 1

Delete one section of information from a field full of info

Posted: Sun Jul 15, 2007 10:49 pm
by beloveddoll
Ok, hopefully I can explain this clearly enough.

I have several tables setup in my database. One is called image. image has id, url, and a bunch of other fields which stores image submission details when someone submits an image to the server. id is primary and auto increment.

Another table, userdata stores all of the registered person`s information, suchs as username, email address, favourites, etc. The field imageFavs is the field where if someone were to click on the "Add to favourites" link, id from image gets stored into imageFavs in userdata. Code shown:

Code: Select all

session_start();
include("dbcon.php");
include("auth.php");

//Date: February 25 2007
//For: secrettrance.net
//Description: Favorites the trance
$log = $_SESSION['sess_name'];

//add favorite
mysql_query("UPDATE userdata SET imageFavs=CONCAT(imageFavs,\",$id\") WHERE login='$log'");
mysql_query("INSERT INTO messagedata (recBoxID, sendBoxID, content, subject, isNew, date) VALUES ($g, '1', 'Your trance was added to $log `s favourites.<br><a href=trances.php?id=$id>Please click here to go to the trance</a>.<br>This is an automated message please do not reply.', 'New favourite added!', 1, NOW() )");

//Now let's add one to the favorite counter for the trance.
mysql_query('UPDATE `secrett1_artgallery`.`image` SET `favnum` = favnum +1 WHERE `image`.`id` =' . $id . ' LIMIT 1');
//This increments the field favnum under the row of the specified ID.

mysql_close($con);

//redirect
header("Location: trances.php?id=$id");
Now, suppose someone has their imageFavs field loaded with a bunch of these images. What if they decide "Hey, I want to remove this one image from my favourites list."

The original guy who did the original coding for my site had a favouritedelete code but it was...crappy. It removed the , from the field but the id numbers were still left and were molded together into one large number and that messed up the entire favourites display.

How do I make the code remove just the number?

(Just so everyone knows, I`ve learned a lot about php since the guy did the codes, I lost contact with him years ago which got me started in learning php myself. So I have basic understanding of php but am still quite new to it all.)

Like if the imageFavs entry was:
,34,637,364,24,274,11

And the person wants to remove image #24

,24 would need to be removed from the list while the rest remains intact.

How is this accomplished?

Posted: Sun Jul 15, 2007 11:04 pm
by feyd
This is partly why I commented about your table design in your previous thread...

Posted: Sun Jul 15, 2007 11:09 pm
by beloveddoll
feyd wrote:This is partly why I commented about your table design in your previous thread...
How do you recommend that it be done?

Posted: Sun Jul 15, 2007 11:25 pm
by feyd
beloveddoll wrote:How do you recommend that it be done?
Normalized.
I wrote:It would appear your database isn't designed to use typical normalization techniques. For example there would typically be a table containing separate rows referring to the particular user's "favs" which would contain a reference to the "collection" table. Selection would be a single query and the user would have an unlimited (nearly) number of "favs" versus the oddly limited form your code appears to use.

Posted: Sun Jul 15, 2007 11:27 pm
by beloveddoll
So you mean I would need a new table that would basically be like:

id user fav

And then have the page call on that?

That sounds like what I have for my comment system. But I run into a problem there. I can`t get the code to work with another table. Like in the example of this, I have yet to figure out how to make the code call on the info of image table that the above table would be linked to.

Just in case my lousy explanation skills are on the rise again

With my comments (because they are set up in the fashion I think you are describing) we have trances.php which calls on the info from image table. It also calls on comment table to show what comments were made to the id of the image table that gets displayed on trances.php

comment table has subject,content,date,id,sender

sender records the id of the person making the comment. subject will have the username of the person making the comment.

My skills do not permit me to figure out how to make the code call on icon from userdata where id from userdata is equal to sender

It will show the standard red x box showing that an image should be there but it won`t load the image.

I fear my skills will prevent me from working this method correctly to show info from image table that will be needed to properly display from this fav table.

Posted: Sun Jul 15, 2007 11:42 pm
by beloveddoll
Maybe I need to figure out how to work that part out before I move on. All right, on to a new topic then.