Results 26 to 50 of 65
Thread: Get rof of the last comma in PHP
-
07-22-2007, 04:42 AM #26Web Hosting Evangelist
- Join Date
- Mar 2004
- Location
- New Zealand
- Posts
- 532
It sounds like you are looking at this the wrong way. Ask not how you can remove the extra comma, ask how you can not add it in the first place.
-
07-22-2007, 06:35 AM #27Web Hosting Master
- Join Date
- Sep 2005
- Location
- India
- Posts
- 778
Here is the code that will work for you:
PHP Code:$sql = 'CREATE table ...
ADD ... ,
ADD ... ,
ADD ... ,
) TYPE=...';
$pos = strrpos($sql, ',');
$query = substr($sql, 0, $pos).substr($sql, $pos+1);
echo $query;
DarshWebSolutions.com : Web Design, PHP Development, E-Commerce Solutions
PDF-ace.com : HTML to PDF API
-
07-22-2007, 04:26 PM #28Web Hosting Master
- Join Date
- Mar 2006
- Posts
- 984
I wasn't aware my topic would have so much postings ...
Althought, the above post from Jatinder would not work from a loop since it would remove all the commas. I already tried that before posting. Any other solutions for loops (or which of the above solution would work with the loop) ?
-
07-22-2007, 08:15 PM #29Retired Moderator
- Join Date
- Feb 2005
- Location
- Australia
- Posts
- 5,849
You got the answer at post #8 - inside the loop you add your terms to an array. Then outside the loop join / implode the array with commas to create your sql statement.
Chris
"Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter
-
07-22-2007, 09:02 PM #30Web Hosting Master
- Join Date
- Mar 2006
- Posts
- 984
Sorry. I tried the post #8 solution and the commas are being eliminated all at once. I only need the last comma to be removed from the paragraph.
-
07-22-2007, 09:23 PM #31Retired Moderator
- Join Date
- Feb 2005
- Location
- Australia
- Posts
- 5,849
If you use that method you don't need to remove the last comma because it's never added. Post your code - I think you're probably missing the point.
Chris
"Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter
-
07-22-2007, 09:42 PM #32Web Hosting Master
- Join Date
- Mar 2006
- Posts
- 984
If you use that method you don't need to remove the last comma because it's never added.
-
07-22-2007, 09:57 PM #33Retired Moderator
- Join Date
- Feb 2005
- Location
- Australia
- Posts
- 5,849
-
07-25-2007, 02:48 AM #34Registered User
- Join Date
- Feb 2003
- Posts
- 433
Try this:
PHP Code:$string = preg_replace("/, $/i", '', $string);
-
07-25-2007, 12:29 PM #35WHT Addict
- Join Date
- Apr 2004
- Posts
- 167
try:
PHP Code:$s = "1,2,3,";
$s = substr($s, 0, -1);
echo $s;
PHP Code:$rest = substr("abcdef", 0, -1); // returns "abcde"
-
07-25-2007, 09:36 PM #36Web Hosting Master
- Join Date
- Mar 2006
- Posts
- 984
-
07-26-2007, 01:59 PM #37Junior Guru Wannabe
- Join Date
- Mar 2002
- Posts
- 36
-
07-26-2007, 02:02 PM #38Web Hosting Master
- Join Date
- Mar 2006
- Posts
- 984
Unfortunitely, I cannot put it outside the loop since the ) TYPE=MyISAM is the last line of the paragraph and also needs to be in the loop as it does not contain any commas. The line before the last is the target.
-
07-26-2007, 03:07 PM #39Hail Eris !
- Join Date
- Oct 2002
- Location
- Canada
- Posts
- 3,103
After reading 3 pages i still do not see why that comma is there.
PHP Code:$tableName = 'some_name' ;
$tableFields = array () ;
/*
it seems you have some kind of loop that generates your fields
if it is something like this:
$fields = array (
'field1'=>array ( 'int(11)', 'NOT NULL', 'auto_increment'),
'field3'=>array ( 'varchar(255)', 'NOT NULL', 'default \'\''),
...
)
$tableProps = array (
'ENGINE=MyISAM' ,
'DEFAULT CHARSET=utf8',
...
);
you could try :
*/
forach ($fields as $fild=>$fieldData) {
$tableFields[] = " ADD `$field` " . join (" " , $fieldData) ;
}
$query="CREATE table `$tableName`
". join (" , " ,$tableFields ) ."
) ". join (" " , $tableProps ) ." ";
-
07-26-2007, 03:13 PM #40Junior Guru Wannabe
- Join Date
- Mar 2002
- Posts
- 36
PHP Code:$sql = 'CREATE table ... '; // first part of string
while (..)
{
$sql .= 'ADD ... ,'; // add comma separated fields
}
$sql = substr($sql,0,-1); // remove last comma
$sql .= ') TYPE=...'; // add type declaration
echo $sql;
-
07-26-2007, 03:18 PM #41Web Hosting Master
- Join Date
- Mar 2006
- Posts
- 984
See ? Like the above; a single while loop. I need to remove the last comma under the third while loop (since there are three in total).
PHP Code:while {
...
while {
...
while {
...
}
}
remove the last comma here.
}
-
07-26-2007, 05:20 PM #42Junior Guru Wannabe
- Join Date
- Nov 2005
- Location
- Tampa, Florida
- Posts
- 60
Here's how I manage that:
GIVEN:
Fruit Table contains 3 rows: apple, pear, banana
$query=mysql_Query("SELECT * FROM FRUIT");
while($row=mysql_fetch_array($query))
{
$cnt++;
if($cnt!=1) echo ',';
echo $row[FruitType];
}
would result in:
apple, pear, banana
Hope this helps..Max Rathbone - Systems Admin. - Sago Networks
http://www.sagonet.com
Click 'Live Support' on the website to have your questions answered!
-
07-26-2007, 05:50 PM #43Web Hosting Master
- Join Date
- Mar 2006
- Posts
- 984
For that, you'd need to know the number of times that the $cnt (counter) will be repeated inside the while loop. In your case, you stated the value as 1 (not equal to ...). Although, what if you don't know the number of fields that needs to be outputed ?
-
07-26-2007, 06:11 PM #44Junior Guru Wannabe
- Join Date
- Nov 2005
- Location
- Tampa, Florida
- Posts
- 60
I think you may be confused as to the operation of that code..There is no need to know the number of items...
The first time it runs... cnt is made to equal 1. For this reason, no comma is placed before the mysql item is echo'd. Every time after that, cnt is not equal to 1, so a comma is placed before the item is echo'd. It will only place a comma BEFORE each item, so with this code it is not possible to accidentally end up with a comma at the end of the line.
It's comma management...simplicity at its finest. lol
To demonstrate this, I converted the same code to use an array instead of mySQL. You can find the working code and .txt here:
http://www.shouden.us/comma/index.php
http://www.shouden.us/comma/index.txt
Hope this clears up any confusion.Max Rathbone - Systems Admin. - Sago Networks
http://www.sagonet.com
Click 'Live Support' on the website to have your questions answered!
-
07-26-2007, 06:51 PM #45Retired Moderator
- Join Date
- Feb 2005
- Location
- Australia
- Posts
- 5,849
horizon, just about any of the suggestions in this huge thread will work if used properly. Since your program still doesn't work the problem must be in your logic (the loop structure) and since you won't post the actual code there's not much chance of fixing that, but I'll give it one last try:
Perhaps this is where your logic is going wrong. If you're constructing a single sql statement finishing with "TYPE=MyISAM" you do NOT need that inside a loop!Chris
"Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter
-
07-26-2007, 07:22 PM #46Web Hosting Master
- Join Date
- Mar 2006
- Posts
- 984
you do NOT need that inside a loop!
The first time it runs... cnt is made to equal 1. For this reason, no comma is placed before the mysql item is echo'd. Every time after that, cnt is not equal to 1, so a comma is placed before the item is echo'd. It will only place a comma BEFORE each item, so with this code it is not possible to accidentally end up with a comma at the end of the line.
-
07-26-2007, 08:39 PM #47Junior Guru Wannabe
- Join Date
- Nov 2005
- Location
- Tampa, Florida
- Posts
- 60
Let's say you wanted to query two tables..Back to my original example, here's a simple way you could do it:
GIVEN:
Fruit Table contains 3 rows: apple, pear, banana
Animal Table contains 3 rows: tiger, cat, mouse
$queryA=mysql_Query("SELECT * FROM FRUIT");
while($row=mysql_fetch_array($queryA))
{
$cnt++;
if($cnt!=1) echo ', ';
echo $row[FruitType];
}
$queryB=mysql_Query("SELECT * FROM ANIMAL");
while($row=mysql_fetch_array($queryB))
{
$cnt++;
if($cnt!=1) echo ', ';
echo $row[AnimalType];
}
would result in:
apple, pear, banana, tiger, cat, mouse
that would be querying two tables.
If this isnt quite what you are trying to do, perhaps you could elaborate by providing an example of what you are trying to do?Max Rathbone - Systems Admin. - Sago Networks
http://www.sagonet.com
Click 'Live Support' on the website to have your questions answered!
-
07-26-2007, 08:42 PM #48Junior Guru Wannabe
- Join Date
- Nov 2005
- Location
- Tampa, Florida
- Posts
- 60
Generally speaking with programming, there are several different ways you can accomplish any given task. This thread is proof of this theory. Several possible solutions have been provided to you.
If you are having difficulty making your code work, perhaps you should take a step back from it and look at the overall view of what you are trying to do and see if you can come up with another way to do it.
I personally do this after I've banged my head on the monitor/keyboard enough times trying to solve a troublesome problem. In most cases, I end up writing whole sections of code over, but what I end up with is usually superior to what I had in the first place.Max Rathbone - Systems Admin. - Sago Networks
http://www.sagonet.com
Click 'Live Support' on the website to have your questions answered!
-
07-26-2007, 08:54 PM #49Web Hosting Master
- Join Date
- Mar 2006
- Posts
- 984
If this isnt quite what you are trying to do, perhaps you could elaborate by providing an example of what you are trying to do?
2 - I'm trying to gather all indexed fields from each SQL tables by selecting each of them.
3 - I'm trying to gather all the fields type (int, char, varchar etc ...) with it's value.
4 - I'm trying to gather each of these indexed fields - seeing if they are NULL, NOT NULL
5 - I'm trying to gather each tables info containing a PRIMARY KEY or INDEX fields.
6 - In the end, the ) TYPE line (ending).
7 - Once all these infos has been gathered, I wish to write them on an SQL file.
8 - Once the SQL file has been created, I'd like an download header so that the user could save the file.
I'm trying to create my own mySQL backup file without depending on mysqldump since some web hosting company uses restrictions towards their customers.
Hope this is more elaborated now.
-
07-26-2007, 09:15 PM #50Junior Guru Wannabe
- Join Date
- Nov 2005
- Location
- Tampa, Florida
- Posts
- 60
Alright. wow. What you are asking here is a serious undertaking. I'd definitely classify this as a project as opposed to a task.
It would be very difficult to try and code all the intricacies that a potential database could have. While not impossible, it will take a lot of research into the mySQL docs in order to create code that handles all possibilities.
With that said, perhaps there is some other way that you could accomplish the same thing? I'm the Senior Systems Administrator at Sago Networks. We have multiple shared servers which would potentially put many customers into the same situation as you, not being able to perform a system/exec call through PHP. Perhaps your provider would be willing to set up a nightly cronjob for you that backs up specific databases of yours and drops them in your home directory? I would if one of my customers asked. It's not hard to see that the restrictions they placed on the server(to protect it and the customers on it) have hindered your ability to do what you need to do. If they aren't willing to do that for you, you might consider finding another provider?
Another possible solution would be an app that runs on a Windows PC, perhaps at home. There are many, many apps out there that will do remote backups of SQL data. It would be of little consequence to set up your windows scheduler to start this app(if it doesnt do scheduling natively) to perform a backup of your SQL data.
It seems to me that you can probably take another approach to this problem that would save you countless hours programming. However if you are intent on continuing this programming project, please let me know what you have accomplished so far, and where you are hung up on so I can try and provide some help without having to go through the entire methodology.Max Rathbone - Systems Admin. - Sago Networks
http://www.sagonet.com
Click 'Live Support' on the website to have your questions answered!