Increment value

Issue #33 new
Former user created an issue

Hi all how increases a numerical value?

$data = [ 'revisioni' => +1 ];

$data = [ 'revisioni' => 'revisioni'+1 ];

not working, thanks

Comments (1)

  1. Bernd Z.

    Hello,

    i had the same problem, in the update function replace the foreach loop with

    foreach ($data as $key => $val) {
    
                if ( $val == 'add1' )
                {
                    $valstr[] = "`$key`" . " = `$key`+1";
                }
                else if (strpos($val, '()') == true OR is_numeric($val))
                    { $valstr[] = "`$key`" . " = $val"; }
                else
                    { $valstr[] = "`$key`" . " = '$val'"; }
            }
    

    now you can use the update +1 function with

    $where = array(
             'id' => 14
    );
     $data = array(
             'views' => 'add1'
    );
    
    $db->where($where)->update('table', $data); 
    
    // Produces: UPDATE table SET views= views+1 WHERE id = '14' ;
    
  2. Log in to comment