PHP Loop Iteration Counts

What's a good method of determining the loop number you're on during a php while loop?

In my below code I need to delimit the variable($insightIDS) each loop with a comma, but if there is only 1 result or it's the last result of the loop I don't want to add a comma.

function getCustomRSSID($userID)
{
    include 'config.php';
    $sql="select insightID
              from usersInsights
              where userID = ".$userID;
    $result=$db->query($sql);
    $insightIDS = 0;
    while($row=$result->fetch_assoc()){

        $insightIDS .= $row['insightID'];

    }
    return $insightIDS;
}

Open in new window

LVL 12
Nathan RileyFounderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

James BilousSoftware EngineerCommented:
Create a variable outside of the loop and then increment it on every loop iteration. You can use it to tell which iteration you are on.
function getCustomRSSID($userID)
{
    include 'config.php';
    $sql="select insightID
              from usersInsights
              where userID = ".$userID;
    $result=$db->query($sql);
    $insightIDS = 0;
    $i = 0;
    while($row=$result->fetch_assoc()){
        $i++;
        $insightIDS .= $row['insightID'];
         print($i);
    }
    return $insightIDS;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Marco GasiFreelancerCommented:
Personally, I like to keep things simple: I leave the loop proceeds adding a comma each time then, once the loop has finished, I delete the last comma using substr() function:

$insightIDS  = substr( $insightIDS, 0, strlen( $insightIDS ) - 1 );

Open in new window


If you're adding a space after the comma, then subtract 2 charactyers instead of one:

$insightIDS  = substr( $insightIDS, 0, strlen( $insightIDS ) - 2 );

Open in new window

0
Ray PaseurCommented:
There is also the rtrim() function to remove a collection of specific characters from the end of lines.
http://php.net/manual/en/function.rtrim.php
0
Ray PaseurCommented:
You might also write it like this.  (Different paths to the same end, 6+3=9, 7+2=9, etc)
function getCustomRSSID($userID)
{
    include 'config.php';
    $sql="select insightID
              from usersInsights
              where userID = ".$userID;
    $result=$db->query($sql);
    $insightIDS = [];
    while($row=$result->fetch_assoc()){

        $insightIDS[] = $row['insightID'];

    }
    return implode(',', $insightIDS);
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.