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';;









share|improve this question

















  • 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














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';;









share|improve this question

















  • 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












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';;









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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












  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto