Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3271168
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:42:13+00:00 2026-05-17T18:42:13+00:00

So I have a function as detailed below, I believe it is too long

  • 0

So I have a function as detailed below,
I believe it is too long and can be shortened.
I think I have fallen over in the array manipulation part.
Please ignore the strange database syntax


So basically this function is given a string like this

abc, def, ghi, jkl, mno

due to some glitches it can be received with an extra , so it ends up as

abc, def, ghi, jkl, mno,

The function must convert the string so it looks like

‘abc’, ‘def’, ‘ghi’, ‘jkl’, ‘mno’ ($groups)

The string is then used in a select query

SELECT group_name as name FROM contact_groups WHERE group_name IN($groups);

We use array_diff() on the original array and the array from the select query.
This will give us an array of all the groups that we are using that do not exist. ($create)

Next we Loop over the array and create the groups

foreach($create as $group){
  $values = array(
    'user_id', $_SESSION['user_id'],
    'group_name', $group
  );
  $this->database->insert_query($values, 'contact_groups');
}

Now we do the select query again, but this time we do it to get the id’s of the groups

SELECT group_id as id FROM contact_groups WHERE group_name IN($groups);

And finally we loop over the group id’s and add them to another table.


private function groups($groups){
  $groups = split(', ', $groups);
  $intersect = array();
  $db_rows = array();

  foreach($groups as &$group)
    $group = trim(str_replace (',', '', $group)); //remove any rogue spaces or commas

  $groupsq = $groups;
  foreach($groupsq as &$group)
    $group = '\''.$group.'\'';

  $groupsq = implode(', ', $groupsq);
  $q = "SELECT group_name as name FROM contact_groups WHERE group_name IN($groupsq);";
  $r = $this->database->results($this->database->query($q));
  while($row = mysql_fetch_assoc($r)) {
    $intersect[] = $row;
  }

  //create a list of non-existent groups
  $create = array_diff($groups, $intersect);

  foreach($create as $group){
    $values = array(
      'user_id', $_SESSION['user_id'],
      'group_name', $group
    );

    $this->database->insert_query($values, 'contact_groups');
    $this->database->query($q);
  }

  $q = "SELECT group_id as id FROM contact_groups WHERE group_name IN($groupsq);";
  $r = $this->database->results($this->database->query($q));
  while($row = mysql_fetch_assoc($r)) {
    $db_rows = $row;
  }

  foreach($db_rows as $group){
    $values = array(
      'group_id' => $group,
      'contact_id' => $this->contact_id
    );

    $q = $this->database->insert_query($values, 'contact_groups_link');
    $this->database->query($q);
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:42:13+00:00Added an answer on May 17, 2026 at 6:42 pm

    Well, a much shorter function for the first few steps is:

    /**
     @param string like bla, bla, bla, bla,
     @return string like "bla", "bla", "bla"
    */
    function blablabla($string){
        return rtrim(preg_replace('/([a-z]+)/', '"\1"', $string), ',');   
    }
    

    As of the rest of your code, I’m not really sure what you’re trying to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple jQuery function--detailed below-- that I'm using to manipulate the position
I have downloaded glibc's source code, and find the function ceil() for a long
I have the following function in nodejs, res.execSync takes in multiple parameters as detailed
In short Why, based on the code detailed below, is the shouldLoad function not
I have function like this: function ypg_delete_img($id, $img) { $q = $this->ypg_get_one($id); $imgs =
I have function LoadTempMovieList(), and need to load movies from sessionStorage. But it seems
i have function public Menu Details(int? id) { return _dataContext.Menu.Include(ChildMenu).FirstOrDefault(m => m.MenuId == id);
I have function: char *zap(char *ar) { char pie[100] = INSERT INTO test (nazwa,
I have function getCartItems in cart.js and I want to call that function in
I have function Start() that is fired on ready. When I click on .ExampleClick

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.