0

I have a mysql database set up with column names such as:

Room1Type Room1Level Room1Dimensions Room2Type Room2Level ...etc

The number of rooms will vary depending on each entry. Some rows will have 5 roooms, some will have 30. There is also a column that tells me how many rooms total there are so I can loop through them. The problem is, when looping through them in a "for loop" how can I change the variable name in the loop to reference a single column, such as "$room['Room1Type']" on the first loop and then "$room['Room2type']" on the second loop, etc?

1
  • Maybe it's better if you just look into database normalization... Commented Jul 17, 2016 at 19:26

2 Answers 2

1

You can create keys' names as:

$index = 2;
echo $room['Room' . $index . 'type'];
Sign up to request clarification or add additional context in comments.

Comments

0

I believe you need to restructure your database (unless for very rare reason you're forced to use this structure) .. Your current structure will give lots of troubles later when you need to query/edit records .. besides, it's not the normal practice.

The new structure might look like Something like this: (check this simple table)

If you decided to go this way and already created this table this way, next step is to read about "Database normalization"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.