PHP Web Host - Quality Web Hosting For All PHP Applications Sign up for PayPal and start accepting credit card payments instantly
  Login or Register
 • Home • Downloads • Your Account • Forums • 

View next topic
View previous topic


Google
 
Web RavenPHPScripts (This Site)
Post new topic   Reply to topic
Author Message
Donovan
Client


Joined: Oct 07, 2003
Posts: 675
Location: Ohio

PostPosted: Fri Dec 14, 2007 2:38 pm Reply with quote Back to top

I tried this code to help me trim a " from a Last_Name field. It is not working.
Code:

$stripname = $db->sql_query("UPDATE ".$prefix."$table_name SET Name_Last = TRIM(LEADING '\"' FROM 'Name_Last'");


I also tried a ltrim but could not get that working either.

The field looks like this.

Quote:

"ABRAHAM
"BAXTER
"COOPER


The Name_First looks like this
Quote:

COREY "
ROBYN A"
NICHOL W"


Yes you can imagine what the field looked like from the csv file

Quote:

"COOPER, ROBYN A"


I split it on the comma during the import

Code:

FIELDS TERMINATED BY ','


But now I can't get the " stripped off the name fields.

Any help is appreciated.
View user's profile Send private message Visit poster's website ICQ Number
evaders99
Moderator


Joined: Apr 30, 2004
Posts: 2762

PostPosted: Fri Dec 14, 2007 3:38 pm Reply with quote Back to top

Trim is designed to remove spaces. You'll need to use the function MID to return a valid substring of the entire string.
Only registered users can see links on this board!
Get registered or login to the forums!


During import, can't you also set a "fields surrounded by" parameter? I thought there was something like that so it would automatically take out quotes
View user's profile Send private message Visit poster's website
Donovan
Client


Joined: Oct 07, 2003
Posts: 675
Location: Ohio

PostPosted: Fri Dec 14, 2007 4:50 pm Reply with quote Back to top

There is a fields enclosed by but the data looked like "Smith, Joe E" and I wanted to break the last name and first name apart.
View user's profile Send private message Visit poster's website ICQ Number
Gremmie
Moderator


Joined: Apr 06, 2006
Posts: 2374
Location: Iowa, USA

PostPosted: Fri Dec 14, 2007 7:01 pm Reply with quote Back to top

This should get you started....(warning not tested)

Code:

$line = '"Smith, Joe"';
$s = preg_replace('/"(\w+),\s+(\w+)"/', '$1,$2', $line);
list($last, $first) = explode(',', $s);


The preg_replace strips off the leading and trailing double quotes, leaving you with last name comma first name. Then you use an explode to split that into two parts based on the comma.

You'll have to beef up the regular expression if there is an optional middle initial or something.

References:
Only registered users can see links on this board!
Get registered or login to the forums!
Only registered users can see links on this board!
Get registered or login to the forums!
View user's profile Send private message
Donovan
Client


Joined: Oct 07, 2003
Posts: 675
Location: Ohio

PostPosted: Mon Dec 17, 2007 2:34 pm Reply with quote Back to top

I am trying to write these valued back to the table but can't seem to get it functioning correctly.

Code:
//Strip the " from imported students name.
$altertable = $db->sql_query("ALTER TABLE ".$prefix."$table_name ADD Name_First varchar(30) NULL AFTER ImportedName");
$altertable = $db->sql_query("ALTER TABLE ".$prefix."$table_name ADD Name_Last varchar(50) NULL AFTER Name_First");
$sql = $db->sql_query("SELECT * FROM ".$prefix."$table_name WHERE ImportedName IS NOT NULL");
while ($row = $db->sql_fetchrow($sql)) {            
      $Name = $row['ImportedName'];
$s = preg_replace('/"(\w+),\s+(\w+)"/', '$1,$2', $Name);
list($Fixed_Name_Last, $Fixed_Name_First) = explode(',', $s);
}
$fixname = $db->sql_query("UPDATE ".$prefix."$table_name SET Name_Last = '$Fixed_Name_Last', Name_First = '$Fixed_Name_First' WHERE Session_ID ='$Session_ID'");


...it is writing the last value in the query to all records.

For First_Name I have ELI
For Last_Name I have ZARKHIN

these are written for every record.

I figured if I had this
Code:
WHERE Session_ID ='$Session_ID'
it would write each corrosponding values.
View user's profile Send private message Visit poster's website ICQ Number
fkelly
Moderator


Joined: Aug 30, 2005
Posts: 2064
Location: near Albany NY

PostPosted: Mon Dec 17, 2007 8:59 pm Reply with quote Back to top

Sorry it is late and I don't have the energy to simulate this. In that last query are you sure that '$Fixed_Name_Last' is not being interpreted literally and that you don't need to escape the ' mark with a \ ? I usually prefer the two step method of putting the SQL statement into a variable (e.g., $sql = ("UPDATE ".$prefix."$table_name SET Name_Last = '$Fixed_Name_Last', Name_First = '$Fixed_Name_First' WHERE Session_ID ='$Session_ID'") and then executing that separately. That way for diagnostic purposes I can echo out the SQL I've created and see exactly how it is being interpreted. I'd also put in a check that says if (!fixname) and then echo out any mysql errors.

Practice defensive programming from the outset and check for errors as you go, echo things to make sure they are coming out the way you expect and you will find that things go a lot smoother.
View user's profile Send private message Visit poster's website
Display posts from previous:       
Post new topic   Reply to topic

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Forums ©
 

All logos and trademarks in this site are property of their respective owner.
The comments are property of their posters, all the rest © 2002-2008 by Raven
Proud to be listed at Lobo Links Web Directory

You can syndicate our news using the file xml

CSE HTML Validator Helped Clean up This Page! [Valid RSS] valid RSS 2.0 Valid robots.txt Stop Spam Harvesters, Join Project Honey Pot

Website engines core code is © copyright by PHP-Nuke but has been heavily patched and modified by myself and others.
PHP-Nuke is a free software released under the GNU/GPL.


:: fisubice phpbb2 style by Daz :: PHP-Nuke theme by www.nukemods.com ::

:: fisubice Theme Recoded To 100% W3C CSS & HTML 4.01 Transitional Compliance by Raven and 64bitguy ::

:: W3C CSS Compliance Validation :: W3C HTML 4.01 Transitional Compliance Validation ::

zerosum