Variable variables and php
up vote
-2
down vote
favorite
I'm building a concepts-database for my students. I have made a decision to store the school subjects in one column in the database where a 1 represents that the concept belongs to that subject. A concept can belong to several subjects, hence the strange storing system. I gather the altered subjects from the code shown in the code below. It bothers me that i can't make a loop where the variables change for each row. I have tried. I can get the subjects in an array from my database, but I can't manage to make the variable variables work.
TL;DR: How can I code the subjects in to a loop from my stored subjects, stored in the variable $subjects_short. I tried with
if ($'_POST[' . $subjects_short . ']'== 1)$term_subject .= '1'; else $term_subject .= '0';;
in the loop and I can't get it to work.
if ($_POST['sv']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['en']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ma']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ms']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['bi']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['fy']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ke']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['tk']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ge']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['hi']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['re']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['sh']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['hkk']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['idh']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['mu']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['sl']== 1)$term_subject .= '1'; else $term_subject .= '0';;
php loops variables variable-variables
|
show 3 more comments
up vote
-2
down vote
favorite
I'm building a concepts-database for my students. I have made a decision to store the school subjects in one column in the database where a 1 represents that the concept belongs to that subject. A concept can belong to several subjects, hence the strange storing system. I gather the altered subjects from the code shown in the code below. It bothers me that i can't make a loop where the variables change for each row. I have tried. I can get the subjects in an array from my database, but I can't manage to make the variable variables work.
TL;DR: How can I code the subjects in to a loop from my stored subjects, stored in the variable $subjects_short. I tried with
if ($'_POST[' . $subjects_short . ']'== 1)$term_subject .= '1'; else $term_subject .= '0';;
in the loop and I can't get it to work.
if ($_POST['sv']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['en']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ma']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ms']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['bi']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['fy']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ke']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['tk']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ge']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['hi']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['re']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['sh']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['hkk']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['idh']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['mu']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['sl']== 1)$term_subject .= '1'; else $term_subject .= '0';;
php loops variables variable-variables
2
Its not a variable variable. Just an variable index. Go with$_POST[$subject_short]
– DarkBee
Nov 10 at 23:14
I'm not sure how to say this more constructively... if you're going to claim to be a teacher, you really owe it to your students to understand the subject matter a lot better than this. If I saw data design and implementation like that in a code review, the next thing I'd be reviewing would be resumes for potential replacement hires.
– kungphu
Nov 10 at 23:37
Thanks! Worked like a charm now and my code gets much prettier
– Björn Andersson
Nov 10 at 23:59
I'm not teaching programming. I teach social sciences. This is my hobby and I could never dream of teaching programming.
– Björn Andersson
Nov 11 at 0:02
You should use three tables: See How to store an array into mysql?
– symlink
Nov 11 at 0:14
|
show 3 more comments
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I'm building a concepts-database for my students. I have made a decision to store the school subjects in one column in the database where a 1 represents that the concept belongs to that subject. A concept can belong to several subjects, hence the strange storing system. I gather the altered subjects from the code shown in the code below. It bothers me that i can't make a loop where the variables change for each row. I have tried. I can get the subjects in an array from my database, but I can't manage to make the variable variables work.
TL;DR: How can I code the subjects in to a loop from my stored subjects, stored in the variable $subjects_short. I tried with
if ($'_POST[' . $subjects_short . ']'== 1)$term_subject .= '1'; else $term_subject .= '0';;
in the loop and I can't get it to work.
if ($_POST['sv']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['en']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ma']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ms']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['bi']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['fy']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ke']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['tk']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ge']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['hi']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['re']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['sh']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['hkk']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['idh']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['mu']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['sl']== 1)$term_subject .= '1'; else $term_subject .= '0';;
php loops variables variable-variables
I'm building a concepts-database for my students. I have made a decision to store the school subjects in one column in the database where a 1 represents that the concept belongs to that subject. A concept can belong to several subjects, hence the strange storing system. I gather the altered subjects from the code shown in the code below. It bothers me that i can't make a loop where the variables change for each row. I have tried. I can get the subjects in an array from my database, but I can't manage to make the variable variables work.
TL;DR: How can I code the subjects in to a loop from my stored subjects, stored in the variable $subjects_short. I tried with
if ($'_POST[' . $subjects_short . ']'== 1)$term_subject .= '1'; else $term_subject .= '0';;
in the loop and I can't get it to work.
if ($_POST['sv']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['en']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ma']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ms']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['bi']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['fy']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ke']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['tk']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['ge']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['hi']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['re']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['sh']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['hkk']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['idh']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['mu']== 1)$term_subject .= '1'; else $term_subject .= '0';;
if ($_POST['sl']== 1)$term_subject .= '1'; else $term_subject .= '0';;
php loops variables variable-variables
php loops variables variable-variables
asked Nov 10 at 23:12
Björn Andersson
73
73
2
Its not a variable variable. Just an variable index. Go with$_POST[$subject_short]
– DarkBee
Nov 10 at 23:14
I'm not sure how to say this more constructively... if you're going to claim to be a teacher, you really owe it to your students to understand the subject matter a lot better than this. If I saw data design and implementation like that in a code review, the next thing I'd be reviewing would be resumes for potential replacement hires.
– kungphu
Nov 10 at 23:37
Thanks! Worked like a charm now and my code gets much prettier
– Björn Andersson
Nov 10 at 23:59
I'm not teaching programming. I teach social sciences. This is my hobby and I could never dream of teaching programming.
– Björn Andersson
Nov 11 at 0:02
You should use three tables: See How to store an array into mysql?
– symlink
Nov 11 at 0:14
|
show 3 more comments
2
Its not a variable variable. Just an variable index. Go with$_POST[$subject_short]
– DarkBee
Nov 10 at 23:14
I'm not sure how to say this more constructively... if you're going to claim to be a teacher, you really owe it to your students to understand the subject matter a lot better than this. If I saw data design and implementation like that in a code review, the next thing I'd be reviewing would be resumes for potential replacement hires.
– kungphu
Nov 10 at 23:37
Thanks! Worked like a charm now and my code gets much prettier
– Björn Andersson
Nov 10 at 23:59
I'm not teaching programming. I teach social sciences. This is my hobby and I could never dream of teaching programming.
– Björn Andersson
Nov 11 at 0:02
You should use three tables: See How to store an array into mysql?
– symlink
Nov 11 at 0:14
2
2
Its not a variable variable. Just an variable index. Go with
$_POST[$subject_short]
– DarkBee
Nov 10 at 23:14
Its not a variable variable. Just an variable index. Go with
$_POST[$subject_short]
– DarkBee
Nov 10 at 23:14
I'm not sure how to say this more constructively... if you're going to claim to be a teacher, you really owe it to your students to understand the subject matter a lot better than this. If I saw data design and implementation like that in a code review, the next thing I'd be reviewing would be resumes for potential replacement hires.
– kungphu
Nov 10 at 23:37
I'm not sure how to say this more constructively... if you're going to claim to be a teacher, you really owe it to your students to understand the subject matter a lot better than this. If I saw data design and implementation like that in a code review, the next thing I'd be reviewing would be resumes for potential replacement hires.
– kungphu
Nov 10 at 23:37
Thanks! Worked like a charm now and my code gets much prettier
– Björn Andersson
Nov 10 at 23:59
Thanks! Worked like a charm now and my code gets much prettier
– Björn Andersson
Nov 10 at 23:59
I'm not teaching programming. I teach social sciences. This is my hobby and I could never dream of teaching programming.
– Björn Andersson
Nov 11 at 0:02
I'm not teaching programming. I teach social sciences. This is my hobby and I could never dream of teaching programming.
– Björn Andersson
Nov 11 at 0:02
You should use three tables: See How to store an array into mysql?
– symlink
Nov 11 at 0:14
You should use three tables: See How to store an array into mysql?
– symlink
Nov 11 at 0:14
|
show 3 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244337%2fvariable-variables-and-php%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Its not a variable variable. Just an variable index. Go with
$_POST[$subject_short]
– DarkBee
Nov 10 at 23:14
I'm not sure how to say this more constructively... if you're going to claim to be a teacher, you really owe it to your students to understand the subject matter a lot better than this. If I saw data design and implementation like that in a code review, the next thing I'd be reviewing would be resumes for potential replacement hires.
– kungphu
Nov 10 at 23:37
Thanks! Worked like a charm now and my code gets much prettier
– Björn Andersson
Nov 10 at 23:59
I'm not teaching programming. I teach social sciences. This is my hobby and I could never dream of teaching programming.
– Björn Andersson
Nov 11 at 0:02
You should use three tables: See How to store an array into mysql?
– symlink
Nov 11 at 0:14