I am trying to connect a database with php and display result dynamically using javascript. Here's What i am trying to do -
<?php
function mainMenu($q){
$res=array();;
$q->setFetchMode(PDO::FETCH_NUM);
while($r = $q->fetch()){
array_push($res, "
<li>
<a class='gn-icon ".mysql_real_escape_string($r[0])."'>".mysql_real_escape_string($r[1])."
</a>
</li>");
}
return $res;
} ?>
Now here is the html , which definitely works
<ul id="sidemenu" class="gn-menu">
<?php
$a=mainMenu($q);
foreach ($a as $value) {
echo $value;
}
?>
</ul>
but when i try this -
<script>
$('#sidemenu').html(<?php
$b=mainMenu($q);
foreach ($b as $value) {
echo "$value";
}
?>);
</script>
It doesnt work the, i just see blank space in my source and nothing is printed in the list, can anyone tell me where i am going wrong...
json_encode
.<a class='gn-icon ".mysql_real_escape_string($r[0])."'>
should beurlencode
.html('<? your_code?>')
. QUOTESimplode
instead offoreach
while printing.