I have a PHP script which interacts with my MySQL database. In my database I've got multiple columns, like:
'temp_0_state', 'temp_0_color', 'temp_1_state', 'temp_1_color', 'temp_2_state', 'temp_2_color' etc etc. till 'temp_50_state', 'temp_50_color'
All 50 records contain under the 'temp_xxx_state' a name or will be empty ("") and under 'temp_xxx_color' a color.
I can show all the colors of the 50 records manually, like
if ($user['temp_0_state'] == "") {
} else {
echo . $user['temp_0_color'] . ;
}
if ($user['temp_1_state'] == "") {
} else {
echo . $user['temp_1_color'] . ;
}
if ($user['temp_2_state'] == "") {
} else {
echo . $user['temp_2_color'] . ;
}
etc etc till
if ($user['temp_50_state'] == "") {
} else {
echo . $user['temp_50_color'] . ;
}
But it'll be easier, and to reduce the amount of php code, to use a foreach statement on the number in the middle of the column name for all records between 0 and 50 (0 < n < 50). How can I split the names and use a foreach statement on the number 0 till 50 between the underscores?