GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Mar-2004, 12:12
Kurtanator Kurtanator is offline
New Member
 
Join Date: Feb 2004
Posts: 5
Kurtanator is on a distinguished road

Removing the last comma in a string


hi,

i'm trying to use the left() function to remove an additional comma at the end of a string but it won't parse. Here is a section of code that is trying to pull column names and then gets placed into an Insert statement.

$get_columns=$new_db->query("explain grades");

while($row=$new_db->fetch_row($get_columns))
{
//an index to loop through the column names
$i=0;
$column_name=$row[$i];

//create a table to list all of the current columns
$names .="$column_name,";

//increment index to go to the next column name
$i++;
}

$msg=left($names, len($names)-1);
$newmsg="Insert into grades("$msg")...........";

Is there something that has to be included so the parser knows what the left function is and is there maybe a better way to remove the last comma?


I would appreciate any help,
KurtM
  #2  
Old 09-Mar-2004, 13:53
erniegerdie erniegerdie is offline
Junior Member
 
Join Date: Feb 2004
Location: England
Posts: 71
erniegerdie will become famous soon enough
Quote:
Originally Posted by Kurtanator
$msg=left($names, len($names)-1);
$newmsg="Insert into grades("$msg")...........";

I think your problem is the line:

$newmsg="Insert into grades("$msg")...........";

Try
PHP Code:

$newmsg="Insert into grades('$msg')...........";
or 
$newmsg="Insert into grades(\"$msg\")..........."; 



The speech marks are the start and stop markers for you string. If you want to use one inside a string you have to place the backslash infront of them to tell the parser not to see them as actual code.

Hope that helps
  #3  
Old 10-Mar-2004, 14:58
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Quote:
Originally Posted by Kurtanator
...is there maybe a better way to remove the last comma?
..

Hello Kurt,

Sorry I haven't had the time to go through your issues in the PHP forum recently but I can quickly post an alternative solution you can try:

use the rtrim() function to remove the last comma... like this:

PHP Code:

<?php
//....
//create a table to list all of the current columns
$names .="$column_name,";
}
// now removing the comma
$names = rtrim( $names, ',' );
?>

 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
AdSonar spider / bot useragent string JdS Advertising & Affiliates Forum 1 10-Mar-2006 16:05
Including Maps and strings?? maddie C++ Forum 17 05-Jul-2004 06:25
Problem putting variables cumulatively into a string, warny_maelstrom C Programming Language 3 05-Feb-2004 09:29
[function] AutoLink (converts URLS into links inside a string) JdS PHP Code Library 0 26-Jan-2004 05:02
storing a token pointer as a string CoreLEx C Programming Language 1 07-Oct-2003 11:33

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:38.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.