1

I have defined a PHP function that works absolutely fine but I am facing a problem. I am trying to call GenerateIDNumber function multiple times on same page with different values but its returning the same value of first time defined variables, is there any way to close/end the function and recall it with new values.

Here is my code

<?php
/* GenerateID Number */
function GenerateIDNumber($tablenames, $columnnames)
{
    global $conn;
    global $highest_id;

    $sql    = "SELECT MAX(" . $columnnames . ") as max FROM " . $tablenames;
    $rs     = mysqli_query($conn, $sql);
    $result = mysqli_fetch_row($rs);
    if ($result[0] >= 1) {
        $highest_id = $result[0] + 1;
    } else {
        $highest_id = 1;
    }
}
/* GenerateID Number */

$highest_id    = null;
$mytable_name  = "tblleasesalespayment";
$mycolumn_name = "PaymentID";
GenerateIDNumber($mytable_name, $mycolumn_name);
$printNumber     = $highest_id;
$SaleIDGenerator = "CRV-";
$SetNumberCust   = $SaleIDGenerator . $_SESSION['ACTBRANCHID'] . date('y') . sprintf("%08s", $printNumber);

$Code1 = $SetNumberCust;

$highest_id    = null;
$mytable_name  = "tblrepossess_vehicles";
$mycolumn_name = "rp_id";
new GenerateIDNumber($mytable_name, $mycolumn_name);
$printNumber   = $highest_id;
$SetNumberCust = "REP-" . $_SESSION['ACTBRANCHID'] . date('y') . sprintf("%08s", $printNumber);
$Code2         = $SetNumberCust;
?> 
7
  • 1
    Shudder... someone still using the global "feature"... Commented Jun 7, 2017 at 20:12
  • Actually your function does not return any value. It has no return statement. Commented Jun 7, 2017 at 20:12
  • @arkascha $highest_id is used to combine with the returning value Commented Jun 7, 2017 at 20:13
  • instead of using global, return value from the function and assign it to highest_id Commented Jun 7, 2017 at 20:14
  • Sure, I see you try that. But that is not the same as returning the value. In short words: stop using the global keyword! It is evil! It causes huge issues. Return the value you compute from the function, then things work. Read the php documentation about how functions are used in php. Commented Jun 7, 2017 at 20:15

1 Answer 1

1

Check if this servers your request

/* GenerateID Number */
function GenerateIDNumber($tablenames, $columnnames)
{
    global $conn;

    $sql    = "SELECT MAX(" . $columnnames . ") as max FROM " . $tablenames;
    $rs     = mysqli_query($conn, $sql);
    $result = mysqli_fetch_row($rs);
    if ($result[0] >= 1) {
        $highest_id = $result[0] + 1;
    } else {
        $highest_id = 1;
    }
    return $highest_id ;
}



 $highest_id    = null;
$mytable_name  = "tblleasesalespayment";
$mycolumn_name = "PaymentID";
$highest_id = GenerateIDNumber($mytable_name, $mycolumn_name);
$printNumber     = $highest_id;
$SaleIDGenerator = "CRV-";
$SetNumberCust   = $SaleIDGenerator . $_SESSION['ACTBRANCHID'] . date('y') . sprintf("%08s", $printNumber);

echo $Code1 = $SetNumberCust. " As Code1 value <br />";

$highest_id    = null;
$mytable_name  = "tblrepossess_vehicles";
$mycolumn_name = "rp_id";
$highest_id = GenerateIDNumber($mytable_name, $mycolumn_name);
$printNumber   = $highest_id;
$SetNumberCust = "REP-" . $_SESSION['ACTBRANCHID'] . date('y') . sprintf("%08s", $printNumber);
echo $Code2 = $SetNumberCust. " As Code2 value <br />";
Sign up to request clarification or add additional context in comments.

9 Comments

I am still getting the same value again
Are you getting the value as 1?
No actually on first call GenerateIDNumber function it returns the perfect values but on second call GenerateIDNumber it must check for new table and column name but it returns the value of first call
Try to echo $sql for each call and see if they are different.
this is what I am getting, CRV-41700000000 As Code1 value, REP-41700000000 As Code2 value
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.