data not parsing to view using template render codeigniter - php

I have a result array from query in a controller, like :
[...]controller/Admin.php :
public function user_email()
{
$this->db->select('user_email');
$this->db->from('user');
$query = $this->db->get();
$data['email'] = $query->result_array();
$this->template->render('admin/user_driver', $data, 'admin');
}
The libraries/Template.php :
class Template {
protected $ci;
public $data;
public function __construct()
{
$this->ci =& get_instance();
}
public function render($view, $viewdata = null, $template = 'frontend')
{
$html = $this->ci->load->view($view, $viewdata, TRUE);
$this->data['template'] = $this->parse_blocks($html);
$this->data['_viewdata'] = $viewdata;
$this->ci->load->view("template/{$template}", $this->data);
}
public function parse_blocks($html)
{
$blocks = array();
if(empty($html)) $html = " ";
libxml_disable_entity_loader(false);
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_use_internal_errors(false);
libxml_disable_entity_loader(true);
foreach ($dom->getElementsByTagName('template') as $node) {
$section = $node -> getAttribute('block');
$blocks[$section] = $this->DOMinnerHTML($node);
}
return $blocks;
}
private function DOMinnerHTML(DOMNode $element)
{
$innerHTML = "";
$children = $element->childNodes;
foreach ($children as $child)
{
$innerHTML .= $element->ownerDocument->saveHTML($child);
}
return $innerHTML;
}
public function block_exists($blockname = NULL)
{
if(empty($blockname))
return false;
return (is_array($this->data) && is_array($this->data['template']) && array_key_exists($blockname,$this->data['template']));
}
}
I var_dump($email) in my user_driver.php view, but it throws Undefined variable: email. The file user_driver.php is under [...]views/admin/user_driver.php.
What did I do wrong?

Your result from the Database is currently returning as a multi-dimensional array result. Change this to row if you're expecting just one result:
$data['email'] = $query->row_array();
This will provide you with a single dimensional array to fetch from:
$this->db->select('user_email');
$this->db->from('user');
$query = $this->db->get();
$dbresult = $query->row_array();
$emailAddress = $dbresult["user_email"];
$data['email'] = $emailAddress;

Related

output and call array from class function (rollingcurl)

Excuse my English, please.
I use Rollingcurl to crawl various pages.
Rollingcurl: https://github.com/LionsAd/rolling-curl
My class:
<?php
class Imdb
{
private $release;
public function __construct()
{
$this->release = "";
}
// SEARCH
public static function most_popular($response, $info)
{
$doc = new DOMDocument();
libxml_use_internal_errors(true); //disable libxml errors
if (!empty($response)) {
//if any html is actually returned
$doc->loadHTML($response);
libxml_clear_errors(); //remove errors for yucky html
$xpath = new DOMXPath($doc);
//get all the h2's with an id
$row = $xpath->query("//div[contains(#class, 'lister-item-image') and contains(#class, 'float-left')]/a/#href");
$nexts = $xpath->query("//a[contains(#class, 'lister-page-next') and contains(#class, 'next-page')]");
$names = $xpath->query('//img[#class="loadlate"]');
// NEXT URL - ONE TIME
$Count = 0;
$next_url = "";
foreach ($nexts as $next) {
$Count++;
if ($Count == 1) {
/*echo "Next URL: " . $next->getAttribute('href') . "<br/>";*/
$next_link = $next->getAttribute('href');
}
}
// RELEASE NAME
$rls_name = "";
foreach ($names as $name) {
$rls_name .= $name->getAttribute('alt');
}
// IMDB TT0000000 RLEASE
if ($row->length > 0) {
$link = "";
foreach ($row as $row) {
$tt_info .= #get_match('/tt\\d{7}/is', $doc->saveHtml($row), 0);
}
}
}
$array = array(
$next_link,
$rls_name,
$tt_info,
);
return ($array);
}
}
Output/Return:
$array = array(
$next_link,
$rls_name,
$tt_info,
);
return ($array);
Call:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function get_match($regex, $content, $pos = 1)
{
/* do your job */
preg_match($regex, $content, $matches);
/* return our result */
return $matches[intval($pos)];
}
require "RollingCurl.php";
require "imdb_class.php";
$imdb = new Imdb;
if (isset($_GET['action']) || isset($_POST['action'])) {
$action = (isset($_GET['action'])) ? $_GET['action'] : $_POST['action'];
} else {
$action = "";
}
echo " 2222<br /><br />";
if ($action == "most_popular") {
$popular = '&num_votes=1000,&production_status=released&groups=top_1000&sort=moviemeter,asc&count=40&start=1';
if (isset($_GET['date'])) {
$link = "https://www.imdb.com/search/title?title_type=feature,tv_movie&release_date=,".$_GET['date'].$popular;
} else {
$link = "https://www.imdb.com/search/title?title_type=feature,tv_movie&release_date=,2018".$popular;
}
$urls = array($link);
$rc = new RollingCurl([$imdb, 'most_popular']); //[$imdb, 'most_popular']
$rc->window_size = 20;
foreach ($urls as $url) {
$request = new RollingCurlRequest($url);
$rc->add($request);
}
$stream = $rc->execute();
}
If I output everything as "echo" in the class, everything is also displayed. However, I want to call everything individually.
If I now try to output it like this, it doesn't work.
$stream[0]
$stream[1]
$stream[3]
Does anyone have any idea how this might work?
Thank you very much in advance.
RollingCurl doesn't do anything with the return value of the callback, and doesn't return it to the caller. $rc->execute() just returns true when there's a callback function. If you want to save anything, you need to do it in the callback function itself.
You should make most_popular a non-static function, and give it a property $results that you initialize to [] in the constructor.. Then it can do:
$this->results[] = $array;
After you do
$rc->execute();
you can do:
foreach ($imdb->results as $result) {
echo "Release name: $result[1]<br>TT Info: $result[2]<br>";
}
It would be better if you put the data you extracted from the document in arrays rather than concatenated strings, e.g.
$this->$rls_names = [];
foreach ($names as $name) {
$this->$rls_names[] = $name->getAttribute('alt');
}
$this->$tt_infos = [];
foreach ($rows as $row) {
$this->$tt_infos[] = #get_match('/tt\\d{7}/is', $doc->saveHtml($row), 0);
}
$this->next_link = $next[0]->getAttribute('href'); // no need for a loop to get the first element of an array

