table users has 14 columns – id, src link, title…
JavaScript
x
function get_atitles(){
global $db;
$st = $db->query("select * from users order by name asc");
$arr = $st->fetchAll();
$ht = '';
foreach($arr as $el){
$ht .= "<div class='atitle' data-id= " . $el['id'] . " data-src='" . $el['src'] . "... and so on for all of the 14 columns...>" . $el['title'] . "</div>n";
}
echo $ht;
}
Is there a shorter way, like this:
JavaScript
foreach($arr as $el){
$ht .= "<div class='atitle'
//foreach column in users $ht .= "data-" . column_name = column_value
$ht .= ">" . $el['title'] . "</div>n";
}
Advertisement
Answer
Maybe something like this:
JavaScript
function get_atitles(){
global $db;
$st = $db->query("select * from users order by name asc");
$arr = $st->fetchAll(PDO::FETCH_ASSOC);
$ht = '';
foreach($arr as $el){
$ht = $ht . "<div class='atitle' ";
foreach($el as $key=>$col)
if($key != 'title')
$ht .= "data-".$key.'="'.$col.'" ';
$ht.= '>'.$el['title'] . "</div>n";
}
echo $ht;
}