0

I have two functions that I am working with. The first one does a database call and grabs two attributes of a user. The UserID and Markup for that user.

/*
  Search Markup for a specific QID
*/
function searchMarkup($identifier){

  global $markupArray;

  if(isset($markupArray)){
    foreach($markupArray as $m){
      if((string)$key->QID == (string)$identifier){
        return $m->markup;
      }
    }
  }
  return '';
}

/*
  Fetch the markup data for this dashboard
*/
function fetchMarkup(){

  global $dashboardID;
  global $markupArray;

  $objDB = new DB;
  $objMarkup = $objDB
    -> setStoredProc('FetchMarkup')
    -> setParam('dashboardID', $dashboardID)
    -> execStoredProc()
    -> parseXML();

  // Create an array of the markup
  if(isset($objMarkup->data)){
    $i = 0;
    foreach($objMarkup->data as $m){
      $markup[$i] = array();
      $markup[$i]['QID'] = (string)$m->QID;
      $markup[$i]['markup'] = (string)$m->Markup;
      $i++;
    }
    $markupArray = $markup;
  }
}

When I run fetchMarkup() and then print out $markupArray, I get the result of:

Array
(
    [0] => Array
        (
            [QID] => Q0002
            [markup] => success
        )

    [1] => Array
        (
            [QID] => Q101
            [markup] => success
        )

    [2] => Array
        (
            [QID] => Q200
            [markup] => info
        )

)

My next step is to be able to search that array by providing a QID and having it return the markup value to me.

I am trying to so something like searchMarkup('Q0002') to have it tell me the result of markup but I am not getting any response.

How could I go about retrieving the value of markup from the array that is created by fetchMarkup() ?

SBB
  • 8,560
  • 30
  • 108
  • 223
  • So show us the code you have written for the `searchMarkup()` function – RiggsFolly Dec 23 '16 at 01:36
  • The code for that function is the first one – SBB Dec 23 '16 at 01:37
  • in your function `searchMarkup` you use a `$key` that is never defined, and you use the array as an object. – Jeff Dec 23 '16 at 01:39
  • `if($m['QID'] == $identifier){ return $m['markup']; }` – Jeff Dec 23 '16 at 01:40
  • ...and please try to avoid globals. Always return something from functions, _never_ change globals in functions, unless they are parameters. – Jeff Dec 23 '16 at 01:42
  • @Jeff - Regarding the globals, what if its like an ID of something that needs to be passed to a databse call? Do I pass that to the function everywhere instead of defining it globally ? – SBB Dec 23 '16 at 01:44
  • Yes, passing it around seems ridiculous in first place, but gives you a better overview of what is needed and _changed_ somewhere. Right now you don't know, that you change a global var when calling `fetchMarkup()` - unless you remember. And you have no idea what `fetchMarkup()` depends on. – Jeff Dec 23 '16 at 01:57

1 Answers1

1

why you are using $key ? which came from no where

foreach($markupArray as $m)

as you can see you alias $m not $key

And $markupArray is an associated array not an object array

So instead of

if((string)$key->QID == (string)$identifier){
    return $m->markup;
}

since it is an associated array change it to

if((string)$key['QID'] == (string)$identifier){
    return $m['markup'];
}

So your searchMarkup would be like this

function searchMarkup($identifier){

  global $markupArray;

  if(isset($markupArray)){
    foreach($markupArray as $m){
      if((string)$m['QID'] == (string)$identifier){
        return $m['markup'];
      }
    }
  }
  return '';
}

Demo

Beginner
  • 4,118
  • 3
  • 17
  • 26
  • Thanks! I used this and its working fine but I am wondering about performance. My markup array for example contains 3 results in this scenario and I have 10,000 identifiers. Instead of looping through each one of them, is there anyone to search for it directly? – SBB Dec 23 '16 at 04:40