Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6987015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:52:55+00:00 2026-05-27T18:52:55+00:00

I have some PHP script querying a mysql database and echoing out a piece

  • 0

I have some PHP script querying a mysql database and echoing out a piece of javascript. Everything is working perfectly, except after echoing the last row of the query, I don’t want to echo a comma after the closing bracket. The result of the script should look something like this below, with the last line NOT ending with a comma.

        {title: 'title1', url: 'mp3:mp3-1', start: 660, duration: 398},
        {title: 'title2', url: 'mp3:mp3-2', start: 464, duration: 32},
        {title: 'title3', url: 'mp3:mp3-3', start: 2956, duration: 139},
        {title: 'title4', url: 'mp3:mp3-4', start: 653, duration: 62},
        {title: 'title5', url: 'mp3:mp3-5', start: 1816, duration: 74}

As my code is now, the last line gets a comma just like the lines before it, and it is breaking my resulting javascript code (in IE only of course!!). I’m assuming the answer has to do with counting the number of rows left and if it’s greater then 1 echo one thing, and if it’s equal to 1 echo something else, I’m just not sure how to translate that into PHP code. Thanks for your help.

The code for the loop goes like this:

<?php
    foreach($_SESSION['section_remember'] as $key =>$value)
    {   

    $sql = "SELECT `section`.`start`,`section`.`stop`,`section`.`title`,`daily_show`.`audio_file`
                FROM `section`
                INNER JOIN `daily_show`
                ON `section`.`daily_show_id` = `daily_show`.`id`
                WHERE `section`.`id` = $value";
    $result = mysql_query($sql);


    while ($query = mysql_fetch_array($result))
    {
        $id = $query['id'];
        $start = $query['start'];
        $stop = $query['stop'];
        $title = $query['title'];
        $audio =$query['audio_file'];
        $duration =$stop-$start;

        echo"{title: '",$title;
        echo"', ";
        echo"url: 'mp3:",$audio;
        echo"', ";
        echo"start: ",$start;
        echo", ";
        echo"duration: ",$duration;
        echo"}, ";              
    }
    }
    ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:52:56+00:00Added an answer on May 27, 2026 at 6:52 pm

    Instead of keeping track of whether you are on the last row, you could just trim the trailing comma using rtrim. All of the trimming functions (trim, ltrim, rtrim) in PHP take a second parameter $charlist in which you can specify which characters to trim.

    ob_start(); // use output buffering or append to a string variable
    while ($query = mysql_fetch_array($result))
    {
        $id = $query['id'];
        $start = $query['start'];
        $stop = $query['stop'];
        $title = $query['title'];
        $audio = $query['audio_file'];
        $duration = $stop-$start;
    
        printf('{"title":"%s", "url":"mp3:%s", "start":%d, "duration":%d},',
            $title,
            $audio,
            intval($start),
            intval($duration)
            );
    }
    echo rtrim(ob_get_clean(), ', ');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script that uses some css/javascript tabs, they work on my
I have a php script which accesses a MSSQL2005 database, reads some data from
i need to have some php code inside javascript <script ...> <?php echo ...
I have some PHP script for export MYSQL table into Excel format, but I
My server have default php version of 4, so when I run some script
I have some problems with a PHP script that calls a Bash script.... in
I have a PHP script that works by calling items from a database based
Hi all I have one problem. I am connecting QT with php script..Everything works
I have a php script that sends out emails with attachments once people have
I have some troubles with encoding and mailing. I send post-request to php-script, when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.