Page 1 of 1

Re-positioning After Delete

Posted: Fri Feb 11, 2011 10:36 am
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();
	}

Re: Re-positioning After Delete

Posted: Fri Feb 11, 2011 4:32 pm
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?

Re: Re-positioning After Delete

Posted: Sat Feb 12, 2011 10:14 am
by grishnoff2323
Yes, that's correct -

Table = subjects
4 Rows = id, subject_name, visible, position

Re: Re-positioning After Delete

Posted: Sat Feb 12, 2011 10:24 am
by grishnoff2323
id, int(11), AUTO_INCREMENT
subject_name, varchar(30), utf8_general_ci
position, int(3)
visible, tinyint(1)

Re: Re-positioning After Delete

Posted: Sun Feb 13, 2011 10:29 am
by social_experiment
Do you have any existing code that you can paste? Specifically related to the switching you wish to do.

Re: Re-positioning After Delete

Posted: Sun Feb 13, 2011 11:42 am
by grishnoff2323
No I don't. I assumed it would be done during the UPDATE which I pasted above. Is that not correct?

Re: Re-positioning After Delete

Posted: Sun Feb 13, 2011 11:50 am
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>

Re: Re-positioning After Delete

Posted: Sun Feb 13, 2011 3:48 pm
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).