Code that sorts multiple divs according to xml node

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andreigoea
Forum Newbie
Posts: 2
Joined: Tue Nov 10, 2009 2:44 pm

Code that sorts multiple divs according to xml node

Post by andreigoea »

Hello,
Please be kind, this is my first ever PHP script and I've only worked on it 2 days.

The script should first display images. The information for the images is located in an xml file.They are sorted by the ID tags in the xml. After displaying the images I want the user to be able to change their order by changing the ID tag in the xml. I've done pretty much everything but it just doesn't swap the id's correctly.

Code: Select all

<?php
// LOAD XML
//----------------------------------------------------------------
//Count Clip
 
$xmlDoc = new DOMDocument();
$xmlDoc->load("content.xml");
$clip = $xmlDoc->getElementsByTagName("clip");
$clipUrl = $xmlDoc->getElementsByTagName("file");
$elNo = $clip->length;
$rowsNo = $elNo/8;
$rowsNo =  ceil($rowsNo);
$contentHeight = $rowsNo*160+10;
 
 
//Get Clip Name
$imgTitles = $xmlDoc->getElementsByTagName("title");
 
//Get Clip No
$no = $xmlDoc->getElementsByTagName("no");
 
 
 
//MOVE SCRIPT
//----------------------------------------------------
//
 
    if(isset($_POST['add']))
{
    $noPressed = $_POST["imageNo"];
    $noPressedb = $_POST["imageNob"];
    $noPresseda = $_POST["imageNoa"];
 
    
    $currentNo=($no->item($noPressed));
    $newCurrentNo= $xmlDoc->createElement('no', $noPresseda);
    $currentNo->parentNode->replaceChild($newCurrentNo, $currentNo);
    $nextNo=($no->item($noPresseda));
    $newNextNo= $xmlDoc->createElement('no', $noPressed);
    $nextNo->parentNode->replaceChild($newNextNo,$nextNo);
    $xmlDoc->save('content.xml');
    
}
    else if(isset($_POST['sub']))
{
    $noPressed = $_POST["imageNo"];
    $noPressedb = $_POST["imageNob"];
    $noPresseda = $_POST["imageNoa"];
 
    
    $currentNo=($no->item($noPressed));
    $newCurrentNo= $xmlDoc->createElement('no', $noPressedb);
    $currentNo->parentNode->replaceChild($newCurrentNo, $currentNo);
    $nextNo=($no->item($noPressedb));
    $newNextNo= $xmlDoc->createElement('no', $noPressed);
    $nextNo->parentNode->replaceChild($newNextNo,$nextNo);
    $xmlDoc->save('content.xml');
    
}
 
 
?>
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
 
<?php
echo "<div id='pics' style='margin:auto; width:960px; height:"; echo $contentHeight; echo "px; background-color:#e6e6e6; border:2px solid #a02223;'>";
 
for ($imgCount=0; $imgCount < $elNo; $imgCount += 1){
    $noV=($no->item($imgCount)->nodeValue);
    $noVb=($no->item($imgCount-1)->nodeValue);
    $noVa=($no->item($imgCount+1)->nodeValue);
    $image=($clipUrl->item($noV)->nodeValue);
    $imgTitle=($imgTitles->item($noV)->nodeValue);
 
    $subNo = "<div><form id='$imgCount moveDown' action='$_SERVER[PHP_SELF]' name='$imgCount moveDown' enctype='multipart/form-data' action='' method='post'>
<input name='imageNo' type='hidden' value='$noV' />
<input name='imageNob' type='hidden' value='$noVb'/>
<input name='imageNoa' type='hidden' value='$noVa'/>
<input type='submit' name='sub' value='<' >
</form></div>";
    $addNo = "<div><form id='$imgCount moveUp' action='$_SERVER[PHP_SELF]' name='$imgCount moveUp' enctype='multipart/form-data' action='' method='post'>
<input name='imageNo' type='hidden' value='$noV' />
<input name='imageNob' type='hidden' value='$noVb'/>
<input name='imageNoa' type='hidden' value='$noVa'/>
<input type='submit' name='add' value='>' >
</form></div>";
    
    if(file_exists($image)) 
{   
    list($width, $height, $type, $attr) = getimagesize($image);
    if ($height>$width){
        printf("<div style='padding:5px; width:110px; height:150px; float:left; margin:0px; border-bottom:1px solid #333333;'><div style='height:100px; width:100px;'><A href='$image'><img src='$image' height='100' alt='$image'></A></div><div style='padding:2px; text-align:left; width:100%%;'>$imgTitle  </div>  <table><tr><td>$subNo</td><td> $addNo </td></tr></table></div>");
 
    } else {
        printf("<div style='padding:5px; width:110px; height:150px; float:left; margin:0px; border-bottom:1px solid #333333;'><div style='height:100px; width:100px;'><A href='$image'><img src='$image' width='100' alt='$image'></A></div><div style='padding:2px; text-align:left; width:100%%;'>$imgTitle   </div>  <table><tr><td>$subNo</td><td> $addNo </td></tr></table></div>");
    }
} 
else 
{ 
printf("<div style='padding:5px; width:110px; height:150px; float:left; margin:0px;border-bottom:1px solid #333333;'>No Image</div>") ;
}
echo '<br> noV=' .$noV;
echo '<br> noVb=' .$noVb;
echo '<br> noVa=' .$noVa;
}
 
 
echo "<br></div>";
 
?>
</body>
</html>
The script is located at:http://mediapod.org/ProPlayer/order.php
Thank you
andreigoea
Forum Newbie
Posts: 2
Joined: Tue Nov 10, 2009 2:44 pm

Re: Code that sorts multiple divs according to xml node

Post by andreigoea »

Should I add more details, or is the method I'm trying to use completely wrong?
Post Reply