Re-positioning After Delete

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
grishnoff2323
Forum Newbie
Posts: 5
Joined: Wed Dec 01, 2010 4:03 pm

Re-positioning After Delete

Post by grishnoff2323 »

Hey there!
I am working on a CMS for a basic website. Currently this code below will update the navigation bar correctly, but if you change the position on a subject item, it duplicates position numbers. For instance, if I had the following subject items: Home, About, and Contact - if I wanted to change Contact to position #1, it would...but Home would also stay at position #1.

I am trying to alter this code so that if someone were to update the position of a subject item, the other subject item would switch to the other number. (if Contact changed to position #1, Home would switch to position #3.)

I am not versed in php classes and OOP so basic php coding help would be great. If you need other parts of the code, let me know. Thank you so much.

Code: Select all

$id = mysql_prep($_GET['subj']);
$subject_name = mysql_prep($_POST['subject_name']);
$position = mysql_prep($_POST['position']);
$visible = mysql_prep($_POST['visible']);
				
$query = "UPDATE subjects SET 
		subject_name = '{$subject_name}', 
		position = {$position}, 
		visible = {$visible} 
	WHERE id = {$id}";
$result = mysql_query($query, $connection);
	if (mysql_affected_rows() == 1) {
	        $message = "The subject was successfully updated.";
	} else {
		$message = "The subject was NOT updated.";
		$message .= "<br />". mysql_error();
	}
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Re-positioning After Delete

Post by social_experiment »

You would have to modify values of the other links as well (excuse me for stating the obvious). Can you paste an example of what the information looks like in the database? The fields are subject_name, visible and position, am i correct in saying each subject item occupies 1 row?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
grishnoff2323
Forum Newbie
Posts: 5
Joined: Wed Dec 01, 2010 4:03 pm

Re: Re-positioning After Delete

Post by grishnoff2323 »

Yes, that's correct -

Table = subjects
4 Rows = id, subject_name, visible, position
Last edited by grishnoff2323 on Sat Feb 12, 2011 10:24 am, edited 1 time in total.
grishnoff2323
Forum Newbie
Posts: 5
Joined: Wed Dec 01, 2010 4:03 pm

Re: Re-positioning After Delete

Post by grishnoff2323 »

id, int(11), AUTO_INCREMENT
subject_name, varchar(30), utf8_general_ci
position, int(3)
visible, tinyint(1)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Re-positioning After Delete

Post by social_experiment »

Do you have any existing code that you can paste? Specifically related to the switching you wish to do.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
grishnoff2323
Forum Newbie
Posts: 5
Joined: Wed Dec 01, 2010 4:03 pm

Re: Re-positioning After Delete

Post by grishnoff2323 »

No I don't. I assumed it would be done during the UPDATE which I pasted above. Is that not correct?
grishnoff2323
Forum Newbie
Posts: 5
Joined: Wed Dec 01, 2010 4:03 pm

Re: Re-positioning After Delete

Post by grishnoff2323 »

Here is the form I use. It is used for adding a new subject as well as editing an existing one. When $new_subject = false that obviously says that you are editing an existing subject.

Code: Select all

<?php if (!isset($new_subject)) {$new_subject = false;} ?>

<p>Subject Name: <input type="text" name="subject_name" value="<?php echo $sel_subject['subject_name']; ?>" id="subject_name" /></p>
<p>Postition: <select name="position">
	<?php
		if (!$new_subject) {
			$subject_set = get_all_subjects();
			$subject_count = mysql_num_rows($subject_set);
		} else {
			$subject_set = get_all_subjects();
			$subject_count = mysql_num_rows($subject_set) + 1;
		}
		for($count=1; $count <= $subject_count; $count++) {
			echo "<option value=\"{$count}\"";
			if ($sel_subject['position'] == $count) { echo " selected"; }
			echo ">{$count}</option>";
		}
	?>
	</select>
</p>
<p>Visible:
	<input type="radio" name="visible" value="0"
	<?php
		if ($sel_subject['visible'] == 0) { echo " checked"; }
	?> /> No
		&nbsp;
	<input type="radio" name="visible" value="1"
	<?php 
	if ($sel_subject['visible'] == 1) { echo " checked"; }
	?> /> Yes
</p>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Re-positioning After Delete

Post by social_experiment »

grishnoff2323 wrote:No I don't. I assumed it would be done during the UPDATE which I pasted above. Is that not correct?
Update is correct but you will somehow have to calculate new values for each of the new positions, i was refering to that code. If i understand your idea correctly i would go about it in the following manner : Assume that the selected value becomes #1, the previous #1 would now become + something and so forth (depeding on the amount of links).
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply