Juank Juank -4 years ago 109
MySQL Question

PHP: string data- attribute gets split

So I'm creating buttons from a result set of MySQL query, and binding data- attributes from those results. All fine with numbers and dates, but when the field is a string, the data- attribute created gets split by the whitespaces.

My PHP code:

$options = "";
while($row=mysqli_fetch_array($of)) {
$options .= "<p><a class='btn btn-default btn-open-modal' role='button' data-id=".htmlentities($row["art_id"])." data-name=".htmlentities($row["art_name"]).">See details &raquo;</a></p>";
}


When I inspect the button, I get something like data-name="nike" shox="" (if the name in the table is "nike shox"), but data-id="1" is just fine.

How can I get prevent that the data-name gets split?

Answer Source

Change this...

$options .= "<p><a class='btn btn-default btn-open-modal' role='button' data-id=".htmlentities($row["art_id"])." data-name=".htmlentities($row["art_name"]).">See details &raquo;</a></p>";

To this...

$options .= '<p><a class="btn btn-default btn-open-modal" role="button" data-id="' . htmlentities($row["art_id"]) . '" data-name="' . htmlentities($row["art_name"]) . '">See details &raquo;</a></p>';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download