11-29-2016, 02:09 PM
So, this would be if each line of the db is an entire language... To generate the form... Note that you can pass in an array of columns you don't want to show (such as id)
Here's the usage...
$t = 'language';
$skip = ['id'];
generateForm($t,$id,$skip);
And here's how you update...
Here's the usage...
Code:
function generateForm($table,$id, $skip=[]){
Code:
$db = DB::getInstance();
Code:
$fields = [];
Code:
$q=$db->query("SELECT * FROM {$table} WHERE id = ?",array($id));
Code:
$r=$q->first();
Code:
foreach($r as $field => $value) {
Code:
if(!in_array($field, $skip)){
Code:
echo '<div class="form-group">';
Code:
echo '<label for="'.$field.'">'.ucfirst($field).'</label>';
Code:
echo '<input type="text" class="form-control" name="'.$field.'" id="'.$field.'" value="'.$value.'">';
Code:
echo '</div>';
Code:
}
Code:
}
Code:
return true;
Code:
}
$t = 'language';
$skip = ['id'];
generateForm($t,$id,$skip);
And here's how you update...
Code:
function updateFields2($post, $skip=[]){
Code:
$fields = [];
Code:
foreach($post as $field => $value) {
Code:
if(!in_array($field, $skip)){
Code:
$fields[$field] = sanitize($post[$field]);
Code:
}
Code:
}
Code:
return $fields;
Code:
}
Here's the usage...
Code:
$skipFields = [];
Code:
$updateFields = updateFields2($_POST, $skipFields);
Code:
$db->update('language',1,$updateFields);