27.10.2015 Views

AJAX and PHP

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>AJAX</strong> Grid<br />

}<br />

// execute the query<br />

if ($result = $this->mMysqli->query($queryString))<br />

{<br />

// fetch associative array<br />

while ($row = $result->fetch_assoc())<br />

{<br />

// build the XML structure containing products<br />

$this->grid .= '';<br />

foreach($row as $name=>$val)<br />

$this->grid .= '' .<br />

htmlentities($val) .<br />

'';<br />

$this->grid .= '';<br />

}<br />

// close the results stream<br />

$result->close();<br />

}<br />

// update a product<br />

public function updateRecord($id, $on_promotion, $price, $name)<br />

{<br />

// escape input data for safely using it in SQL statements<br />

$id = $this->mMysqli->real_escape_string($id);<br />

$on_promotion = $this->mMysqli->real_escape_string($on_promotion);<br />

$price = $this->mMysqli->real_escape_string($price);<br />

$name = $this->mMysqli->real_escape_string($name);<br />

// build the SQL query that updates a product record<br />

$queryString = 'UPDATE product SET name="' . $name . '", ' .<br />

'price=' . $price . ',' .<br />

'on_promotion=' . $on_promotion .<br />

' WHERE product_id=' . $id;<br />

// execute the SQL comm<strong>and</strong><br />

$this->mMysqli->query($queryString);<br />

}<br />

// returns data about the current request (number of grid pages, etc)<br />

public function getParamsXML()<br />

{<br />

// calculate the previous page number<br />

$previous_page =<br />

($this->mReturnedPage == 1) ? '' : $this->mReturnedPage-1;<br />

// calculate the next page number<br />

$next_page = ($this->mTotalPages == $this->mReturnedPage) ?<br />

'' : $this->mReturnedPage + 1;<br />

// return the parameters<br />

return '' .<br />

'' . $this->mReturnedPage . ''.<br />

'' . $this->mTotalPages . ''.<br />

'' . $this->mItemsCount . ''.<br />

'' . $previous_page . ''.<br />

'' . $next_page . '' .<br />

'';<br />

}<br />

// returns the current grid page in XML format<br />

public function getGridXML()<br />

{<br />

return '' . $this->grid . '';<br />

}<br />

// returns the total number of records for the grid<br />

private function countAllRecords()<br />

{<br />

/* if the record count isn't already cached in the session,<br />

read the value from the database */<br />

208<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!