Reputation: 5747
I have written a script that allows me to reorder items on a menu.
The code selects (to display), the title of the page and the order in which the page appears on the menu.
For example, if there are 5 pages (About Us, Mission & Vision, Awards, Environmental Policy, Locations), the order will be 1-5.
As you can see from the screenshot, the code generates 5 dropdowns all with the 5 page options. On the other dropdown, you get 1-5.
What I'm trying to do is get the menu to load the names and page order that the menu is already in straight into that form.
So each dropdown will have a page title and the corresponding order in the other dropdown. The user would then be able to change this if needed.
How could I change my code to achieve this?
<?php
$apageid = array();
$apagename = array();
$apageorder = array();
$q = "SELECT g.id, g.title, n.order FROM tbl_general g INNER JOIN tbl_navigation n ON n.pageid = g.id WHERE n.type = '$_SESSION[parent]' ORDER BY n.order";
$return = $database->query($q);
while($row=mysql_fetch_assoc($return)){
$apageid[] = $row['id'];
$apagename[] = $row['title'];
$apageorder[] = $row['order'];
}
$count = count($apageid);
$bpageid = array();
$bpageorder = array();
?>
<div class="form-holder">
<?php
//run through each page one at a time and update the order of the menu
for($i=0; $i<$count; $i++){
?>
<div class="form-row">
<div class="form-label">
Page Name
</div>
<div class="form-field">
<select class="select-small" name="<?php echo $bpageid[$i] ?>">
<?php
for($j=0; $j<$count; $j++){
?>
<option value="<?php echo $apageid[$j]; ?>"><?php echo $apagename[$j]; ?></option>
<?php
}
?>
</select>
<select class="select-small" name="<?php echo $bpageorder[$i] ?>">
<?php
for($k=0; $k<$count; $k++){
?>
<option value="<?php echo $apageorder[$k]; ?>"><?php echo $apageorder[$k]; ?></option>
<?php
}
?>
</select>
</div>
</div>
<?php
}
?>
Upvotes: 0
Views: 941
Reputation: 1003
Alright I know what you need.
change this:
for($i=0; $i<$count; $i++){
to this:
mysql_data_seek( $return ); //reset the pointer to do the same query
while($row=mysql_fetch_assoc($return)){
$id = $row['id'];
$title = $row['title'];
$order = $row['order'];
this will loop each id field straight from the db instead of from your arrays. Also we have variables now for each field during the loop. Now change the array var for id to the id var we added earlier:
<select class="select-small" name="<?php echo $id; ?>">
Now for the page name select fields lets add this to the second for loop:
for($j=0; $j<$count; $j++){
if($apageid[$j] == $id) {
$selected = true;
} else {
$selected = false;
}
Now add a variable to make the current option value in the array 'selected' if it is equal to the value of the current row
<option value="<?php echo $apageid[$j]; ?>" <? echo ($selected == true) ? 'selected="selected"' : ''; ?>><?php echo $apagename[$j]; ?></option>
then do the same for the select on the bottom:
<select class="select-small" name="<?php echo $order; ?>">
<?php
for($k=0; $k<$count; $k++){
if($apageorder[$k] == $order) {
$selected = true;
} else {
$selected = false;
}
?>
<option value="<?php echo $apageorder[$k]; ?>" <? echo ($selected == true) ? 'selected="selected"' : ''; ?>><?php echo $apageorder[$k]; ?></option>
<?php
}
?>
</select>
Upvotes: 2