how to get rid of a comma inserted by imploding

Discussion in 'PHP' started by gilgalbiblewheel, Dec 8, 2009.

  1. #1
    An extra comma is inserted in the beginning. Since I put for ($getTds = 1 instead of for ($getTds = 0. Here:
    $tdarr[$getTds]= implode(", ", $tds);
    PHP:
    in
    $tds = array(); //to put each record together to pass it on to the $sql
    $tds[]="";
    if($_GET["no"]==NULL){
    	for ($getTds = 1; $getTds < 1000; $getTds++) {// this divides the tds into 1000 to make it easier to insert in the db table
    		if ($getTds % 10 !== 0) {//if not a multiple of 10
    				$tds[].= "'".$tdInnerHTML[1][$getTds]."'";
    				//echo "<span style=\"font-weight: bold;\">" . $getTds . ")</span>" .$tdInnerHTML[1][$getTds]. " ";//lays out the data
    		}else{//if a multiple of 10
    				$tdarr[$getTds]= implode(", ", $tds);
    
    			echo $tdarr[$getTds]."<br />\n";
    			//$sql = "INSERT INTO ".$acronym." (".$fieldarr.") VALUES (".$tdarr[$getTds].")";
    			//echo $sql."\n";
    			//mysql_query($sql) or die(mysql_error());
    			$tds=array(); //empties the previous array content
    		}
    	}
    }
    PHP:

     
    gilgalbiblewheel, Dec 8, 2009 IP
  2. harsh789

    harsh789 Member

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #2
    I think the second line

    $tds[]="";
    PHP:
    is causing the problem. Try removing it.
     
    harsh789, Dec 8, 2009 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    But then the next occurrence of $tds[] is $tds[].= "'".$tdInnerHTML[1][$getTds]."'";

    Won't there be an error?
     
    gilgalbiblewheel, Dec 8, 2009 IP
  4. califmerchant

    califmerchant Active Member

    Messages:
    112
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #4
    use substr to remove first character(comma)?

    $tdarr[$getTds]=substr(implode(", ", $tds),1);
     
    califmerchant, Dec 8, 2009 IP
  5. The Chosen Generl

    The Chosen Generl Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    califmerchant option is a good option, by I think you have done this wrong.
    What you have just done is the following things, lets create an array named tds, than create a cell and sets its value to a null string. Now some stuff loop: if .. set a new cell .. else get the array into and remove everything.
    You should remove the $tds[]=""; and change the $tds[].= to $tds[]=.
     
    The Chosen Generl, Dec 8, 2009 IP