Hi,

I have this code that adjusts the position of the subjects and the pages in my site.

This is working where the value of the radio button = 1 (which is = showing, in the database - And 0 = Not showing) And I am talking about pages for my site :-)

Im pulling the values from the form here:

$visible = ($_POST['visible']);
$position = ($_POST['position']);
$oldposition = ($_POST['oldposition']);
$pid = ($_POST['pid']);
$subjectid = ($_POST['subjectid']);
$oldsubjectid = ($_POST['oldsubjectid']);
$pagetitle = ($_POST['pagetitle']);
$description = ($_POST['description']);
$keywords = ($_POST['keywords']);
$linklabel = ($_POST['linklabel']);
$pagebody = ($_POST['pagebody']);

This code below works perfect for adjusting the positions of both pages or subjects when the value of the radio button is = 1..
(I know maybe its long, Im new to it)
Which is = $visible from the above written:

if($visible = '1'){ // The value of the radio button, which means the page will display

/* Hvis en PAGE flytter subject ($oldsubjectid != subjectid), så bliver eksisterende sider i det "nye" subject 1 højere, og siderne i det "gamle" subject bliver en mindre*/
if($oldsubjectid != $subjectid){
$queryAdjustDownForOldSubject = mysqli_query($myConnection, "UPDATE pages SET pos = pos - 1 WHERE pos > $oldposition AND subjectid = $oldsubjectid");
$queryAdjustUpForNewSubject = mysqli_query($myConnection, "UPDATE pages SET pos = pos + 1 WHERE pos >= $position AND subjectid = $subjectid");
$query = mysqli_query($myConnection, "UPDATE pages SET subjectid='$subjectid', pagetitle='$pagetitle', linklabel='$linklabel', description='$description', keywords='$keywords', pos='$position', pagebody='$pagebody', showing='$visible', lastmodified='now()' WHERE id='$pid'") or die (mysqli_error($myConnection));	


echo '<div align="center">Operation Completed Successfully! Head back to the admin home page, or the main website to see your changes!<br /><br /><a href="login/admin.php">Click Here For Admin home!</a> 
<br /><br /><a href="../" >Or head back to view the live website!</a></div>';
exit();
}

// Hvis en PAGE IKKE skifter subject, men får en LAVERE position, f.eks NED til position 2 FRA position 12
if ($oldposition > $position){
$queryDown = mysqli_query($myConnection, "UPDATE pages SET pos = pos + 1 WHERE pos >= $position AND pos <= $oldposition AND subjectid = $subjectid");
$query = mysqli_query($myConnection, "UPDATE pages SET subjectid='$subjectid', pagetitle='$pagetitle', linklabel='$linklabel', description='$description', keywords='$keywords', pos='$position', pagebody='$pagebody', showing='$visible', lastmodified='now()' WHERE id='$pid'") or die (mysqli_error($myConnection)); 
} 

elseif($oldposition < $position) {
// Hvis en PAGE IKKE skifter subject, men får en HØJERE position, f.eks OP til position 12 FRA position 2
$queryUp = mysqli_query($myConnection, "UPDATE pages SET pos = pos - 1 WHERE pos > $oldposition AND pos <= $position AND subjectid = $subjectid");
$query = mysqli_query($myConnection, "UPDATE pages SET subjectid='$subjectid', pagetitle='$pagetitle', linklabel='$linklabel', description='$description', keywords='$keywords', pos='$position', pagebody='$pagebody', showing='$visible', lastmodified='now()' WHERE id='$pid'") or die (mysqli_error($myConnection));
} 

elseif($oldposition = $position) {	
// Hvis en PAGE ikke skifter position, foretages en "almindelig" update query
$query = mysqli_query($myConnection, "UPDATE pages SET subjectid='$subjectid', pagetitle='$pagetitle', linklabel='$linklabel', description='$description', keywords='$keywords', pos='$position', pagebody='$pagebody', showing='$visible', lastmodified='now()' WHERE id='$pid'") or die (mysqli_error($myConnection));
} 
echo '<div align="center">Operation Completed Successfully! Head back to the admin home page, or the main website to see your changes!<br /><br /><a href="login/admin.php">Click Here For Admin home!</a> 
<br /><br /><a href="../" >Or head back to view the live website!</a></div>';
exit();
}

And then my problem appears: If I click the radio button with the value = 0.

The I get gaps in the positions in the database.

1) If a page stays under the same subject, BUT visible (Radio btn) is set to = 0, meaning the page doesnt diaplay - Then the pages whch have a bigger position than the page I just set to visible = 0, doesnt adjust their position, and go 1 position down, where they are bigger than the "visible = 0 page" - I get a gap then.

2) If a page moves to another subject, and get a position etc etc, but I set visible = 0 - then the script actually makes a gap in the positions under the new subject, even thoug the page is not visible. Like the position is reserved.. :-) So even though the page is not visible, the pages under the new subject, adhusts their position as if it was visible!

