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 7681381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:19:43+00:00 2026-05-31T18:19:43+00:00

First off, I know the title is generic and not fitting. I just couldn’t

  • 0

First off, I know the title is generic and not fitting. I just couldn’t think of a title that could describe my problem.

I have a table Recipients in MySQL structured like this:

id   |   email   |   status
1      foo@bar         S
2      bar@baz         S
3      abc@def         R
4      sta@cko         B

I need to convert the data into the following XML, depending on the status field. For example:

<Recipients>
    <RecipientsSent>
        <!-- Have the 'S' status -->
        <recipient>foo@bar</recipient>
        <recipient>bar@baz</recipient>
    </RecipientsSent>

    <RecipientsRegexError>
        <recipient>abc@def</recipient>
    </RecipientsRegexError>

    <RecipientsBlocked>
        <recipient>sta@cko</recipient>
    </RecipientsBlocked>
</Recipients>

I have this PHP code to implement this ($recipients contains an associative array of the db table):

<Recipients>

<RecipientsSent>
<?php
foreach ($recipients as $recipient):
    if ($recipient['status'] == 'S'):
        echo "<recipient>" . $recipient['email'] . "</recipient>";
    endif;
endforeach;
?>
</RecipientsSent>

<RecipientsRegexError>
<?php
foreach ($recipients as $recipient):
    if ($recipient['status'] == 'R'):
        echo "<recipient>" . $recipient['email'] . "</recipient>";
    endif;
endforeach;
?>
</RecipientsRegexError>
<?php /** same loop for the B status */ ?>

</Recipients>

So, this means that if I have 1000 entries in the table and 4 different status’ that can be checked, it means that there will be 4 loops, each one executing 1000 times.

How can this be done in a more efficient manner? I thought about fetching four different sets from the database, meaning 4 different queries, would that be more efficient? I’m thinking it could be done with one loop but but I can’t come up with a solution.

Any way this could be done with only one loop?

  • 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-31T18:19:44+00:00Added an answer on May 31, 2026 at 6:19 pm

    With one loop you can do following

    <?php
    
    $string1, $string2 = '';
    
    foreach ($recipients as $recipient) {
      $recipient_email = "<recipient>" . $recipient['email'] . "</recipient>";
    
      switch($recipient['status']) {
        case 'a':
          $string1 .= $recipient_email;
          break;
        case 'b':
          $string2 .= $recipient_email;
          break;
      }
    }
    ?>
    
    <Recipients>
      <RecipientsSent>
        <?php echo $string1; ?>
      </RecipientsSent>
      <RecipientsRegexError>
        <?php echo $string2; ?>
      </RecipientsRegexError>
    </Recipients>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I'm not entirely sure that my question title is very descriptive, so
First off: I know that AES is a block cipher and not a hashing
First off I'm not sure that's the proper title for this question but hopefully
First, I know there are methods off of the generic List<> class already in
First off, let me start off that I am not a .net developer. The
First off: I know that this isn't reliable for actually checking if I can
first of all sorry for the title. I know this is not so clear
First off, I know this is a base JS issue, not jQuery. I am
First off, I know there are ways to make it so that text can
I know first off that this is probably the worst looking regex ever but

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.