Php Display While Loop From Function - php

I have a question regarding displaying the contents of a function, this function displaying a while loop.
Here is a function within my model:
function get_results($id)
{
$stmt = "select * where ... "
$stmt = $this->BEAR->Database->query($stmt);
$result = '';
while($row = mysqli_fetch_array($stmt))
{
$result .= '<div>';
$result .= $row['name'];
$result .= '</div>';
}
$this->BEAR->Template->setData('loop', $result, FALSE);
}
This is my Controller:
$BEAR->Webprofile->get_results(Template->getData('id'));
And this is my view:
<?php echo $this->getData('loop');?>
This displays the Loop within my view with no problem. But what I wish for is not to have any HTMl within my Model, Is there anyway of doing this (As this can cause a large amount of HTML in my Model). Maybe a way I can set the data within the Model and then get the data within my view.
I tried setting within the Model functions while loop individually like the following:
while($row = mysqli_fetch_array($stmt))
{
$this->BEAR->Template->setData('name', $row['name']);
$this->BEAR->Template->setData('name', $row['age']);
}
Then call the function in the Controller and call each setData, but this only displayed the first result not the full while loop of contents.
Therefore I wish to display all the contents of my while loop in my view (with HTML) but wish my function to just be getting and setting the Data. Can this be done? Any thoughts or guidance would be appreciated.

You need to apply some discipline to your MVC. Your models need to return raw data. It should return only objects or arrays of data. The key is consistency.
Your views need to include all the code to add your html formatting. Having a view that simply calls a model function you wrote that spits out a div or an ordered list, makes the entire concept of the view useless. Your views should provide all the HTML code.
Since you're using PHP, you can easily drop in and out of HTML.
Start with something like this in your model:
function get_results($id)
{
$stmt = "select * where ... "
$stmt = $this->BEAR->Database->query($stmt);
$results = array();
while($row = mysqli_fetch_array($stmt))
{
$results[] = $row['name'];
}
return results;
}
From there, you should be able to figure out that your controller should call this function, and pass the $results into your view/template along with the specific view file for rendering.

function get_results($id)
{
$stmt = "select * where ... "
$stmt = $this->BEAR->Database->query($stmt);
$result = '';
$result = mysqli_fetch_array($stmt);
return $result;
}
Then in your controller:
$this->BEAR->Template->setData('loop', $model->get_results($id), FALSE);
Then in your template
foreach($rows as $row){
....do something with each row
}
full example of how to get the data from the model and then pass to the template
class MyController {
function controller_showResults(){
$model = new Model();
$results = $model->get_results($_GET['id']);
$this->BEAR->Template->setData('loop', $results, FALSE);
}
}
Now the view assuming that the first argument to setData in template is a variable passed to the view and that variable is $results
<?php foreach($loop as $l): ?>
<div><?php echo $l['name'] ?></div>
<?php endforeach; ?>

Related

Storing variables in loop and passing it to controller

Here is my code/layout:
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$username = $row['username'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
}
as you can see, i am looping through my "users" database table and there are thousands users.
How can i store the information and pass it to my controller to display the information?
Instead of using the while loop, you can fetch them all into an array using the fetchAll method:
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
But are you really fetching your entire users table? It seems like a lot of work for your application to do. Normally for something like an index (for example) you would use pagination so you wouldn't have to fetch thousands of rows for each request.
It's tough to answer this question without knowing the specific problem you're facing. But if you're seeking basic guidance re: model-view-controller, this extremely-simplified class outline might offer some guidance:
class MyModel
{
public function getData()
{
$toReturn = array();
// Prepare & execute your SQL statement.
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$toReturn[] = array(
$row['username'],
$row['first_name'],
$row['last_name']
);
}
return $toReturn;
}
}
class MyController
{
public function indexAction()
{
$model = new MyModel();
// Retrieve the data from the model.
$dataForView = $model->getData();
// Send the data to the view so it can be displayed.
$this->render($dataForView);
}
}

Efficient way to reuse the same call to the same sql table

