Jump to content

[SOLVED] Trim funtion not working right...


jmac2501

Recommended Posts

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

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());
}
}
}
?> 

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!

 

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.