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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:22:08+00:00 2026-05-23T18:22:08+00:00

I had to make some changes to my app to work with a relationship

  • 0

I had to make some changes to my app to work with a relationship change in my db.

Originally I had whats below for a 1::0-1 relationship

if ($model->address->AddressLine1) echo $model->address->AddressLine1.'<br />';
if ($model->address->AddressLine2) echo $model->address->AddressLine2.'<br />';
if ($model->address->city->Name) echo $model->address->city->Name;
if ($model->address->city->regionCode->RegionCode) echo ', '.$model->address->city->regionCode->RegionCode;

but had to change it to work with a 1::0-n relationship

foreach ($model->address as $al1)
foreach ($model->address as $al2)
foreach ($model->address as $al2)
foreach ($model->address as $city)
foreach ($model->address as $region) {
    echo $al1->AddressLine1.' '.$al2->AddressLine2.'<br/>'.$city->city->Name.' '.$city->city->regionCode->RegionCode;
}

I want to retain the functionality of the if in my original code. With the original code I was able to use

', '.

in

if ($model->address->city->regionCode->RegionCode) echo
', '. $model->address->city->regionCode->RegionCode;

to only add a comma after City only when a Region is present in the db.

So how can I get that back and utilize if within my array?

  • 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-23T18:22:09+00:00Added an answer on May 23, 2026 at 6:22 pm

    You only need one foreach loop — you’re just iterating through a single array.

    You can stick the conditionals into the foreach loop, updating the variable name. This assumes that $model->address is an array. On each iteration $address will be set to a subsequent element of that array.

    foreach ( $model->address as $address ) {
        if ($address->AddressLine1) echo $address->AddressLine1.'<br />';
        if ($address->AddressLine2) echo $address->AddressLine2.'<br />';
        if ($address->city->Name) echo $address->city->Name;
        if ($address->city->regionCode->RegionCode) echo ', '.$address->city->regionCode->RegionCode;
    }
    

    For more information:

    • PHP foreach documentation
    • foreach tutorial
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails (2.3.5) app and I had to make some changes on
Since the launch of IOS 5, I have had to make some changes to
I am about to make some changes in my site in the area of
If you had to make a case to a business about adopting or moving
I had more database and I tried to make backup for them but they
i had soulation .i want to make navigation bar with items i will select
I recently had an idea to create my own String class to make using
Just wondering if you had any thoughts on this problem. I want to make
If I had 1000 rows, would a single-line separator make the table view bigger
Had an interesting discussion with some colleagues about the best scheduling strategies for realtime

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.