How looks good structure of construct for creating multidimensial array

I am trying to create constructor which creates an multidimensional array. My result should be like this:-
Checkout my array $result_array
For now I have error: Illegal offset type. Note that I have als use __toString() becose I work on xml data.
class Property {
public $xmlClass;
public $elemClass = '';
public $first_array = array();
public $result_array = array();
public $data = '';
public $data2 = '';
public function __construct($xml, $elem) {
$this->xmlClass = $xml;
$this->elemClass = $elem;
foreach ($xml->xpath('//*[#baza]') as $val) {
$this->first_array[] = $val;
foreach ($val->ksiazka as $value) {
$data = $value->$elem->__toString();
$this->result_array[$this->first_array][] = $data;
}
}
}
public function getResult() {
return $this->result_array;
}
}
$result_autor = new Property($xml, 'autor');
$autor = $result_autor->getResult();
You need to change your two foreach() like below:-
foreach($xml->xpath('//*[#baza]') as $val) {
//$this->first_array[] = $val; not needed
foreach($val->ksiazka as $key=> $value){ //check $key here
$data = $value->$elem->__toString();
$this->result_array[$key][] = $data; // add $key hear
}
}
If the above not worked then check this too:-
foreach($xml->xpath('//*[#baza]') as $key=> $val) { //check $key here
//$this->first_array[] = $val; not needed
foreach($val->ksiazka as $value){
$data = $value->$elem->__toString();
$this->result_array[$key][] = $data; // add $key hear
}
}

CodeIgniter(2.1.0) Fatal Error :: Call to undefined method Paidlisting_model::ls()

