Skip to content

Commit

Permalink
New function - dbkeys($table_name)
Browse files Browse the repository at this point in the history
The function generates an array of table column with clean values.

$news_data = dbkeys(DB_NEWS);

is same as..

$news_data = [
'news_id'=>0,'
'news_subject' => '',
//..... xxx lines long.
];

Signed-off-by: deviance <meangczac.chan@gmail.com>
  • Loading branch information
FrederickChan committed Aug 6, 2022
1 parent e18866a commit 2f9a201
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions includes/db_handlers/all_functions_include.php
Expand Up @@ -100,6 +100,29 @@
}
});

/**
* Get the column names of a table
*
* @param $table
*
* @return array
*/
function dbkeys($table) {
static $col_names = [];

if (empty($col_names[$table])) {
$res = dbquery("SHOW COLUMNS FROM $table");
$col_names = [];
while ($rows = dbarray($res)) {
$col_names[$table][] = $rows['Field'];
}
}

return (array)$col_names[$table] ? array_map(function ($k) {
return '';
}, array_flip($col_names[$table])) : [];
}

/**
* Send a database query
*
Expand Down

0 comments on commit 2f9a201

Please sign in to comment.