i'm trying to do a recursive function. It's a simple id creation and checking the id inside the database. If the id is already created, then it should run the function back to create a new id and do the checking again. Below is the code.
public function session_order(){
$sr_function = new sr_function();
$session_no = (rand(0,2));
//i have set the order_id in the db as '1'//
$sql = "SELECT COUNT(order_id) as order_count
FROM src_order WHERE order_id = '".$session_no."'";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);
if ($row['order_count'] == 1){
$this->session_order();
}
return $session_no;
}
how ever, when $row['order_count'] == 1, the function did not run the session_order() back to create another session no. Thank you
When you generate a successful ID, you need to pass it back up the call stack.
if ($row['order_count'] == 1){
$session_no = $this->session_order();
}
Why are you doing this with recursion? A simple iterative loop seems more reasonable. For one thing, the current implementation repeats the DB query for every ID creation. Isn't the query result supposed to be the same every time? Or are you altering the ID list in parallel?