Thread: echo and commas

    #1
  1. Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    49
    Rep Power
    15
    hi,

    another slightly stupid question that i'm not sure how to fix: I am writing a metatag generator, for which i get the info from a database. The code is:

    $brandmetatag = new DBconnect;
    $query = "select item_name from dbtable where item_name like '%'";
    $brandmetatag-> dbquery($query);

    while ($brandmetatag->next_record()):
    $i = 0;
    $j = sizeof($brandmetatag->Record);
    while ($i < $j) {
    echo $brandmetatag->Record[$i], ", ";
    $i++;}

    endwhile;


    For some reason, I always get two commas in a row printed out after each Record. When I leave a space this comes out fine, as does leaving no separation. Any ideas why this is or how to get around it ?

    cheers,
    -nick
  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4492
    not sure if this is the problem, but php uses a period to join strings together. it looked like you were trying to use a comma i.e. javascript.

    echo "Hello" . " World";
    echo $brandmetatag->Record[$i] . ", ";

    ---John Holmes...
  4. #3
  5. Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    49
    Rep Power
    15
    unfortunately that musn't be it, cos i stil get the same result, namely two commas in a row separated by a space. Actually according to the php manual i think you can use the comma with echo to print a series of things, as in:

    echo "foo", $bar;

    but anyways, does anyone know how to get rid of this ? Could it be a bug or something ? Although apparently finding bugs is quite unlikely....

    -nick
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4492
    Are you just tring to join all of the array elements into a comma seperated string? how about using implode? one command will join the entire array for you.

    $string = implode(", ",$array);

    if you needed to get your array back, you could use

    explode(", ",$string);

    it seems like this would be easier than going through a loop and joining them...

    ---John Holmes...

IMN logo majestic logo threadwatch logo seochat tools logo