I have some previously written code that I am trying to modify. This is a portion of code to retrieve entries from a database and then edit inidivual sections, as required. Everything seems to work well with one exception. The code to create the drop down box to select the associated photo album creates the drop down list but does not pre-select the current album as the default valu. Here is the code ... where have I gone wrong?
``

ELSEIF ($do=="editListings"){

$page=$_GET['page'];
$nid = $_GET['nid'];
$iName = $_FILES['listingsImage']['name'];
$town=addslashes($_POST['town']);
$street=addslashes($_POST['street']);
$listings=addslashes($_POST['listings']);
$mls=addslashes($_POST['mls']);
$price=addslashes($_POST['price']);
$adj=addslashes($_POST['adj']);
$special=addslashes($_POST['special']);
$bed=addslashes($_POST['bed']);
$bath=addslashes($_POST['bath']);
$age=addslashes($_POST['age']);
$aid=addslashes($_POST['aid']);
$date=$_POST['date'];
$remImage=($_POST['remImage']);

echo "<span class=\"actionHead\">Modify a Listing</span>"
        ."<br/><br/>";

    IF ($page=="submit"){
        $maxfilesize = 3000000;

        $oldImageQuery = "SELECT * FROM listings WHERE nid=$nid"; 
        $oldImageResult = mysql_query($oldImageQuery, $connection) or die("Query failed : " . mysql_error()); 
        $oldImageName = stripslashes(mysql_result($oldImageResult,0,"image"));

        IF ($iName!=null){
            IF (($oldImageName=="")||($oldImageName==null)){

            $listingsImageArray = array();

            $imageQuery = "SELECT image FROM listings";
            $imageResult=mysql_query($imageQuery, $connection) or die("Query failed : " . mysql_error());
            $imagenumrows = mysql_num_rows($imageResult);

                for ($i = 0; $i < $imagenumrows; $i++) {
                    $image = stripslashes(mysql_result($imageResult,$i,"image"));
                    $image = strrev($image);
                    $image = strstr($image, '.');
                    $image = strrev($image);
                    array_push($listingsImageArray, "$image");
                }

            $maxImageId = max($listingsImageArray);


            IF ($maxImageId==null){
            $maxImageId=0;
            }

            $imageName = $maxImageId + 1;
            $upimage = $imageName.".jpg";
            }
            ELSE{
            $imageName = $oldImageName;
            $upimage = $imageName;
            }

        $uploadfile = $uploaddir.$upimage;
        $uploadTHfile = $uploadThDir."th_".$upimage;
        $uploadsize = $_FILES['listingsImage']['size'];

        /*== get file extension (fn at bottom of script) ==*/
        /*== checks to see if image file, if not do not allow upload ==*/
        $pext = getFileExtension($iName);
        $pext = strtolower($pext);

        if (($pext != "jpg")  && ($pext != "jpeg")) {
        print "<h1>ERROR</h1> The file you uploaded had the following extension: $pext.<br>";
        print "<p>Please upload an image with the extension .jpg or .jpeg ONLY.<br><br>";

        echo "<br /><br /><a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Listings Administration</span>";

        }
        elseif ($maxfilesize <= $uploadsize){
        echo "The image that you have uploaded is too big.  The system will accomodate any images up to 5MB and resize them to ensure optimal browsing.  If your file is larger than this, please resize the image.";
        echo "<br /><br /><a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Listings Administration</span>";
        }
        else{
        if (move_uploaded_file($_FILES['listingsImage']['tmp_name'], $uploadfile)) {

            // The file
            $filename = $uploadfile;
            $desiredwidth = 500;
            $desiredthumb_width = 100;

            // Get new dimensions
            list($width, $height) = getimagesize($filename);
            IF ($width > 500){
            $new_width = $desiredwidth;
            $new_height = $desiredwidth/$width * $height;
            }
            ELSE{
            $new_width = $width;
            $new_height = $height;
            }

            IF ($width > 100){
            $new_thumb_width = $desiredthumb_width;
            $new_thumb_height = $desiredthumb_width/$width * $height;
            }
            ELSE{
            $new_thumb_width = $width;
            $new_thumb_height = $height;
            }

            // Resample
            $image_p = imagecreatetruecolor($new_width, $new_height);
            $image = imagecreatefromjpeg($filename);
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

            $image_t = imagecreatetruecolor($new_thumb_width, $new_thumb_height);
            $imagethumb = imagecreatefromjpeg($filename);
            imagecopyresampled($image_t, $imagethumb, 0, 0, 0, 0, $new_thumb_width, $new_thumb_height, $width, $height);

            // Output
            imagejpeg($image_p, "$uploadfile", 500);
            imagejpeg($image_t, "$uploadTHfile", 100);

            $query = "UPDATE listings SET town='$town', street='$street', listings='$listings', date='$date', mls='$mls', price='$price', adj='$adj', special='$special', bed='$bed', bath='$bath', age='$age', aid='$aid', image='$upimage' WHERE nid='$nid'";
            $result = mysql_query($query, $connection) or die("Query failed : " . mysql_error());
            echo "Thank you, the listings item has been modified."
                ."<br/><br/>";
            echo "<a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Listings Administration</span>";
            } 
            else {
                echo "File upload is not valid.  Check permissions or contact the system administrator.";
                echo "<br /><br />";
                echo "<a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Administration</span>";
            }
        }   
        }

        else{

            if ($remImage=="1"){

            IF ($oldImageName!=""){
            $imagePath = $uploaddir . $oldImageName;
            $imageTHPath = $uploadThDir . "th_".$oldImageName;
            chmod($imagePath, 0755);
            chmod($imageTHPath, 0755);
            unlink($imagePath);
            unlink($imageTHPath);
            }
            $image="";
            $query = "UPDATE listings SET town='$town', street='$street', listings='$listings', date='$date', mls='$mls', price='$price', adj='$adj', special='$special', bed='$bed', bath='$bath', age='$age', aid='$aid', image='$image' WHERE nid='$nid'";
            $result = mysql_query($query, $connection) or die("Query failed : " . mysql_error());
            echo "Thank you, the discovery has been modified."
                ."<br/><br/>";
            echo "<a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Listings Administration</span>";
            }
            ELSE{
            $query = "UPDATE listings SET town='$town', street='$street', listings='$listings', date='$date', mls='$mls', price='$price', adj='$adj', special='$special', bed='$bed', bath='$bath', age='$age', aid='$aid' WHERE nid='$nid'";
            $result = mysql_query($query, $connection) or die("Query failed : " . mysql_error());
            echo "Thank you, the listings item has been modified."
                ."<br/><br/>";
            echo "<a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Listings Administration</span>";
            }
        }
    }

    ELSE{

        //code to display the editor which replaces the textarea.
          echo "<script type=\"text/javascript\" src=\""
          ."$docroot"
          ."editor/fckeditor.js\"></script>"
        ."<script type=\"text/javascript\">"
        ."window.onload = function()"
        ."{"
        ."var oFCKeditor = new FCKeditor( 'listings','750','250','','' ) ;"
        ."oFCKeditor.BasePath   = '"
         ."$docroot"
         ."editor/' ;"
        ."oFCKeditor.ReplaceTextarea() ;"
        ."}"
        ."</script>";
        //editor code end

        $query = "SELECT * FROM listings WHERE nid='$nid'";
        $result = mysql_query($query, $connection) or die("Query failed : " . mysql_error());

        $town = stripslashes(mysql_result($result,0,"town"));
        $street = stripslashes(mysql_result($result,0,"street"));
        $listings = stripslashes(mysql_result($result,0,"listings"));
            $mls=stripslashes(mysql_result($result,0,"mls"));
$price=stripslashes(mysql_result($result,0,"price"));
$adj=stripslashes(mysql_result($result,0,"adj"));
$special=stripslashes(mysql_result($result,0,"special"));
$bed=stripslashes(mysql_result($result,0,"bed"));
$bath=stripslashes(mysql_result($result,0,"bath"));
$age=stripslashes(mysql_result($result,0,"age"));
$aid=stripslashes(mysql_result($result,0,"aid"));
        $date = stripslashes(mysql_result($result,0,"date"));

        $image = mysql_result($result,0,"image");

        echo "<form action=\"default.php?$subUrl&do=editListings&page=submit&nid=$nid\" method=\"post\" name=\"listingsform\" enctype=\"multipart/form-data\">"
        ."<table width=\"100%\">"
        ."<tr><td width=\"80\"><strong>Date:</strong></td><td><input type=\"text\" name=\"date\" value=\"$date\" size=\"11\"  class=\"dateForm\" READONLY>"

        ."<a href=\"javascript:void(0)\" onclick=\"if(self.gfPop)gfPop.fPopCalendar(document.listingsform.date);return false;\" HIDEFOCUS><img class=\"PopcalTrigger\" align=\"absmiddle\" src=\""
        ."$docroot"
        ."includes/cal/calbtn.gif\" width=\"34\" height=\"22\" border=\"0\" alt=\"\"></a>"

            ."<tr><td width=\"80\"><strong>Community:</strong></td><td><input type=\"town\" name=\"town\" size=\"30\" value=\"$town\"></td></tr>"
        ."<tr><td width=\"80\"><strong>Street Address:</strong></td><td><input type=\"text\" name=\"street\" size=\"40\" value=\"$street\"></td></tr>"
                ."<tr><td width=\"80\"><strong>MLS:</strong></td><td><input type=\"text\" name=\"mls\" size=\"40\" value=\"$mls\"></td></tr>"
                        ."<tr><td width=\"80\"><strong>Price:</strong></td><td><input type=\"text\" name=\"price\" size=\"40\" value=\"$price\"></td></tr>"
                        ."<tr><td width=\"80\"><strong>Price Adjustments:</strong></td><td><input type=\"text\" name=\"adj\" size=\"40\" value=\"$adj\"></td></tr>"
                        ."<tr><td width=\"80\"><strong>special Information:</strong></td><td><input type=\"text\" name=\"special\" size=\"100\" value=\"$special\"></td></tr>"
                                ."<tr><td width=\"80\"><strong>Bedrooms:</strong></td><td><input type=\"text\" name=\"bed\" size=\"40\" value=\"$bed\"></td></tr>"
                        ."<tr><td width=\"80\"><strong>Bathrooms:</strong></td><td><input type=\"text\" name=\"bath\" size=\"40\" value=\"$bath\"></td></tr>"                
                                        ."<tr><td width=\"80\"><strong>Age:</strong></td><td><input type=\"text\" name=\"age\" size=\"40\" value=\"$age\"></td></tr>"
        ."<tr><td width=\"120\" valign=\"top\"><strong>Current Image: </strong></td><td>";
        IF ($image!=""){
        echo "<img src=" . $uploadThDir . "th_" . $image . " border=0></td></tr>";
        }
        ELSE{
        echo "No image has been added.";
        }
        IF ($image!=""){
        echo "<tr><td width=170><b>Remove Current Image:</b></td><td><input type=checkbox name=\"remImage\" value=\"1\"></td></tr>";}
        IF ($image!=""){
         $imgText = "Replace Image:";
        }  
        ELSE{
         $imgText = "Add Image:"; 
        }
        echo "<tr><td width=\"130\"><strong>$imgText </strong></td><td><input name=\"listingsImage\" type=\"file\" /></td></tr>"
        ."<tr><td colspan=\"2\" height=\"10\"></td></tr>"
        ."<tr><td width=\"80\"><strong>Please Select an Album:</strong></td><td>"
            ."<select name=\"aid\" size=\"1\">"
            ."<option selected";
                if (($albumPost!=null)||($albumPost!="")){
                echo "value=\"$albumPost\">";
                $albumNameQuery = "SELECT * from galleryalbum where aid=$aid";
                $albumNameResult = mysql_query($albumNameQuery, $connection) or die("Query failed : " . mysql_error());
                $albumName = mysql_result($albumNameResult,0,"aname");  
                echo stripslashes($albumName);
            }
            else{
                echo ">";
            }
             echo "</option>";

                $query = "SELECT * FROM galleryalbum";
                $result = mysql_query($query, $connection) or die("Query failed : " . mysql_error());
                $numrows = mysql_num_rows($result);

                for ($i = 0; $i < $numrows; $i++) {          
                    $aname = mysql_result($result,$i,"aname");
                    $aid = mysql_result($result,$i,"aid");

                    echo "<option value=\"$aid\">";
                    echo $aname;
                    echo "</option>";
                }
                echo "</select>";
            echo "</td></tr>"

        ."<tr><td colspan=\"2\"><strong>Description:</strong><br/><textarea name=\"listings\" rows=10\" cols=\"75\">$listings</textarea></td></tr>"
        ."<tr><td colspan=\"2\"><input name=\"Submit\" value=\"Submit\" type=\"submit\"></td></tr></table>"
        ."</form>";

        echo "<br/>";

        echo "<a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Listings Administration</span>";

    }

    echo "<iframe width=174 height=189 name=\"gToday:normal:agenda.js\" id=\"gToday:normal:agenda.js\" src=\""
    ."$docroot"
    ."includes/cal/ipopeng.htm\" scrolling=\"no\" frameborder=\"0\" style=\"visibility:visible; z-index:999; position:absolute; left:-500px; top:0px;\"></iframe>";

}

``

Recommended Answers

All 2 Replies

That's not very readable, but it looks like you are setting selected for every <option>, instead of just for the one that should be selected.

Where are the following variables set? $albumPost and $aid ? If your var_dump them do you get the expected result? I'm guessing one of those is not set properly and your IF or query statement is failing.

On a side note, it's very bad practice to put large groups of HTML code inside echo statements. It's okay if it's just a couple small tags, but when you have large groups of code (like line 202-219), you sould put a PHP end tag, write your HTML code, then put a PHP start tag and continue with your code. Not only does it avoid having to have the escape character everywhere which is very easy to miss and will make your entire code fail (Used 219 times in this code you posted, and that's not even the entire code you posted), but is a lot easier to read and debug.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.