Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to know where we are in a loop using php

Tags:

php

I met some trouble with a loop, for doing some xml files for withdrawal I have to assign a value to each record (sepa) The first record must be called and set 'FIRST', the nexts must be called and set 'RECURRENT', and the last one must be called and set 'FINAL'

for doing that I do a loop that insert into my database the reord that will be set into the xml file.

for the first, I have no problem, for the recurrents, I also have no problem, but I do not know how to know when the record is the last of my loop, so I can set 'FINAL'

below is my code

 $i=0;
    while ($prlvt = mysql_fetch_assoc($result5)):
    if($i==0){
        $statement = 'FIRST';
    }elseif($i>0){
        $statement = 'RECURRENT';
    }
    $prlvt_rqt = "INSERT INTO `prelevements` SET
`prelevements__n_doss` = '{$_GET['n_doss']}', 
`prelevements__statut` = 0, 
`prelevements__date_record` = '{$prlvt['date_action']}', 
`prelevements__montant` = '{$prlvt['description']}',
`prelevements__statement` = '{$statement}'
";
    mysql_query($prlvt_rqt);
    $i++;

    endwhile;

The trouble is that I do not know how to set my 'FINAL' statement.

Anykind of help will be much appreciated.

like image 694
Stanislas Piotrowski Avatar asked Mar 24 '26 11:03

Stanislas Piotrowski


1 Answers

See the documentation of mysql_num_rows:

if($i==0){
    $statement = 'FIRST';
}elseif($i == mysql_num_rows($result5) - 1){
    $statement = 'FINAL';
}else{
    $statement = 'RECURENT';
}

Consider swichting to mysqli_*, as of PHP 5.5.0 mysql_* will be deprecated.

like image 99
KeyNone Avatar answered Mar 27 '26 00:03

KeyNone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!