Forum Moderators: coopster

Message Too Old, No Replies

Spaces in Strings

Dealing with spaces

         

calmseas

6:39 pm on Sep 20, 2010 (gmt 0)

10+ Year Member



I have the string "Tea For Nosps" (without the quotes)stored in a table column in a MySql DB. For some reason, when I retrieve this string, the only portion that displays is "Tea". So everything after the first space is being ignored. The string is being retrieved into $boat_col_value[$i] in the following code snippet. The same thing happens for all the strings in all the columns.
I would appreciate a suggestion as to how to retrieve the entire string.

$query = "SELECT * FROM boat WHERE certificate = '$certificate' " ;
$result = $db -> query( $query ) or die( "Error Number: " . mysqli_errno($db) . " :" . mysqli_error($db) ) ;

$row = $result->fetch_assoc() ;
$i = 0 ;
echo "<table>" ;

foreach ( $row as $value ) {
$boat_col_value[$i] = $value ;
if ( $i % 5 == 0) {
echo "</tr>" ;
echo "<tr>" ;
}
echo "<td>".$headers[$i]."<input type=\"text\" name= " .$boat_cols[$i]. " value = " .$boat_col_value[$i]. "></td>" ;
$i++ ;
}

Matthew1980

6:49 pm on Sep 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there calmseas,

echo "<td>".$headers[$i]."<input type=\"text\" name = ".$boat_col_value[$i]. " value = " .$boat_col_value[$i]. "></td>" ;


Whether I am right in that assumption or not remains to be seen, but I thought I would point to it!

Other than that have you tried the same query from the command line/mysql query browser or phpmyadmin to see what the output actually is?

Cheers,
MRb

johnblack

6:59 pm on Sep 20, 2010 (gmt 0)



Have you tried checking the html output using View Source?

I'm not sure but you may need quotes around the value attribute i.e.

replace

value = " .$boat_col_value[$i]. "></td>"

with

value = \"" .$boat_col_value[$i]. \""></td>"

May be the same with the name attribute too?

Sorry for the vagueness, 7am here and no coffee yet :)

Matthew1980

7:12 pm on Sep 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



@John black,

Lol, I spotted that but never thought to correct it - I only went and put the same text in my post and just referenced the array name, I think that means that it's beer o'clock!

Cheers,
MRb

johnblack

7:24 pm on Sep 20, 2010 (gmt 0)



@Matthew

Hee hee! A bit early for beer for me! I'll join ya in about 10 hours!


Yeah pretty certain its the quotes, finally got an example working (without the aid of coffee) and looks fine with quotes, and without just displays the non-space characters up to the first space in the string.

Let us know how you get on calmseas

Cheers John

calmseas

11:55 pm on Sep 20, 2010 (gmt 0)

10+ Year Member



Gentlemen -
I do declare I aspire to become a geek when I grow up. Your suggestion was excellent and did the trick. The only difference is I replaced your syntax: value = \"" .$boat_col_value[$i]. \""></td>"
with this syntax: value = \"" .$boat_col_value[$i]. "\"></td>"

I'm afraid I'll never fathom these devilish single and double quotes. Is it at all possible for someone to give me a simpleton, explanation as to what this escaping syntax is really doing?

In any case, thank you for your excellent suggestion and I hope you enjoyed your beer.

johnblack

1:22 am on Sep 21, 2010 (gmt 0)



Sorry about that - the slash was in the wrong place.

I'll try explaining the way I look at the escaping syntax, but be aware I'm no php expert!

If I see the slash I know the next charater is treated as a character and not as part of a php command. E.g. "value = \"" is a string with a double quote at the end

value = "

It's only ever the next character and it can precede itself

"value = \\" is a string

value = \

I hope that sort of makes sense and I'm sure someone can explain it better (and more fully) than me.

No beer yet, but my coffee was good!

calmseas

2:48 pm on Sep 21, 2010 (gmt 0)

10+ Year Member



Thank you for the short tutorial. It is one of my life's ambitions to master these quotes.......... :-)