Foros del Web » Programando para Internet » PHP »

Bucle for o while

Estas en el tema de Bucle for o while en el foro de PHP en Foros del Web. Hola Necesito aplicar pagos a órdenes que están en una tabla. Supongamos que el importe de cada Orden es 100$ y me ingresan 200. Necesito ...
  #1 (permalink)  
Antiguo 20/07/2009, 17:09
(Desactivado)
 
Fecha de Ingreso: septiembre-2004
Mensajes: 360
Antigüedad: 19 años, 7 meses
Puntos: 1
Bucle for o while

Hola
Necesito aplicar pagos a órdenes que están en una tabla.
Supongamos que el importe de cada Orden es 100$ y me ingresan 200. Necesito aplicar 100 a cada Orden pero mi bucle está mal.
Código PHP:
if(isset($_REQUEST['PME_data_payment_amount'])){

$money=$_REQUEST['PME_data_payment_amount'];

$tengo=$money;
$new_paid="";
$new_unpaid="";
 
$sqlt=("SELECT * FROM mytable WHERE client_id='$new_cc' AND (status='Open' OR status='Due') AND unpaid>'0' ORDER BY next_payment_due ");
$resultt mysql_query($sqlt$con) or die("Error reading table: ".mysql_error());
if(
$row mysql_fetch_array($resultt)){
 
 
$myid=$row['id'];
 
$paid=$row['paid'];
 
$unpaid=$row['unpaid'];

DO
{
 
$myid=$row['id'];
$paid=$row['paid'];
$unpaid=$row['unpaid'];
$quota=$row['amount'];
 
 
$topaid=$quota-$row['paid'];
 
$new_paid=$row['paid']+$topaid;
 
$new_unpaid=0;
 
$pago=$quota;

 
$q2=mysql_query("update terms set paid='$new_paid', unpaid='$new_unpaid', aux3='Paid' WHERE id='$myid' ");
 
$r2=mysql_query($q2);
 
$tengo=$tengo-$pago;
  

}
while(
$row mysql_fetch_array($resultt));
}


Pero ese while está mal porque me lo aplica a todos los registros
Alguna ayuda ?
  #2 (permalink)  
Antiguo 21/07/2009, 01:29
(Desactivado)
 
Fecha de Ingreso: septiembre-2004
Mensajes: 360
Antigüedad: 19 años, 7 meses
Puntos: 1
Respuesta: Bucle for o while

Me respondo a mí misma y lo dejo por si alguien lo necesita.
Con esta función podemos saber rápidamente el número de pagos que podemos cancelar a partir del monto recibido y del monto de la cuota a pagar. Luego simplemente recorremos los registros (sin todo ese lío que yo hacía) para cancelar las órdenes a pagar o modificar el saldo deudor, según sea el caso:
Código PHP:
function installmentRegime($amount$installments)
    {
        
/*** initialize number of payments required ***/
        
$num_installments 1;

        
/*** initialize the amount of the last payment ***/
        
$last_payment $amount;

        
/*** calculate the number of payments and the amount of the last payment ***/
        
while($last_payment $installments)
        {
            
/*** subtract from last payment ***/
            
$last_payment -= $installments;
            
$num_installments++;
        }

        
/*** return array has number of payments plus the amount of the final payment ***/
        
return array('num_installments'=>$num_installments'last_payment'=>$last_payment);
    } 
Ejemplo:
Código PHP:
/*** amount to pay ***/
    
$amount 1270;

    
/*** amount of installments ***/
    
$installments 120;

    
$info installmentRegime($amount$installments);

    echo 
$info['num_installments'].' installments. Last installment is '.$info['last_payment']; 
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:43.