Jump to content

Need help with setting a position


NathanLedet

Recommended Posts

I'm working on a small CMS and I've been following the tutorials at Lynda.com.

 

When I'm on the page to Add a Page, I want to set its position (the order), so I have a drop down list which counts the number of records inside the database, and then adds 1 (if I have 3 entries, I want "4" to be highlighted so it will automatically be put in the last position)

 

The code I currently have will count the rows, and then select the position it's currently in:

 

$page_count = mysql_num_rows($page_set);

for($count=1; $count <= $page_count+1; $count++){
echo "<option value=\"{$count}\"";
	if ($sel_page['position'] == $count){
		echo " selected";
		}
			echo ">{$count}</option>";
	}

 

I want to modify that code so that when I'm adding a new entry, it automatically chooses the last available option.

 

Thanks for the help :)

Link to comment
https://forums.phpfreaks.com/topic/126792-need-help-with-setting-a-position/
Share on other sites

Where is your $sel_page['position'] variable being set?

It's set elsewhere in the page and returns a number from the database. that number is the position: 1, 2, 3 etc.

 

 

Also, why not just do this:

	

if (($page_count+1) == $count){
echo " selected";
}

 

 

I dunno... Ask Kevin Skoglund...I'm following his lessons.  :)

 

 

Yes let me gather it up. 

 

but I don't think I was clear enough - sorry about that. 

 

The code I posted does work.  It works whenever I EDIT the page.  It properly displays the selected position.

 

What I am trying to do is re-structure that code that I already have and make it so that when I'm ADDING a page, it automatically chooses the last available position.  So, if I have 3 pages, I want that script to display and have already selected "4" in the drop down menu.

 

 

functions.php

function get_all_pages(){
global $conn;
$query = "SELECT * FROM pages ORDER BY position ASC";
$page_set = mysql_query($query, $conn);
confirm_query($page_set);
return $page_set;	
}

 

add_page.php - Form

$page_set = get_all_pages();
$page_count = mysql_num_rows($page_set);
for($count=1; $count <= $page_count+1; $count++){
echo "<option value=\"{$count}\">{$count}</option>";
}

 

You're still not showing where $sel_page is set. I suggest you check to see if that is set or not. If it is not, default to $page_count+1 rather than $sel_page['position']. But, I don't know your code for setting that, so I can't help you with that part without seeing it.

OK... tinkered around with the code a little bit and this code here works.  I appreciate you help F1Fan, and I apologize for not being clear.  My original code in my first post had nothing to do with the result, and therefore $sel_page makes no difference

 

for($count=1; $count <= $page_count+1; $count++){
echo "<option value=\"{$count}\" ";
if ($count == $page_count+1){
		echo " selected=\"selected\"";
		}
		echo ">{$count}</option>";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.