So I search for this title hoping someone would have already answered it however, I came across similar topics on other languages but not PHP so maybe this will help others.
I am constantly using this following script to call on the database but how can I create it so that I can make it just once at the top of the class for example and use it in every method on the class page that needs it. Example: An single page may not have all of the data it needs from the same table but if the table contains 50% of the data or more for that page, how can I modify this so that I can just say it once and let the rest of the following scripts display the data it extracted in the first place by calling it all just once?
Here's what I have now.
<?php
if($res = $dbConn->query("SELECT Column FROM Table")){
while($d = $res->fetch_assoc()){
printf("Enter HTML here with proper %s", $d['Column']);
}
}
?>
I want to call on this without the printf(" "); collect and store the data so that I can then call the results while printing or echoing the results with the HTML in other methods. What os the most efficient way? I don't want to make the same call over and over and over... well, you get the point.
Should I use fetch_array or can I still do it with fetch_assoc?
not very sure if it's the answer you want.
you can use include/include_once/require/require_once at the top of the page you want to use the function
for example:
general_function.php:
-----
function generate_form( $dbConn, $sql ) {
if($res = $dbConn->query("SELECT Column FROM Table")) {
while($d = $res->fetch_assoc()) {
printf("Enter HTML here with proper %s", $d['Column']);
}
}
}
and for those pages you want to use the function, just put
include "$PATH/general_function.php";
and call generate_form
Try this:
class QueryStorage {
public static $dbConn = null;
public static $results = [];
public static function setConnection($dbConn) {
self::$dbConn = $dbConn;
}
public static function query($query, $cache = true) {
$result = (array_key_exists($query, self::$results))?
self::$results[$query] : self::$dbConn->query($query);
if($cache) {
self::$results[$query] = $result;
}
return $result;
}
public static function delete($query) {
unset(self::$results[$query]);
}
public function clean() {
self::$results = [];
}
}
usage:
at top somewhere pass connection to class:
QueryStorage::setConnection($dbConn);
query and store it:
$result = QueryStorage::query("SELECT Column FROM Table", true);
if($result){
while($d = $result->fetch_assoc()){
printf("Enter HTML here with proper %s", $d['Column']);
}
}
reuse it everywhere:
$result = QueryStorage::query("SELECT Column FROM Table", true); // it will return same result without querying db second time
Remember: it's runtime cache and will not store result for second script run. for this purposes You can modify current class to make it
work with memcache, redis, apc and etc.
If I understood you correctly, then the trick is to make an associative array and access with its 'key' down the code.
$dataArray = array();
// Add extra column in select query for maintaining uniqness. 'id' or it can be any unique value like username.
if($res = $dbConn->query("SELECT Column,id FROM Table")){
while($d = $res->fetch_assoc()){
$dataArray[$d['id']] = $d['Column'];
}
}
//you have value in the array use like this:
echo $dataArray['requireValueId'];
//or , use 'for-loop' if you want to echo all the values
You need a function which takes in the query as a parameter and returns the result.
Like this:
public function generate_query($sql) {
if($res = $dbConn->query($sql)){
while($d = $res->fetch_assoc()){
printf("Enter HTML here with proper %s", $d['Column']);
}
}
}

How to pass data from an customized helper to a view with codeigniter

I have created an helper class to show values coming from a database.
Helper
if (!function_exists('items')){
function show_items($item) {
$ci = get_instance();
$sql = " SELECT * FROM table WHERE items = '$item'";
$result = $ci->db->query($sql);
return $result->row();
}
}
In the view I use the function like this:
echo show_items($item)->main_image);
The echo above shows obviously only the first row of main_image.
My goal is to show all the rows of the selected $item.
If I change the helper like this
$sql = " SELECT * FROM table WHERE items = '$item'";
$result = $ci->db->query($sql);
return $result->result();// first option
return $result->result_array();//second option
}
}
how can I loop the result in the view using for istance a foreach?
your helper code with
$query->result_array():
//this is your view code
<?php $result = show_items($item); ?>// call helper
// DATA RECALL FROM THE HELPER
<?php foreach($result as $data): ?>
<?php echo $data['main_image']; ?>
<?php endforeach; ?>

