Allow the user to sort an array
Posted: Thu Jun 19, 2008 4:47 pm
Hi folks,
I'm stumped on why my PHP array is not recording the correct values passed from another array. I think it is a simple issue with assigning arrays to each other, but I'm not sure. If anyone has an idea, I'd love to know.
This is the page I'm working on:
http://www.kickasscupcakes.com/gallery/BrokenPage.php (page fixed and renamed)
The bottom section has a form that allows the user to enter the sort order of the categories. When the submit button is clicked the page reloads and runs the PHP code.
The PHP code takes the values of the sort fields and puts them in an array which is then sorted with asort() keeping the key values. I'm using the key values to determine how to rearrange the master array. Using a loop, I build a temporary array in the correct order. This array is then run through another loop to update the XML data.
This sounds pretty straight forward and it appears to be sorting the master array correctly, but the temporary array isn't recording it correctly. It will record the first item to move in the correct space, but then uses that value to update any other items that are to be moved.
Feel free to try out the page. I'm echoing the data from the master array as it is being sorted (first loop) and then echoing the temporary array afterwards (second loop).
Jonathan D.
I'm stumped on why my PHP array is not recording the correct values passed from another array. I think it is a simple issue with assigning arrays to each other, but I'm not sure. If anyone has an idea, I'd love to know.
This is the page I'm working on:
http://www.kickasscupcakes.com/gallery/BrokenPage.php (page fixed and renamed)
The bottom section has a form that allows the user to enter the sort order of the categories. When the submit button is clicked the page reloads and runs the PHP code.
The PHP code takes the values of the sort fields and puts them in an array which is then sorted with asort() keeping the key values. I'm using the key values to determine how to rearrange the master array. Using a loop, I build a temporary array in the correct order. This array is then run through another loop to update the XML data.
This sounds pretty straight forward and it appears to be sorting the master array correctly, but the temporary array isn't recording it correctly. It will record the first item to move in the correct space, but then uses that value to update any other items that are to be moved.
Feel free to try out the page. I'm echoing the data from the master array as it is being sorted (first loop) and then echoing the temporary array afterwards (second loop).
Code: Select all
if ($_GET['cmd'] == 'edit') {
//retireve the existing data from the Albums.xml file
global $xml;
$album = $xml->album;
//update the category names from the cat fields
$i = 1;
$sort;
foreach ($album as $data) {
$catname = "cat" . $i;
$data->name = trim(strip_tags($_POST[$catname]));
//put the sort order data into an array
$sortname = "sort" . $i;
$sort[] = trim(strip_tags($_POST[$sortname]));
$i++;
}
//perform an asort on the array
asort($sort);
//do a foreach on the array to get the key index
$i = 0;
echo "**Sort**<br />";
foreach ($sort as $key => $value) {
//use the key to retrieve the category from the xml data.
$tempname = $album[$key]->name;
$name[$i] = $tempname;
$albumName = $album[$i]->name;
echo "$tempname<br />";
//echo $name[$i];
//$category[$i] = $album[$key]->category;
$i++;
}
echo "**Record**<br />";
for( $j=0; $j < $i; $j++) {
echo "$name[$j]<br />";
$xml->album[$j]->name = $name[$j];
$xml->album[$j]->category = $category[$j];
}
//save as new xml object
//write the albums.xml file
//display a message to the user
$message = "Categories succesfully updated";
}