Whats the best way to store multiple locations for users and querying them back out?

Viewed 67

I have an assignment where I need make a table for users which houses basic information like fname,lname,email...etc but I also need to store information about the users location address,zip,city,state...etc. A user can have multiple location information.

I've been just doing a csv but people are telling its pretty bad to do that so I'm trying to do things the right way.

I was learning about many to many and it seems to do the trick. But the problem is I need to load this data to a table for viewing.

I just don't see anyway that this would work without having to do a query inside the first query.

ie:

while($row=mysqli_fetch_assoc($query)){
  $id = $row['id'];
  ///Get user locations based on id here.
}

From what I understand if we're storing user ID lets say to a table to make the relation to the locations table and a user can have multiple locations would join be useless here?

I need to pull up records 25 at a time so it's not only supposed to pull one. I'm using datatables with the collapse/show so the data needs to be in a separate container

1 Answers

The Approach of storing location separately in other table with user_id column will be good and as the user will be unique let's say he is unique regarding his email, his locations can be fetched. Other approach, You can store locations(multiple) in form of object and fetch them then you can get individual location by decoding the json

Related