SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Resistance is Futile webgodjj's Avatar
    Join Date
    Nov 2002
    Location
    Madison, WI USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    text box reordering script abit off...

    Ok... I'm close to getting this to work. i want to be able to order the way things appear on a page. There is a database that has a table test with columns of id and text.

    It (should) show a textbox with the items to be ordered in it and three buttons. One to submit and two others to move the selected item up and down. However, only the submit button shows up..

    atomiccoffeemedia.com/text.php

    Can you find the waldo of bad code here? I am using a php version of an ASP code that was posted in this forum. I also have stripped out the PEAR code....

    PHP Code:
    <?php


    $db 
    mysql_connect("localhost""XXX""XXX");

    if (!
    $db) {
          echo( 
    "<p>Unable to connect to the " .
                
    "database server at this time.</p>" );
          exit();
        }
    mysql_select_db("atommco_test",$db);

    if (! @
    mysql_select_db("atomicco_test") ) {
          echo( 
    "<p>Unable to locate the " .
                
    "database at this time.</p >" );
                 exit();
        }

    ?>

    <SCRIPT>

    function outputList(ar, name, size) {

        var strIDs = "<SELECT SIZE=\"" + size + "\" NAME=\"ro_lst" + name + "\">"
        
        var sel = " SELECTED"
        
        for (var i=0;i<ar.length;i++) {
        
            strIDs += "<OPTION " + sel + " VALUE=\"" + ar[i][0] + "\">" + ar[i][1]
            
            sel = ""
            
        }
        
        strIDs+="</SELECT>"
        
        strIDs+="<INPUT NAME=\"" + name + "\" TYPE=hidden>"
        
        return strIDs
        
    }

    function outputButton(bDir,name,val) {

        return "<INPUT TYPE=button VALUE=\"" + val + "\" ONCLICK=\"move(this.form," + bDir + ",'" + name + "')\">"
        
    }

    function move(f,bDir,sName) {

        var el = f.elements["ro_lst" + sName]
        
        var idx = el.selectedIndex
        
        if (idx==-1)
        
            alert("You must first select the item to reorder.")
            
        else {
        
            var nxidx = idx+( bDir? -1 : 1)
            
            if (nxidx<0)
            
                nxidx=el.length-1
                
            if (nxidx>=el.length)
            
                nxidx=0
                
            var oldVal = el[idx].value
            
            var oldText = el[idx].text
            
            el[idx].value = el[nxidx].value
            
            el[idx].text = el[nxidx].text
            
            el[nxidx].value = oldVal
            
            el[nxidx].text = oldText
            
            el.selectedIndex = nxidx
            
        }
        
    }

    function processForm(f) {

        for (var i=0;i<f.length;i++) {
        
            var el = f[i]
            
            if (el.name.substring(0,6)=="ro_lst") {
            
                var strIDs = ""
                
                for (var j=0;j<f[i].options.length;j++)
                
                    strIDs += f[i].options[j].value + ", "
                    
                    f.elements[f.elements[i].name.substring(6)].value = strIDs.substring(0,strIDs.length-2)
                    
            }
            
        }
        
    }

    </SCRIPT>

    <?

    if (!$submit) {

        
    $sql "SELECT * FROM test";
    //    $result = $db->query($sql);
    $result=mysql_query($sql,$db);
    $num mysql_num_rows($result);

        
    ?>

    <FORM METHOD=post ONSUBMIT="processForm(this)">

    <SCRIPT>

        var arrList = new Array()

    <? // This generates the javascript array from the DB entries

        
    $i 0;

    //    while ($row = $result->fetchRow()) {

    while ($num >= $i) {
    $row mysql_fetch_array($result);

            
    $id $row[id];

            
    $name $row[text];

            echo 
    " arrList[$i] = new_Array(\"$id\",\"$name\")\n";

            
    $i++;

        }

    ?>

        document.write(outputButton(true,"test","Move Up") + "<BR>")

        document.write(outputButton(false,"test","Move Down") + "<BR>")

        document.write(outputList(arrList,"test",10))



    </SCRIPT>

    <BR>

    <INPUT TYPE=submit name="submit" VALUE="Submit New Order">

    </FORM>

    <?

    }



    if (
    $submit) {

        
    // This breaks apart the result and dumps in $order_arr

        
    $order_arr split(','$test);
        
        
    $o 1;
        
        
    // This updates each row in the database.

        // Might be a little too processor intensive?

        
    for($i=0$i<count($order_arr); $i++) {

            echo 
    "HERE: $o - $order_arr[$i]<br>\n";

            
    $sql "UPDATE test SET o=$o WHERE id=$order_arr[$i]";

    //        $result = $db->query($sql);
            
    $result=mysql_query($sql,$db);


            
    $o++;

        }

    }

    ?>
    Last edited by webgodjj; Dec 25, 2002 at 16:57.

  2. #2
    PHP Developer W1LL's Avatar
    Join Date
    Apr 2001
    Location
    Leicester, UK
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no idea, but try posting this one in the Javascript forum aswell.

    - Will


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •