RD Ward
RD Ward

Reputation: 6737

Comparison if HTML / PHP string values with spaces

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;
    }

Upvotes: 0

Views: 843

Answers (1)

ridgerunner
ridgerunner

Reputation: 34425

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;.

Upvotes: 2

Related Questions