I am sort of new to php and totally new to ajax, so im still learning. The function below is what I currently have, it queries the database and outputs the images for my website. This works fine.
I am now trying to generate xml from the results to feed into javascript so that if the user wants to have a different view of the gallery - (2 rows instead of 4 for example), the page just updates without refreshing. Would I have to assign all the results to an array and then extract it with javascript ? Not sure where to even start. Please point me in a direction, many thanks !
<?php include 'library/connect.php';
$result = mysql_query("SELECT * FROM fproduct WHERE fproduct.category='Shirts'");
$cols=3;
echo "<table>";
do{
echo "<tr>"; // adds a new row after the for loop has been executed for 4 times (4 columns)
for($i=1;$i<=$cols;$i++){
$row=mysql_fetch_array($result);
if ($row) {?>
<td> <a href = "Shirtbuy.php?id=<? echo $row['product_id'] ?>" ><img border="0" alt="pic" src="images/products/shirts/largethumbs/<? echo $row['pic']?>" /></a>
<br/>
<b><?= $row['prod_name']?> </b><br/>
<b><?= $row['price']?> </b><br/>
</td>
<td width="100"> </td> <?
}
else{
echo "<td> </td>"; //If there are no more records at the end, add a blank column
}
}
} while($row);
echo "</table>";
include 'library/close.php';
instead of XML
use json
you can use php function json_enode
to convert a php array to json
format string.
$json_data = json_encode($result_array);
then pass it to your javascript & parse it
In javascript side. use jquery
and parse json function
var obj = $.parseJSON(json_data);
Jquery Ajax sample request
function send_data() {
$.ajax({
url : "my_php_code.php",
type : "POST",
cache : false,
data : {
the_key : the_value
}
});
}