Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 65
  1. #26
    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.

  2. #27
    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($sql0$pos).substr($sql$pos+1);

    echo 
    $query
    DarshWebSolutions.com : Web Design, PHP Development, E-Commerce Solutions

    PDF-ace.com : HTML to PDF API

  3. #28
    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) ?

  4. #29
    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

  5. #30
    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.

  6. #31
    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

  7. #32
    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.
    And what if I have three loops to follow - all at the same time (while loop for the threes) ?

  8. #33
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,849
    Quote Originally Posted by horizon View Post
    And what if I have three loops to follow - all at the same time (while loop for the threes) ?
    That may make sense to you but it means nothing to me. Post the problem code if you want help.
    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

  9. #34
    Join Date
    Feb 2003
    Posts
    433
    Try this:

    PHP Code:
            $string preg_replace("/, $/i"''$string); 

  10. #35
    try:

    PHP Code:
    $s "1,2,3,";
    $s substr($s0, -1);
    echo 
    $s
    Got it form the PHP manual

    PHP Code:
    $rest substr("abcdef"0, -1);  // returns "abcde" 
    Powi

  11. #36
    Join Date
    Mar 2006
    Posts
    984
    Quote Originally Posted by Powi View Post
    try:

    PHP Code:
    $s "1,2,3,";
    $s substr($s0, -1);
    echo 
    $s
    Got it form the PHP manual

    PHP Code:
    $rest substr("abcdef"0, -1);  // returns "abcde" 
    Powi
    Already tried the substr like that. Since it's in a loop, it removes all the commas but thanks for your help anyway.

    PHP Code:
    $string preg_replace("/, $/i"''$string); 
    @Emil:
    Getting more interesting. Didn't tried this yet. Will let you know of the results.

  12. #37
    Join Date
    Mar 2002
    Posts
    36
    Quote Originally Posted by horizon View Post
    Already tried the substr like that. Since it's in a loop, it removes all the commas but thanks for your help anyway.

    Do not put it inside the loop.
    Put it after the loop.
    Because the purpose of that statement is to remove
    the last comma.


    PHP Code:
    {
    ..
    loop..
    }

    $final_string substr$string0, -1); 

  13. #38
    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.

  14. #39
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    3,103
    Quote Originally Posted by horizon View Post
    As my subject title mentions. However, here's what I'm trying to do;

    - I'm trying to create generic SQL queries and export it.

    Ex:

    PHP Code:
    CREATE table ...
    ADD ... ,
    ADD ... ,
    ADD ... ,
    TYPE=... 
    How would I remove the last ',' before the ) TYPE= ?
    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 ) ." "

  15. #40
    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

  16. #41
    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.

    That's what I'm trying to accomplish.

  17. #42
    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!

  18. #43
    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 ?

  19. #44
    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!

  20. #45
    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:
    Quote Originally Posted by horizon View Post
    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.
    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

  21. #46
    Join Date
    Mar 2006
    Posts
    984
    you do NOT need that inside a loop!
    Not if there were only a single SQL table. More than one does require to be inside a loop since each blocks needs to be repeated with different table names since each of these tables has different content.

    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.
    The question here is quite the same as the above ... what happends if multiple table names are involved in a loop rather than a single, would it still work as expected ?

  22. #47
    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!

  23. #48
    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!

  24. #49
    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?
    1 - I'm trying to gather all SQL tables (SHOW TABLES command).
    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.

  25. #50
    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!

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •