Hi, I would like to loop the data-attribute to contain all column names and values from a mysql table.

This is what it looks like today:

while ($row = $result->fetch()) {
echo "<div id='{$row['namn']}' data-column1='{$row['column1']}' data-column2='{$row['column2']}'...

So some kind of magic :) loop inside there so I don't have to write all the column names manually.

Is this possible?

Cheers!
/Adam

Recommended Answers

All 2 Replies

Try:

$i = 1;
$str = '';

foreach ($row as $column => $value)
{
    str .= " data-column{$i}='{$value}'";

    $i++;
}

echo $str;

Brilliant!! Thanks a bunch!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.