tinks
tinks

Reputation: 3

Whitespace problem

I am getting data from a database and which is used to populate the select element (list) on my page.

The data stored with which it has two spaces in my DB: "ABC CDE".

When I put the value in the select list, it removes the other space, thus leaving it "ABC CDE". But every time I get the data and I log it, it still has two spaces.

The problem is that if I needlook up something in DB using the name does not return anything because of the difference in spaces.

$query = "SELECT * from Member.people";
 $logger->debug($query);
 $result = mysql_query($query);
 $num = mysql_num_rows($result);

 while ($row = mysql_fetch_array($result)) {
      $op = $row['Name'];                    //ABC  DE
      echo '<option>'.$op.'</option>';       //ABC DE

 }

can someone help?

Upvotes: 0

Views: 786

Answers (3)

Felix Kling
Felix Kling

Reputation: 816374

Consecutive white spaces in HTML are collapsed:

In particular, user agents should collapse input white space sequences when producing output inter-word space.

They are preserved in attributes though. You have to set the value attribute correctly and it will work:

echo '<option value="' . $op . '">'.$op.'</option>';

If you don't do that the content of the elements is used as value:

This attribute specifies the initial value of the control. If this attribute is not set, the initial value is set to the contents of the OPTION element.

what, as you already noticed, will give you the value with only one space (as it is collapsed).

Upvotes: 1

smcphill
smcphill

Reputation: 826

multiple spaces in HTML get normalised in the view. That's why I can do something like this:

<p>Hello         World</p>

yet display as "Hello World".

Do a string replace of " " to &nbsp; - that should fix your problem:

$op = $row['Name'].replace(/\s/g,'&nbsp;');  

Upvotes: 0

YODA
YODA

Reputation: 309

Maybe try to replace the space char to the escape string &nbsp;

Upvotes: 1

Related Questions