jmac2501 Posted May 30, 2007 Share Posted May 30, 2007 i have a text area where i input date at the end of each line of date there is a space that i can't have in there, so i tryed using $var = trim('$var'); but this deleted all but the last line of data. so instead of getting 4 lines of data i get 1. Is there a better way to trim? input = 111111 222222 333333 444444 <?php error_reporting(E_ALL ^ E_NOTICE); if(isset($_POST['DOREO']) && $_POST['DOREO'] == "Get Data") { $con_arr = explode("\n",$_POST['reonum']); print_r ($con_arr); foreach($con_arr as $key => &$val) $var = trim('$var'); { $link = mysql_connect('*******', '******', '*******') or die('Could not connect: ' . mysql_error()); mysql_select_db('*******') or die('Could not select database'); $sql = "INSERT INTO $_POST[Listings] (`reo`) VALUES ('$var')"; echo ("<br />"); echo("$sql"); if(mysql_query($sql)) { echo "Success!"; } else{ echo("FAILED TO INPUT DATA - ".mysql_error()); } } } ?> output = 444444 Link to comment https://forums.phpfreaks.com/topic/53577-solved-trim-funtion-not-working-right/ Share on other sites More sharing options...
trq Posted May 30, 2007 Share Posted May 30, 2007 $var = trim($var); Link to comment https://forums.phpfreaks.com/topic/53577-solved-trim-funtion-not-working-right/#findComment-264792 Share on other sites More sharing options...
jmac2501 Posted May 30, 2007 Author Share Posted May 30, 2007 $var = trim($var); does the same thing as $var = trim('$var'); it trims all the data down to the last line... :'( Link to comment https://forums.phpfreaks.com/topic/53577-solved-trim-funtion-not-working-right/#findComment-264796 Share on other sites More sharing options...
per1os Posted May 30, 2007 Share Posted May 30, 2007 To elaborate why Thorpe is correct and you are not: Thorpes code $var = trim($var); Does not encapsulate the variable in single or double quotes as it is not needed although double quotes would work, it is just a waste of time to do that. Your Code: $var = trim('$var'); Does not work because any variable inside a single quote is taken literally. IE you are trimming $var, not 11111 22222 etc. Since the single quotes do not understand fully the $var as double quotes do. Here is a test you can do to see what I mean <?php $var = "Hello"; echo 'Test1: $var <br />'; echo "Test2: $var <br />"; ?> Hope that helps. EDIT: Just noticed, where you put your $var trim line is inside the foreach BEFORE the { Put it after the first { for the foreach as that is the proper syntax. IE: <?php error_reporting(E_ALL ^ E_NOTICE); if(isset($_POST['DOREO']) && $_POST['DOREO'] == "Get Data") { $con_arr = explode("\n",$_POST['reonum']); print_r ($con_arr); $var = trim($var); // here OR foreach($con_arr as $key => &$val) { $var = trim($var); // here whichever you need. $link = mysql_connect('*******', '******', '*******') or die('Could not connect: ' . mysql_error()); mysql_select_db('*******') or die('Could not select database'); $sql = "INSERT INTO $_POST[Listings] (`reo`) VALUES ('$var')"; echo ("<br />"); echo("$sql"); if(mysql_query($sql)) { echo "Success!"; } else{ echo("FAILED TO INPUT DATA - ".mysql_error()); } } } ?> Link to comment https://forums.phpfreaks.com/topic/53577-solved-trim-funtion-not-working-right/#findComment-264802 Share on other sites More sharing options...
jmac2501 Posted May 30, 2007 Author Share Posted May 30, 2007 ok, I get it now but i am still having the same problem with it. I have tryed $var = trim($var); , $var = trim("$var"); , and $var = trim('$var'); it still deletes all but the last line... :-\ Link to comment https://forums.phpfreaks.com/topic/53577-solved-trim-funtion-not-working-right/#findComment-264810 Share on other sites More sharing options...
trq Posted May 30, 2007 Share Posted May 30, 2007 Post your current code if your still having problems. Link to comment https://forums.phpfreaks.com/topic/53577-solved-trim-funtion-not-working-right/#findComment-264813 Share on other sites More sharing options...
per1os Posted May 30, 2007 Share Posted May 30, 2007 Also another note, do you mean $var or $val, I do not see where $var is defined anywhere..... Link to comment https://forums.phpfreaks.com/topic/53577-solved-trim-funtion-not-working-right/#findComment-264816 Share on other sites More sharing options...
jmac2501 Posted May 30, 2007 Author Share Posted May 30, 2007 ok still no go... lol and it was a typing error on here i ment $val here is what i have. Input = 70042455 70038135 70033436 70025279 70030945 70054751 70035037 70028187 70025593 70024145 70003411 70025609 70036908 70009094 70025619 70024059 70037183 70047389 70028189 <?php error_reporting(E_ALL ^ E_NOTICE); if(isset($_POST['DOREO']) && $_POST['DOREO'] == "Get Data") { $con_arr = explode("\n",$_POST['reonum']); print_r ($con_arr); foreach($con_arr as $key => $val) { $val = trim($val); $link = mysql_connect('*******', '******', '*******') or die('Could not connect: ' . mysql_error()); mysql_select_db('*******') or die('Could not select database'); $sql = "INSERT INTO $_POST[Listings] (`reo`) VALUES ('$var')"; echo ("<br />"); echo("$sql"); if(mysql_query($sql)) { echo "Success!"; } else{ echo("FAILED TO INPUT DATA - ".mysql_error()); } } } ?> <html> Here is the output Array ( [0] => 70042455 [1] => 70038135 [2] => 70033436 [3] => 70025279 [4] => 70030945 [5] => 70054751 [6] => 70035037 [7] => 70028187 [8] => 70025593 [9] => 70024145 [10] => 70003411 [11] => 70025609 [12] => 70036908 [13] => 70009094 [14] => 70025619 [15] => 70024059 [16] => 70037183 [17] => 70047389 [18] => 70028189 ) INSERT INTO listings5 (`streetaddress`, `city`, `price`, `squarefeet`, `bed`, `Fbath`, `Hbath`, `reo`, `agent`, `pic`) VALUES ('','','','','','','','70028189','','')Success! Link to comment https://forums.phpfreaks.com/topic/53577-solved-trim-funtion-not-working-right/#findComment-264836 Share on other sites More sharing options...
trq Posted May 30, 2007 Share Posted May 30, 2007 What is the problem now? Your still using $var in your query. Link to comment https://forums.phpfreaks.com/topic/53577-solved-trim-funtion-not-working-right/#findComment-264842 Share on other sites More sharing options...
per1os Posted May 30, 2007 Share Posted May 30, 2007 You need to remove the first $val = trim($val) inbetween the foreach and the { whether this is effecting it I am not sure. <?php error_reporting(E_ALL ^ E_NOTICE); if(isset($_POST['DOREO']) && $_POST['DOREO'] == "Get Data") { $con_arr = explode("\n",$_POST['reonum']); print_r ($con_arr); // move the connection out of the foreach, you are trying to connect to the DB like 10 times, that will kill your server. $link = mysql_connect('*******', '******', '*******') or die('Could not connect: ' . mysql_error()); mysql_select_db('*******') or die('Could not select database'); foreach($con_arr as $key => $var) { $var = trim($var); // renamed it to $val instead of $var and removed the other one above. $sql = "INSERT INTO $_POST[Listings] (`reo`) VALUES ('$var')"; echo ("<br />"); echo("$sql"); if(mysql_query($sql)) { echo "Success!"; }else{ echo("FAILED TO INPUT DATA - ".mysql_error()); } } } ?> <html> Try that out and see what happens. Link to comment https://forums.phpfreaks.com/topic/53577-solved-trim-funtion-not-working-right/#findComment-264843 Share on other sites More sharing options...
jmac2501 Posted May 30, 2007 Author Share Posted May 30, 2007 ok sorry i got it to work . Sorry for wasting your time but you were a GREAT help... Thanks alot Just needed to move the $var = trim($var); // here OR foreach($con_arr as $key => &$val) { $var = trim($var); // here whichever you need. like frosi said and change the $var to a $val... i feel dumb. thanks again Link to comment https://forums.phpfreaks.com/topic/53577-solved-trim-funtion-not-working-right/#findComment-264850 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.