0

I am having some difficulty finding anything quite similar to what I am trying to accomplish with my code. I have a while loop that executes for each instance of a variable located in a mysql database.

$lref = mysqli_query($con,"SELECT * FROM builtl");

while($lrow = mysqli_fetch_array($lref))
{
   //variables from builtl
   $partnum = $lrow['m3num'];
   $lqty = $lrow['qty'];
   //POST to DB
   postcode($partnum,$lqty);
   echo $lqty;
...//More code exists that is not part of the question

The user defined function postcode() only executes when the form submission button (excluded in sample code) is set. Below is part of the function itself.

function postcode($partnum,$lqty)
{
    $bchk = $partnum."sb";
    if(isset($_POST[$bchk]))
    {
    //builtl
    $lqty=$lqty-1;
    $bltmod="UPDATE builtl SET qty=$lqty WHERE m3num=$partnum";
    if (!mysqli_query($con,$bltmod))
    {
    die('Error: ' . mysqli_error($con));
    }
    }
}

When the code executes, the database is updated inside of the function like it should, but the while loop has already been generated and doesn't pull the new value of $lqty in. I want some of my later code inside the while loop to use the updated value of $lqty. Would using return somewhere help me with this or would reloading the page to pull the new DB value be easier?

Trantis
  • 13
  • 3
  • Are you expecting `postcode()` to update the value of `$lqty`? You need to either have it `return $lqty;`, or make `$lqty` a reference (`function postcode($partnum, &$lqty)`). If you make it a reference, then when it's updated in the function, it'll be updated outside.\ – gen_Eric May 15 '14 at 14:38

2 Answers2

1

Use this line to call postcode():

$lqty = postcode($partnum,$lqty);

and return $lqty from postcode() at the end of the function:

return $lqty;
Quentin Skousen
  • 1,035
  • 1
  • 18
  • 30
  • I left out that I am using my submission button to reload the entire page. Even with this code added, the value is not returned from the function. – Trantis May 15 '14 at 15:03
  • If you are reloading the entire page, `$_POST` won't be set anymore, so `$lqty` isn't getting updated. – Quentin Skousen May 15 '14 at 15:04
  • On the first instance of the reload, assuming the submit caused the reload, the $_POST is set and the code executes properly. I read somewhere that PHP functions have trouble returning values when isset is used, is that possibly what I am facing here? – Trantis May 15 '14 at 15:08
  • I've never heard of or run into that problem. I can't imagine any scenario where this wouldn't work. Are you sure you are returning the value from `postcode()` and catching it in the calling code? – Quentin Skousen May 15 '14 at 15:25
0

try changing

function postcode($partnum,$lqty)

to

function postcode($partnum,&$lqty)

See http://www.php.net/manual/en/language.references.pass.php

Chris Lear
  • 6,592
  • 1
  • 18
  • 26
  • You should avoid passing by reference when possible. see http://stackoverflow.com/questions/2157799/when-to-pass-by-reference-in-php – Quentin Skousen May 15 '14 at 14:42
  • Sure, but this seems like a reasonable case. "I want some of my later code inside the while loop to use the updated value of $lqty" was what made me think it appropriate. Making the postcode function return a value (which is reasonable) is as likely to cause side-effects if it is ever called by other code. But point taken. – Chris Lear May 15 '14 at 14:57
  • Passing by reference seems to be the ideal solution for my situation. This solution worked, thank you Chris. – Trantis May 15 '14 at 15:06