0

Within a BPM application, I'm trying to loop through an associative array of associative arrays and return only the values (APP_UIDs) using a foreach loop. However, I'm only able to display the very last generated value and not all the values.

Here is my code:

$currentUser = @@USER_LOGGED;
//Copy Notes to Subprocess
$caseId = @@APPLICATION; //Case UID
$subcases = executeQuery("SELECT APP_UID FROM SUB_APPLICATION WHERE APP_PARENT='$caseId'");
if (is_array($subcases) and count($subcases) > 0) { 
  foreach($subcases as $subcase)
  $subCaseId = $subcase["APP_UID"] . ", ";

//Update the Sent By status
executeQuery("UPDATE APP_CACHE_VIEW SET PREVIOUS_USR_UID = '$currentUser' WHERE APP_UID IN ('$subCaseId')");
}

The executeQuery function should generate a SQL statement of something like below but it is not. Example:

UPDATE APP_CACHE_VIEW SET PREVIOUS_USR_UID = '54454572356235' WHERE APP_UID IN      
('336545547', '436545534', '736545125')

Any ideas as to what I'm doing wrong?

1
  • The others have given you the correct answer. However I just want to say that in these cases of building IN statements, I prefer to create an array with the values to be used, then create the comma separated string using implode(',', $array) Commented Feb 25, 2013 at 4:48

3 Answers 3

3

try this.

    $currentUser = @@USER_LOGGED;
    //Copy Notes to Subprocess
    $caseId = @@APPLICATION; //Case UID
    $subcases = executeQuery("SELECT APP_UID FROM SUB_APPLICATION WHERE APP_PARENT='$caseId'");
    if (is_array($subcases) and count($subcases) > 0) { 
      $subCaseId = '';
      foreach($subcases as $subcase)
      $subCaseId .= $subcase["APP_UID"] . ", ";

    //Update the Sent By status
    executeQuery("UPDATE APP_CACHE_VIEW SET PREVIOUS_USR_UID = '$currentUser' WHERE APP_UID IN ('$subCaseId')");
    }

Also don't forget to 'reinitialize the string' by doing $subCaseId = ''; in your code.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you are missing concatenation here:

Initialize variable like below on top of loop:

$subCaseId = '';

and use concatenation operator .= as below:

$subCaseId .= $subcase["APP_UID"] . ", ";

Ideally, you should also remove last command and space from variable $subCaseId

also you need to remove single quote around this variable for IN query.

See complete solution as below:

$currentUser = @@USER_LOGGED;
//Copy Notes to Subprocess
$caseId = @@APPLICATION; //Case UID
$subcases = executeQuery("SELECT APP_UID FROM SUB_APPLICATION WHERE APP_PARENT='$caseId'");
if (is_array($subcases) and count($subcases) > 0) { 
    $subCaseId = '';
      foreach($subcases as $subcase)
          $subCaseId = $subcase["APP_UID"] . ", ";
    //To remove last comma and space
    $subCaseId = substr($subCaseId,0,-2);

//Update the Sent By status
executeQuery("UPDATE APP_CACHE_VIEW SET PREVIOUS_USR_UID = '$currentUser' WHERE APP_UID IN ($subCaseId)");
}

Comments

0

Change

 foreach($subcases as $subcase)
  $subCaseId = $subcase["APP_UID"] . ", ";

to :

  $subCaseId = '';
  foreach($subcases as $subcase)
  $subCaseId .= $subcase["APP_UID"] . ", ";

Note the concatenation .=

Comments

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.