Welcome Guest, Not a member yet? Register   Sign In
add 1 to highest number in table
#1

[eluser]rijobo[/eluser]
Hello,

I'm trying to add 1 to a number in my table. I use this function:

Code:
function getFactuurnr(){
$this-> db-> select('factuurnummer');
$this-> db-> order_by ("factuurnummer", "desc");
$this-> db-> limit(1);
$nummeroud = $this-> db-> get('factuur');
$nummernieuw = $nummeroud + 1;
return $nummernieuw;
}

When I do this, its keeps coming up with 2 and i get this warning:

Severity: Notice
Message: Object of class CI_DB_mysql_result could not be converted to int
Filename: models/mfactuur.php
Line Number: 12

Line 12 is

Code:
$nummernieuw = $nummeroud + 1;
#2

[eluser]Ben Edmunds[/eluser]
Try this:

Code:
function getFactuurnr(){
$this-> db-> select('factuurnummer');
$this-> db-> order_by ("factuurnummer", "desc");
$this-> db-> limit(1);
$nummeroud = $this-> db-> get('factuur');
$result = $nummeroud->row();
$nummernieuw = $result->factuurnummer + 1;
return $nummernieuw;
}
#3

[eluser]rijobo[/eluser]
That's it, thank you!
Could you explain to me why this is the solution?
#4

[eluser]Ben Edmunds[/eluser]
No problem.

Sure I can explain. $nummeroud is just the query and you have to call row(), row_array(), result(), result_array(), etc on it for it to return the object(s) or array(s) to work with.

row() is used since you are only selecting one row from the DB and it returns the row as an object. If you were to do row_array it would return the row as an array so you would use $result['factuurnummer'] instead of $result->factuurnummer.


Make sense?
#5

[eluser]rijobo[/eluser]
thanks again, makes sense now!




Theme © iAndrew 2016 - Forum software by © MyBB