mysql - Sending data of variable length to SQL table from PHP, is there a better way? -


i have json string coming php file js/ajax. number of entries dynamic, ranging 6 30.

i populate array follow

$myarray = array(); $datalength= count($decodedjson['make'][0]['model'][0]['color']); ($x = 0; $x < $datalength*2; $x+=2) {     $myarray[$x] = $decodedjson['make'][0]['model'][0]['color'][$x/2]['int'];     $myarray[$x+1] = $decodedjson['make'][0]['model'][0]['color'][$x/2]['ext']; } ($x = $datalength*2; $x < 30; $x++) {     $myarray[$x] = 0; } 

so gives me array end-padded zeros data @ front.

now, want insert sql table has maximum number of column

$sql = "insert cars values ( '$datalength', '$myarray[0]', '$myarray[1]', '$myarray[2]', '$myarray[3]', '$myarray[4]', '$myarray[5]', '$myarray[6]', '$myarray[7]', '$myarray[8]', '$myarray[9]', '$myarray[10]', '$myarray[11]', '$myarray[12]', '$myarray[13]', '$myarray[14]', '$myarray[15]', '$myarray[16]', '$myarray[17]', '$myarray[18]', '$myarray[19]', '$myarray[20]', '$myarray[21]', '$myarray[22]', '$myarray[23]', '$myarray[24]', '$myarray[25]', '$myarray[26]', '$myarray[27]', '$myarray[28]', '$myarray[29]', 42)"; 

but i'm thinking there must better way???

thanks help, hsc.

why don't fill array zeros when declare it; can save last loop:

$myarray =array_fill(0,30,0); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -