Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Not a member yet? Click here to register.
Forgot Password?

Associative array without while loop

Asked Modified Viewed 1,544 times
E
Elvenelf
E
  • Member, joined since
  • Contributed 184 posts on the community forums.
  • Started 19 threads in the forums
  • Started this discussions
asked
Member

Sometimes we repeatedly want to have an associative array and having to write a while loop everytime to get one array in the variable $data (typically) gets a bit bothersome depending on how many times one has to write it in a certain plugin or infusion.... So I came up with a shortcut to put the entire result of a query into an associative array that one can just assign to one variable - and we can do whatever with it as we need.


function dbarrayassoc($result){   
   if(dbrows($result) > 0)   {
      $output = array();
      while($data = dbarray($result)){
         $output[] = $data;
      }
      return $output;
   }else{
      return false;
   }
}


I pasted it inside maincore.php file right after dbarray.

You'd use it like this...
$result = dbquery("SELECT * FROM tbl WHERE condition='1' ");
$data = dbarrayassoc($result);


$data will have an array of data rows. Since we don't always query the database to iterate over rows but sometimes we need to get more than one row... this can be a useful function.... it was for me, so I share in case it helps you too. :)
0 replies

2 posts

J
JoiNNN
J
JoiNNN 10
  • Veteran Member, joined since
  • Contributed 850 posts on the community forums.
  • Started 100 threads in the forums
answered
Veteran Member

Nice snippet, we should include it in the core and maybe add another argument to json encode the result, useful for ajax requests when you only need the data.
0 replies
E
Elvenelf
E
  • Member, joined since
  • Contributed 184 posts on the community forums.
  • Started 19 threads in the forums
  • Started this discussions
answered
Member

Nice idea, I like the way you think. :D

It couold be something like...
function dbarrayassoc($result, $json_encode = false){   
   if(dbrows($result) > 0)   {
      $output = array();
      while($data = dbarray($result)){
         $output[] = $data;
      }
      if($json_encode) return json_encode($output);
      return $output;
   }else{
      return false;
   }
}
0 replies

Labels

None yet

Statistics

  • Views 0 views
  • Posts 2 posts
  • Votes 0 votes
  • Topic users 2 members

2 participants

E
E
  • Member, joined since
  • Contributed 184 posts on the community forums.
  • Started 19 threads in the forums
  • Started this discussions
J
J
JoiNNN 10
  • Veteran Member, joined since
  • Contributed 850 posts on the community forums.
  • Started 100 threads in the forums

Notifications

Track thread

You are not receiving notifications from this thread.

Related Questions

Not yet