I’m facing an interesting problem here. I have a piece of code where I’d like to send some data from different arrays to my database. The problem is that I have a lot of code duplication, so I was trying to make a function out of it. An example:
$i = "0";
$education = $user['education'][$i]['school']['name'];
while ($education != null){
mysql_query("INSERT INTO educations VALUES(
{$user['id']},
'{$user['education'][$i]['school']['name']}'
)");
}
$i++;
$education = $user['education'][$i]['school']['name'];
}
And I’d like it to work like this:
$i = "0";
$item = $user['education'][$i]['school']['name'];
$table = 'educations'
$current = $item;
while ($current != 'EOL'){
if ($current != null){
mysql_query("INSERT INTO {$table} VALUES(
{$user['id']},
'{$item}'
)");
}
$i++;
$current = $item;
}
The thing I’m trying to do is to save the variable variable $user['education'][$i]['school']['name'] in $item in such a way that, whenever I store $item in another variable, it inserts the value of $i again. I don’t know if this is even possible, but it’s something interesting to think about.
For variables names, you can use something like ${‘myvariable_’.$i}, which will output $myvariable_0, _1, …
For array, I don’t think it works (can you try it please?), else you should use a function like that: