0

I am having trouble comparing these two strings, I am not exactly sure what is happening, but when the values contain spaces, and the equality should be true, it is returning false, although it works flawlessly for strings without spaces.

I have a sneaky feeling there needs to be some preg for NBSP or something, but I am just utterly lost at what to do... The line I am referring to will have stars on it.

    function getRecords($column,$table){

        $options = "";

            if(isset($_POST['submit'])) {
                $selected = $_POST[$column];
            }       

        $query = "SELECT DISTINCT $column FROM $table ORDER BY $column ASC";
        $result = mysql_query($query);

        if(!$result) {
            $options = "<option>Error Retrieving Records</option>\n";;
        }

        else {
            while($row=mysql_fetch_assoc($result)) {
                $value = $row[$column];

                $options .= "<";
                    $options .= "option value=";
                        $options .= $value;

corrections here, thanks ridgerunner!

                    $options .= "option value=\""; 
                    $options .= $value."\"";

    //define selected value if it exists        
                    if(isset($selected)) {
***************         if($selected==$value) {
                            $options .= " selected";
                        }
                            $options .= "";
                    }
                $options .= ">";
                $options .= $value;
                $options .= "</option>\n";
            }
        }

        return $options;
    }
2
  • 1
    Print out the results of selected and value and you will easily be able to see where the discrepancy is. Commented May 1, 2011 at 16:38
  • Please provide the print_r() details of $value. Additionally, $selected is only defined, if $_POST['submit'] is set. Commented May 1, 2011 at 16:39

1 Answer 1

2

You need to enclose the OPTION attribute value in quotation marks. Instead of:

$options .= "option value=";

Use:

$options .= "option value=\"";

And do the same thing with the closing quote. And to be safe, you may also want to convert any double quotes within $value to &quot;.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh my God, you solved it! It looks a bit odd in my code... but it works!$options .= "option value=\""; $options .= $value."\"";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.