16th line $data['paidls']=$this->paidlisting->ls(); throws an error Call to undefined method. I have pasted my controller and model here.
Controller| package.php
<?php
class Package extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('advertise/advertise_model','advertise');
$this->load->model('advertise/package_model','package');
$this->load->model('advertise/paidlisting_model','paidlisting');
$this->load->model('gallery/gallery_model','gallery');
$this->load->model('log','log');
$this->load->library('payment');
$myself = $this->session->userdata('client');
}
public function index(){
$data = array();
$data['banners']=$this->package->ls();
$data['paidls']=$this->paidlisting->ls();
$this->load->library('media');
foreach($data['banners'] as $key=>$banner)
{
if($this->gallery->fetchAllData('adv_package',$banner->id)){
$images = array();
$imgs = $this->gallery->fetchAllData('adv_package',$banner->id);
foreach ($imgs as $im)
{
$images[] =array(to_imageurl($im),'alt'=>$im->alt);
}
$data['banners'][$key]->image = $images;
}else{
$data['banners'][$key]->image = '';
}
}
foreach($data['paidls'] as $key=>$row)
{
if($this->gallery->fetchAllData('adv_paidlisting_package',$row->id))
{
$images = array();
$imgs = $this->gallery->fetchAllData('adv_paidlisting_package',$row->id);
foreach ($imgs as $im)
{
$images[] =array(to_imageurl($im),'alt'=>$im->alt);
}
$data['paidls'][$key]->image = $images;
}else{
$data['paidls'][$key]->image = '';
}
}
Model | paidlisting_model.php
public function ls(){
$result = $this->db->get_where('adv_paidlisting_package',array('status'=>1));
$data = array();
if($result->num_rows()){
foreach($result->result() as $item){
array_push($data,$item);
}
}
return $data;
}
Please help me to fix.

formating simplexml object is wrong

I am trying to format from a simpleXML object, but its returning:
title1title2title3
Even though this is...
return ''.$title.'';
Here's my whole code.
<?php
class ForumFeed {
private function getXMLFeeds($feed = 'all')
{
/*
Fetch the XML feeds
*/
$globalFeedXML = file_get_contents('/forum/syndication.php?limit=3');
$newsFeedXML = file_get_contents('/forum/syndication.php?fid=4&limit=3');
/*
Turn feed objects
*/
$globalFeed = new SimpleXMLElement($globalFeedXML);
$newsFeed = new SimpleXMLElement($newsFeedXML);
/*
Return requested feed
*/
if ($feed == 'news') {
return $newsFeed;
} else if ($feed == 'all') {
return $globalFeed;
} else {
return false;
}
}
private function formatFeeds($feed, $node)
{
/*
Format Feeds for displayable content..
*/
$getFeedObject = $this->getXMLFeeds($feed);
$feedData = $getFeedObject->xpath('channel/item/'.$node);
while (list( , $node) = each($feedData)) {
echo $node;
}
}
public function feed($feed)
{
$title = $this->formatFeeds($feed, 'title');
$url = $this->formatFeeds($feed, 'url');
return ''.$title.'';
}
}
$feeds = new ForumFeed();
echo $feeds->feed('all');
I'm not sure what I'm doing wrong.
The problem is in this function:
private function formatFeeds($feed, $node)
{
/*
Format Feeds for displayable content..
*/
$getFeedObject = $this->getXMLFeeds($feed);
$feedData = $getFeedObject->xpath('channel/item/'.$node);
while (list( , $node) = each($feedData)) {
echo $node;
}
}
It's echoing $node, rather than returning anything to the caller. Try something like this:
private function formatFeeds($feed, $node)
{
/*
Format Feeds for displayable content..
*/
$getFeedObject = $this->getXMLFeeds($feed);
$feedData = $getFeedObject->xpath('channel/item/'.$node);
$result = array();
while (list( , $node) = each($feedData)) {
$result[] = $node;
}
return implode(', ', $result);
}

convert Object to array

I ceated a function to convert a list of parent child to an object of class "city" :
public static function createCity(array $config)
{
$city= new City();
$id=key($config);
$label= $config[$id]['label'];
$city->setId($id);
$city->setLabel($label);
$children = $config[$id]['childrens'];
if(!empty($children)){
foreach($children as $key_child => $child) {
$children = array($key_child => $child);
$city->addChild(self::createCity($children));
}
}
return $city;
}
Now I would to creat a function to do the opposite => convert an object of type Class City to an array,so I do like that :
public function getCityArray(City$rootCity)
{
$result = array();
$result['id'] = $rootCity->getId();
$result['label']= $rootCity->getLabel();
$children = $rootCity->getChildren();
if ( !empty($children)) {
foreach ($children as $key_child => $child) {
$result['childrens'] = array($key_child => $child );
$result[] = $this->getCityArray($child);
}
}
return $result;
}
But it doesn't work because when I do var_dump('$result') so I have a list with no end and the loop does not stop?
Try this. Since I don't know have the full code, not sure if it will work. The class variable $result will contain the results.
$result = array();
public function getCityArray(City $rootCity) {
$result['id'] = $rootCity->getId();
$result['label']= $rootCity->getLabel();
$children = $rootCity->getChildren();
if ( !empty($children)) {
$result['childrens'] = $children;
$this->result[] = $result;
foreach ($children as $key_child => $child) {
$this->getCityArray($child);
}
}
}

Categories