justWired
justWired

Reputation: 99

Foreach multidimensional array display values

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?

Upvotes: 0

Views: 118

Answers (3)

Minesh
Minesh

Reputation: 2302

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)");
}

Upvotes: 1

Prasanth Bendra
Prasanth Bendra

Reputation: 32840

Change

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

to :

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

Note the concatenation .=

Upvotes: 0

Maulik Vora
Maulik Vora

Reputation: 2584

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.

Upvotes: 3

Related Questions