JackBurton
JackBurton

Reputation: 191

PHP: conditional output for last record in a resultset

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"}, ";              
    }
    }
    ?>

Upvotes: 0

Views: 799

Answers (4)

nikc.org
nikc.org

Reputation: 16993

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(), ', ');

Upvotes: 2

Salman Arshad
Salman Arshad

Reputation: 272346

The comma can be placed at the top of your loop instead of bottom; that way you know for sure that there is at least one row to go:

$count = 0;
while ($query = mysql_fetch_array($result))
{
    echo $count++ > 0 ? "," : ""; // notice the use of post-increment operator
    echo"{";
    echo"}";              
}

Upvotes: 2

Grrbrr404
Grrbrr404

Reputation: 1805

Before your echo loop safe the mysql row count in a var. In your loop check your loop indicator ($i or whatever) and make a if ($cout - 1 == $i) {echo 'something else';} instead of your usual echo.

Regards

Upvotes: 1

user836352
user836352

Reputation:

I think you're on the right track. Get the number of rows from that query. for every row, increment a counter that starts at zero. if the number of rows and the counter are not equal, print a line with a comma. else, print a line without a comma. Good luck!

Upvotes: 2

Related Questions