how to create short code for data array sql











up vote
0
down vote

favorite












i'm already can insert data into database using below query. But i want to shortening code for my a few column name because my it just like a continues number. i already explode mark by m1=A, m2=B, m3=C, m4=A and continue..



Below is my controller:



$mark = 'ABCADDBBAACBCDDABBCA';
$totalquestion = '10';

for($i=0; $i<$totalquestion; $i++):
$no = $i+1;
$m = substr($mark, 0, $no);
endfor;

$data = array(
'TotalQuestion' => $totalquestion,
'Mark' => $mark,
'm1' => $m[0],
'm2' => $m[1],
'm3' => $m[2],
'm4' => $m[3],
'm5' => $m[4],
'm6' => $m[5],
'm7' => $m[6],
'm8' => $m[7],
'm9' => $m[8],
'm10' => $m[9],
'm11' => $m[10],
'm12' => $m[11],
'm13' => $m[12],
'm14' => $m[13],
'm15' => $m[14],
'm16' => $m[15],
'm17' => $m[16],
'm18' => $m[17],
'm19' => $m[18],
'm20' => $m[19]
);

$this->excel_import_model->insert($data);


This is my result
enter image description here



My question is how to re-code for my data array so i dont need to type manually m1, m2, m3 till m20. Sorry for my bad english










share|improve this question
























  • just do it once and put it in a function. Then you don't need to type it multiple times but just call the function. Otherwise you can maybe work with a for-loop, because there is always +1 to the next value
    – Svenmarim
    Nov 19 at 8:35















up vote
0
down vote

favorite












i'm already can insert data into database using below query. But i want to shortening code for my a few column name because my it just like a continues number. i already explode mark by m1=A, m2=B, m3=C, m4=A and continue..



Below is my controller:



$mark = 'ABCADDBBAACBCDDABBCA';
$totalquestion = '10';

for($i=0; $i<$totalquestion; $i++):
$no = $i+1;
$m = substr($mark, 0, $no);
endfor;

$data = array(
'TotalQuestion' => $totalquestion,
'Mark' => $mark,
'm1' => $m[0],
'm2' => $m[1],
'm3' => $m[2],
'm4' => $m[3],
'm5' => $m[4],
'm6' => $m[5],
'm7' => $m[6],
'm8' => $m[7],
'm9' => $m[8],
'm10' => $m[9],
'm11' => $m[10],
'm12' => $m[11],
'm13' => $m[12],
'm14' => $m[13],
'm15' => $m[14],
'm16' => $m[15],
'm17' => $m[16],
'm18' => $m[17],
'm19' => $m[18],
'm20' => $m[19]
);

$this->excel_import_model->insert($data);


This is my result
enter image description here



My question is how to re-code for my data array so i dont need to type manually m1, m2, m3 till m20. Sorry for my bad english










share|improve this question
























  • just do it once and put it in a function. Then you don't need to type it multiple times but just call the function. Otherwise you can maybe work with a for-loop, because there is always +1 to the next value
    – Svenmarim
    Nov 19 at 8:35













up vote
0
down vote

favorite









up vote
0
down vote

favorite











i'm already can insert data into database using below query. But i want to shortening code for my a few column name because my it just like a continues number. i already explode mark by m1=A, m2=B, m3=C, m4=A and continue..



Below is my controller:



$mark = 'ABCADDBBAACBCDDABBCA';
$totalquestion = '10';

for($i=0; $i<$totalquestion; $i++):
$no = $i+1;
$m = substr($mark, 0, $no);
endfor;

$data = array(
'TotalQuestion' => $totalquestion,
'Mark' => $mark,
'm1' => $m[0],
'm2' => $m[1],
'm3' => $m[2],
'm4' => $m[3],
'm5' => $m[4],
'm6' => $m[5],
'm7' => $m[6],
'm8' => $m[7],
'm9' => $m[8],
'm10' => $m[9],
'm11' => $m[10],
'm12' => $m[11],
'm13' => $m[12],
'm14' => $m[13],
'm15' => $m[14],
'm16' => $m[15],
'm17' => $m[16],
'm18' => $m[17],
'm19' => $m[18],
'm20' => $m[19]
);

$this->excel_import_model->insert($data);


This is my result
enter image description here



My question is how to re-code for my data array so i dont need to type manually m1, m2, m3 till m20. Sorry for my bad english










share|improve this question















i'm already can insert data into database using below query. But i want to shortening code for my a few column name because my it just like a continues number. i already explode mark by m1=A, m2=B, m3=C, m4=A and continue..



Below is my controller:



$mark = 'ABCADDBBAACBCDDABBCA';
$totalquestion = '10';

for($i=0; $i<$totalquestion; $i++):
$no = $i+1;
$m = substr($mark, 0, $no);
endfor;

