Design pattern for PHP classes sharing data from SQL queries -
i'm beginner oop. following jist of code, trying find proper design pattern for:
class data { public $location = array(); public $restaurant = array(); } $data = new data; $query = mysqli_query($mysqli, "select * restaurants"); //actually big long query, simplified illustrative purposes here $i = 0; while ($row = mysqli_fetch_array($query)) { $data->location[] = $i.')'.$row['location']."<br>"; $data->restaurant[] = $row['restaurant']."<br>"; $i++; }
i'd access data class php page. (to print out information in html, hence
tags). prefer not run query twice. understand classes created , destroyed in single php page load. i'd appreciated design pattern guidance managing http application state , minimizing computer resources in such situation.
if store data object in $_session variable, have access other pages , upon refresh. mentioned in other post , comments, want separate out html data processing.
class data { public $location = array(); public $restaurant = array(); } // start session session_start(); $data = new data; $query = mysqli_query($mysqli, "select * restaurants"); //actually big long query, simplified illustrative purposes here $i = 0; while ($row = mysqli_fetch_array($query)) { $data->location[] = $i.')'.$row['location']; $data->restaurant[] = $row['restaurant']; $i++; } // html (separate data processing) foreach ($data->location $location) { echo $location . '<br />'; } // save session $_session['data'] = $data;
when wish reference data object page
// start session session_start(); // data object $data = $_session['data']; // data foreach($data->location $location) { echo $location . '<br />'; }
Comments
Post a Comment