How to pass variable value to another file via class'

I have my main (user visible) file which displays posts, and I need to set-up pagination.
It would be easy if I fetch DB in the same file (but I want to avoid that), that is why I created a seperate (user hidden) file which contains class' which are then called from main file(blog.php):
BLOG.php(simplified):
<?php
require 'core.php';
$posts_b = new Posts_b();
$posts_bx = $posts_b->fetchPosts_b();
foreach($posts_hx as $posts_hy){
echo $posts_hy['title'];
}
?>
core.php(simplified);
class Posts_b extends Core {
public function fetchPosts_b(){
$this->query ("SELECT posts_id, title FROM posts");
//return
return $this->rows();
}
}
This works like a charm, but now I need to do the count within query, which works fine, and which gives me a variable $pages=5 (handled inside class posts_b - in file core.php),
core.php(simplified-with variable);
class Posts_b extends Core {
public function fetchPosts_b(){
$this->query ("SELECT posts_id, title FROM posts");
$pages=5;
//return
return $this->rows();
}
}
Now I need a way to return this variable value to blog.php (the way I return rows())
Please help, anyone,
Thank you...
A function can only have a single return value.
There are ways to get around this though. You can make your return value be an array that contains all of the values you want. For example:
return array("pages"=>$pages, "rows"=>$this->rows());
Then in your code
require 'core.php';
$posts_b = new Posts_b();
$posts_bx = $posts_b->fetchPosts_b();
$pages = $posts_bx["pages"];
foreach($posts_hx["rows"] as $posts_hy){
echo $posts_hy['title'];
}
?>
Or you can adjust a input parameter provided it was supplied as a reference
public function fetchPosts_b(&$numRows){
$this->query ("SELECT posts_id, title FROM posts");
//return
return $this->rows();
}
In your code
require 'core.php';
$posts_b = new Posts_b();
$pages = 0;
$posts_bx = $posts_b->fetchPosts_b(&$pages);
foreach($posts_hx["rows"] as $posts_hy){
echo $posts_hy['title'];
}
?>
Or you can opt to figure out your pagination outside of the fetchPosts_b method.
$posts_bx = $posts_b->fetchPosts_b();
$pages = floor(count($posts_bx)/50);

php pdo in class + best method to display return array

I have the following class:
<?php
class photos_profile {
// Display UnApproved Profile Photos
public $unapprovedProfilePhotosArray = array();
public function displayUnapprovedProfilePhotos() {
$users = new database('users');
$sql='SELECT userid,profile_domainname,photo_name FROM login WHERE photo_verified=0 AND photo_name IS NOT NULL LIMIT 100;';
$pds=$users->pdo->prepare($sql); $pds->execute(array()); $rows=$pds->fetchAll();
$unapprovedProfilePhotosArray = $rows;
echo 'inside the class now....';
foreach($rows as $row) {
echo $row['userid'];
}
}
}
I can display the data successfully from the foreach loop.
This is a class that is called as follows and want to be able to use the array in the display/view code. This why I added the "$unapprovedProfilePhotosArray = $rows;" but it doesn't work.
$photos_profile = new photos_profile;
$photos_profile->displayUnapprovedProfilePhotos();
<?php
foreach($photos_profile->unapprovedProfilePhotosArray as $row) {
//print_r($photos_profile->unapprovedProfilePhotosArray);
echo $row['userid'];
}
?>
What is the best way for me to take the PHP PDO return array and use it in a view (return from class object). I could loop through all the values and populate a new array but this seems excessive.
Let me know if I should explain this better.
thx
I think you're missing the $this-> part. So basically you're creating a local variable inside the method named unapprovedProfilePhotosArray which disappears when the method finishes. If you want that array to stay in the property, then you should use $this->, which is the proper way to access that property.
...
$pds=$users->pdo->prepare($sql); $pds->execute(array()); $rows=$pds->fetchAll();
$this->unapprovedProfilePhotosArray = $rows;
...

Categories