3) And...lets say admin is editing a page, he lets it stay under the same subject, but moves the position of the page from lets say position 1 to position 4, he fills everything out, but sets visible = 0! - Then the script again makes a gap, and "reserves" that position, even though the page is not showing (visible = 0)

I it a bit hard to explain, but I hope someone have understood my problem and can see how else I can adjust the positions when the radio button with value = 0 is set. Thats the button which decides wheter a page should display or not.

Can I somehow integrate if($visible = '0'){Code here....}

In the above script?

What a story, Hope someone sees it and maybe can help me with this issue :-)

Dani AI

Generated

Quick diagnosis for : two things are causing the gaps. The script never remembers the previous visibility state, so it cannot tell a 1->0 toggle from a 1->1 or 0->1 change, and some conditional checks use the assignment operator (=) instead of a comparison (== or ===). That combination makes the “visible” code path run when it should not, and the page ends up reserving a slot or leaving the old slot unadjusted — exactly the gaps you described (same-subject hide, move+hide, reorder+hide).

What to change (in order of impact): add a hidden oldvisible field to the edit form so the parser knows the prior state; validate/cast numeric inputs (positions and ids) to integers; replace = in conditions with == or ===; and explicitly handle the four transition cases (1->0, 0->1, 1->1, 0->0) combined with whether the subject changed. Semantically: a page counts toward positions only when visible=1. So 1->0 must decrement positions in the old subject (closing the gap); 0->1 must increment positions in the target subject (making room); moving while visible=1 must decrement the old subject and increment the new; visible=0 moves should not reserve space.

Safety and reliability: run the related updates and the final page UPDATE inside a single transaction to avoid partial states. Escape or bind all inputs (prepared statements or at least mysqli_real_escape_string) and check query results. Use NOW() (without quotes) or a PHP timestamp for last-modified. If concurrency is a real concern, lock affected rows (SELECT ... FOR UPDATE) or serialize admin edits.

Quick practical step to try now: add the hidden oldvisible input, fix the conditional operators, and group the adjustment queries plus the page update in one transaction. Those three changes alone will prevent positions from being left reserved by non-visible pages.

This is the form I process above, if it helps to understand it better :-)

<form id="form" name="form" method="post" action="page_edit_parse.php" onsubmit="return validate_form ( );">
    
<p><b>Sidens titel:&nbsp;<span class="required">*</span></b></p><input name="pagetitle" type="text" id="pagetitle" size="55" maxlength="65" value="<?php echo $pagetitle; ?>"  />
   
<br /><br />
    
<p><b>Navn på link:&nbsp;<span class="required">*</span></b></p><input name="linklabel" type="text" id="linklabel" size="55" maxlength="65" value="<?php echo $linklabel ?>"  />
    
<br /><br />
    
<?php 	
// Her bestemmes hvilket SUBJECT siden skal høre til				
$query="SELECT id, linklabel, pos FROM subjects ORDER BY pos ASC";
?>            					           		
<?php $result = mysqli_query($myConnection, $query); ?>
<p><b>Vælg hvilket subject siden skal tilhøre:&nbsp;<span class="required">*</span></b></p>
<select class="boxstyles" name='subjectid'>
<?php			
// Udskriver værdierne i select boxen, med ID, LINKLABEL
while($row=mysqli_fetch_array($result)){
echo "<option value=\"$row[id]\">$row[linklabel]</option>";
// Option values bliver tilføjet efterhånden som arrayen bliver løbet igennem
}
?>
</select>						
<?php mysqli_free_result($result); ?>
	
<br /><br />
    
<p><b>Vælg sidens position:&nbsp;<span class="required">*</span></b></p><input name="position" type="text" id="position" size="1" maxlength="10" value="<?php echo $position ?>" /> 	
    
<br /><br />

<p><b>Upload siden:&nbsp;<span class="required">*</span></b></p>
<input type="radio" name="visible" value="1" checked> Ja &nbsp;&nbsp;
<input type="radio" name="visible" value="0"> Nej
    
<br /><br />
</div>     
     
<div id="body_right">
<p><b>Description:</b><i>&nbsp;(Optional - Read "Tips On SEO" for information!)</i></p><textarea name="description" id="description" cols="47" rows="3"><?php echo $description; ?></textarea>   

<br /><br />  
  
<p><b>Keywords:</b><i>&nbsp;(Optional - Read "Tips On SEO" for information!)</i></p><textarea name="keywords" id="keywords" cols="47" rows="3"><?php echo $keywords; ?></textarea>

</div>

<div id="body_bottom">
<p><b>Sidens indhold:&nbsp;<span class="required">*</span></b></p><textarea name="pagebody" id="textarea1" cols="100" rows="19"><?php echo $pagebody; ?></textarea> 
   
<br />

<input name="oldsubjectid" type="hidden" value="<?php echo $oldsubjectid; ?>" />
<input name="oldposition" type="hidden" value="<?php echo $oldposition; ?>" />
<input name="pid" type="hidden" value="<?php echo $id; ?>" /><br />
<input type="submit" name="button" id="button" value="&nbsp;Udfør!&nbsp;" />
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.