I'm trying to output a button using PHP but I cant seem to get the '
and "
correctly.
This is the output I should be getting from the PHP code below:
<button class="button button1" onclick="buttonTest(99.16918,-82.9191)"> Lithuania </button>
This is my code:
foreach ($getClasses as $row){
echo '<button class="button button1" onclick="buttonTest(', $row->BUILDING_Latitude ,',',$row->BUILDING_Longtitude,)>', $row->COURSE_Title,'</button>';
}
how do i get php to output this html code above i get an error
syntax error, unexpected '>' (48) : eval()'d code
syntax error, unexpected ')', expecting ',' or ';' (48) : eval()'d code
Update
Also, please advice me the best way to rewrite this, so that these kind of issues do not occur in the future.
You missed the '
here:
tude, ')">', $ro
//----^-^
Full code:
echo '<button class="button button1" onclick="buttonTest(', $row->BUILDING_Latitude ,',',$row->BUILDING_Longtitude, ')">', $row->COURSE_Title,'</button>';
//------------------------------------------------------------------------------------------------------------------^-^
I would really write this way:
<?php
foreach ($getClasses as $row) {
echo "<button class=\"button button1\" onclick=\"buttonTest({$row->BUILDING_Latitude}, {$row->BUILDING_Longtitude})\">{$row->COURSE_Title}</button>";
}
Or even better:
<?php
foreach ($getClasses as $row) {
$lat = $row->BUILDING_Latitude;
$lon = $row->BUILDING_Longtitude;
$course = $row->COURSE_Title;
echo "<button class=\"button button1\" onclick=\"buttonTest($lat, $lon)\">$course</button>";
}