$data = array(
'TotalQuestion' => $totalquestion,
'Mark' => $mark,
'm1' => $m[0],
'm2' => $m[1],
'm3' => $m[2],
'm4' => $m[3],
'm5' => $m[4],
'm6' => $m[5],
'm7' => $m[6],
'm8' => $m[7],
'm9' => $m[8],
'm10' => $m[9],
'm11' => $m[10],
'm12' => $m[11],
'm13' => $m[12],
'm14' => $m[13],
'm15' => $m[14],
'm16' => $m[15],
'm17' => $m[16],
'm18' => $m[17],
'm19' => $m[18],
'm20' => $m[19]
);

$this->excel_import_model->insert($data);


This is my result
enter image description here



My question is how to re-code for my data array so i dont need to type manually m1, m2, m3 till m20. Sorry for my bad english







mysql sql codeigniter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 7:44

























asked Nov 19 at 7:39









asnka go

32




32












  • just do it once and put it in a function. Then you don't need to type it multiple times but just call the function. Otherwise you can maybe work with a for-loop, because there is always +1 to the next value
    – Svenmarim
    Nov 19 at 8:35


















  • just do it once and put it in a function. Then you don't need to type it multiple times but just call the function. Otherwise you can maybe work with a for-loop, because there is always +1 to the next value
    – Svenmarim
    Nov 19 at 8:35
















just do it once and put it in a function. Then you don't need to type it multiple times but just call the function. Otherwise you can maybe work with a for-loop, because there is always +1 to the next value
– Svenmarim
Nov 19 at 8:35




just do it once and put it in a function. Then you don't need to type it multiple times but just call the function. Otherwise you can maybe work with a for-loop, because there is always +1 to the next value
– Svenmarim
Nov 19 at 8:35












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










    $mark = 'ABCADDBBAACBCDDABBCA';
$totalquestion = '20';

$data = array();
$data = array(
'TotalQuestion' => $totalquestion,
'Mark' => $mark,
);

//Split String into array
$list = str_split($mark);
$mlist = array();

//Loop through splited string
for($i=0; $i<$totalquestion; $i++):
$mlist['m'.$i] = $list[$i];
endfor;

//Merge here or you an even use push array in for loop itslef
$data = array_merge($data,$mlist);





share|improve this answer





















  • Great. As i need. Thank you sir
    – asnka go
    Nov 20 at 1:37











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%2f53370222%2fhow-to-create-short-code-for-data-array-sql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










    $mark = 'ABCADDBBAACBCDDABBCA';
$totalquestion = '20';

$data = array();
$data = array(
'TotalQuestion' => $totalquestion,
'Mark' => $mark,
);

//Split String into array
$list = str_split($mark);
$mlist = array();

//Loop through splited string
for($i=0; $i<$totalquestion; $i++):
$mlist['m'.$i] = $list[$i];
endfor;

//Merge here or you an even use push array in for loop itslef
$data = array_merge($data,$mlist);





share|improve this answer





















  • Great. As i need. Thank you sir
    – asnka go
    Nov 20 at 1:37















up vote
0
down vote



accepted










    $mark = 'ABCADDBBAACBCDDABBCA';
$totalquestion = '20';

$data = array();
$data = array(
'TotalQuestion' => $totalquestion,
'Mark' => $mark,
);

//Split String into array
$list = str_split($mark);
$mlist = array();

//Loop through splited string
for($i=0; $i<$totalquestion; $i++):
$mlist['m'.$i] = $list[$i];
endfor;

//Merge here or you an even use push array in for loop itslef
$data = array_merge($data,$mlist);





share|improve this answer





















  • Great. As i need. Thank you sir
    – asnka go
    Nov 20 at 1:37













up vote
0
down vote



accepted







up vote
0
down vote



accepted






    $mark = 'ABCADDBBAACBCDDABBCA';
$totalquestion = '20';

$data = array();
$data = array(
'TotalQuestion' => $totalquestion,
'Mark' => $mark,
);

//Split String into array
$list = str_split($mark);
$mlist = array();

//Loop through splited string
for($i=0; $i<$totalquestion; $i++):
$mlist['m'.$i] = $list[$i];
endfor;

//Merge here or you an even use push array in for loop itslef
$data = array_merge($data,$mlist);





share|improve this answer












    $mark = 'ABCADDBBAACBCDDABBCA';
$totalquestion = '20';

$data = array();
$data = array(
'TotalQuestion' => $totalquestion,
'Mark' => $mark,
);

//Split String into array
$list = str_split($mark);
$mlist = array();

//Loop through splited string
for($i=0; $i<$totalquestion; $i++):
$mlist['m'.$i] = $list[$i];
endfor;

//Merge here or you an even use push array in for loop itslef
$data = array_merge($data,$mlist);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 11:29









somsgod

1998




1998












  • Great. As i need. Thank you sir
    – asnka go
    Nov 20 at 1:37


















  • Great. As i need. Thank you sir
    – asnka go
    Nov 20 at 1:37
















Great. As i need. Thank you sir
– asnka go
Nov 20 at 1:37




Great. As i need. Thank you sir
– asnka go
Nov 20 at 1:37


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370222%2fhow-to-create-short-code-for-data-array-sql%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







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft