CodeIgniter Forums
retrieving and showing data - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: retrieving and showing data (/showthread.php?tid=65690)

Pages: 1 2


RE: retrieving and showing data - InsiteFX - 07-13-2016

Then that's his error he is trying to assign an object to an array

Here are to function to handle objects and arrays to convert to each other.

PHP Code:
if ( ! function_exists('array_to_object'))
{
    
/**
     * array_to_object ()
     * --------------------------------------------------------------------
     *
     * Converts an array to an object.
     *
     * @param   $data
     * @return  object
     */
    
function array_to_object($data)
    {
        if (
is_array($data))
        {
            
/**
             * Return array converted to object Using __FUNCTION__
             * (Magic constant) for recursive call
             */
            
return (object) array_map(__FUNCTION__$data);
        }
        else
        {
            return 
$data;
        }
    }
}

if ( ! 
function_exists('object_to_array'))
{
    
/**
     * object_to_array ()
     * --------------------------------------------------------------------
     *
     * Converts an object to an array.
     *
     * USAGE: $array = object_to_array($object);
     *
     * @param $obj
     * @return array
     */
    
function object_to_array($obj)
    {
        if (
is_object($obj))
        {
            
// Gets the properties of the given object with get_object_vars function
            
$obj get_object_vars($obj);
        }

        if (
is_array($obj))
        {
            
/**
             * Return array converted to object Using __FUNCTION__
             * (Magic constant) for recursive call
             */
            
return array_map(__FUNCTION__$obj);
        }
        else
        {
            return 
$obj;
        }
    }




RE: retrieving and showing data - davy_yg - 07-13-2016

Hello,

Where to type in that function?  In which file?

I have this error appears in views/pcategories.php


A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: views/pcategories.php
Line Number: 54
Backtrace:
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyGiondaCI\application\views\pcategories.php
Line: 54
Function: _error_handler
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyGiondaCI\application\controllers\Cpages.php
Line: 47
Function: view
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyGiondaCI\index.php
Line: 315
Function: require_once

views/pcategories.php


PHP Code:
<div class="widget-box">
                        <
div class="widget-title"><h5>Parent Categories</h5></div>
                        <
div class="widget-content">
                        
                        <
table border="0" style="width: 100%; height: 90px;">
                            <
tr>
                                <
td>PARENT NAME</td>
                                <
td>DESCRIPTION</td>
                                <
td>EDIT</td>
                                <
td>DELETE</td>    
                            </
td>
                            
                            <?
php// foreach($posts as $post){?>
                            <tr><?php echo $this->Mpages->call_parentctg(); ?>
                                <td><?php echo /*$data['ctgparent_name'];*/ $ctgparent_name?></td>
                                <td><?php echo /*$data['ctgparent_description'];*/ $ctgparent_description?></td>
                                <td><button type="button" class="edit" onclick="location.href='<?php echo site_url('cpages/editparentctg'); ?>';">EDIT</button></td>
                                <td><button type="button" class="delete" href="adminform.php">DELETE</button></td>    
                            </td>    
                            <tr>
                                <td><?php //echo $data['ctgparent_name']; ?></td>
                                <td><?php //echo $data['ctgparent_description']; ?></td>
                                <td><button type="button" class="edit" onclick="location.href='<?php echo site_url('cpages/addparentctg'); ?>';">EDIT</button></td>
                                <td><button type="button" class="delete" href="adminform.php">DELETE</button></td>    
                            </td>
                            <?php// }?> 
                            
                        </table>            
                        </div>
                    </div> 



line 54:  <tr><?php echo $this->Mpages->call_parentctg(); ?>


RE: retrieving and showing data - davy_yg - 07-13-2016

I try to insert that new function in the controllers:

PHP Code:
Parse errorsyntax errorunexpected 'if' (T_IF), expecting function (T_FUNCTIONin C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyGiondaCI\application\controllers\Cpages.php on line 35
A PHP Error was encountered

Severity
Parsing Error

Message
syntax errorunexpected 'if' (T_IF), expecting function (T_FUNCTION)

Filenamecontrollers/Cpages.php

Line Number
35

Backtrace



controllers/cpages.php


PHP Code:
public function __construct()
    {
        
parent::__construct();
        
        
$this->load->helper('url');
        
$this->load->helper('url_helper');
        
$this->load->helper('form');
        
        
$this->load->library('form_validation');
        
        
$this->load->model('Mpages');
    }
     
    if ( ! 
function_exists('array_to_object'))
    {
 
   /**
     * array_to_object ()
     * --------------------------------------------------------------------
     *
     * Converts an array to an object.
     *
     * @param   $data
     * @return  object
     */
 
   function array_to_object($data)
 
   {
 
       if (is_array($data))
 
       {
 
           /**
             * Return array converted to object Using __FUNCTION__
             * (Magic constant) for recursive call
             */
 
           return (object) array_map(__FUNCTION__$data);
 
       }
 
       else
        
{
 
           return $data;
 
       }
 
   }
}

if ( ! 
function_exists('object_to_array'))
{
 
   /**
     * object_to_array ()
     * --------------------------------------------------------------------
     *
     * Converts an object to an array.
     *
     * USAGE: $array = object_to_array($object);
     *
     * @param $obj
     * @return array
     */
 
   function object_to_array($obj)
 
   {
 
       if (is_object($obj))
 
       {
 
           // Gets the properties of the given object with get_object_vars function
 
           $obj get_object_vars($obj);
 
       }

 
       if (is_array($obj))
 
       {
 
           /**
             * Return array converted to object Using __FUNCTION__
             * (Magic constant) for recursive call
             */
 
           return array_map(__FUNCTION__$obj);
 
       }
 
       else
        
{
 
           return $obj;
 
       }
 
   }
    } 
 
    public function 
index() { $this->load->view('index'); }
    
    public function 
categories() { $this->load->view('categories');} 



views/pcategories.php


PHP Code:
<div class="widget-box">
                        <
div class="widget-title"><h5>Parent Categories</h5></div>
                        <
div class="widget-content">
                        
                        <
table border="0" style="width: 100%; height: 90px;">
                            <
tr>
                                <
td>PARENT NAME</td>
                                <
td>DESCRIPTION</td>
                                <
td>EDIT</td>
                                <
td>DELETE</td>    
                            </
td>
                            
                            <?
php// foreach($posts as $post){?>
                            <tr><?php echo $this->Mpages->call_parentctg(); ?>
                                <td><?php echo /*$data['ctgparent_name'];*/ $ctgparent_name?></td>
                                <td><?php echo /*$data['ctgparent_description'];*/ $ctgparent_description?></td>
                                <td><button type="button" class="edit" onclick="location.href='<?php echo site_url('cpages/editparentctg'); ?>';">EDIT</button></td>
                                <td><button type="button" class="delete" href="adminform.php">DELETE</button></td>    
                            </td>    
                            <tr>
                                <td><?php //echo $data['ctgparent_name']; ?></td>
                                <td><?php //echo $data['ctgparent_description']; ?></td>
                                <td><button type="button" class="edit" onclick="location.href='<?php echo site_url('cpages/addparentctg'); ?>';">EDIT</button></td>
                                <td><button type="button" class="delete" href="adminform.php">DELETE</button></td>    
                            </td>
                            <?php// }?> 
                            
                        </table>            
                        </div>
                    </div> 



RE: retrieving and showing data - InsiteFX - 07-14-2016

You place the functions in a CI helper say utility_helper

Then load the helper


RE: retrieving and showing data - arma7x - 07-14-2016

OP please learn how to code and debugging properly, it's basic thing. You only post same errors in diferrent scenarios. Have a nice day buddy.


RE: retrieving and showing data - Ivo Miranda - 07-14-2016

You guys are really patient... I would say that davy_yg is some sort of troll or something spamming all the forum but whatever...


RE: retrieving and showing data - davy_yg - 07-16-2016

Parse error: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION) in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyGiondaCI\application\controllers\Cpages.php on line 34
A PHP Error was encountered
Severity: Parsing Error
Message: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION)
Filename: controllers/Cpages.php
Line Number: 34
Backtrace:

I have a new error that appearing that I am a little bit confuse. 


Line 34 is:  if ( ! function_exists('array_to_object'))


This is a little bit confusing to me.  Why the error appearing?


RE: retrieving and showing data - mwhitney - 07-18-2016

(07-16-2016, 07:18 PM)davy_yg Wrote: Parse error: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION) in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyGiondaCI\application\controllers\Cpages.php on line 34
A PHP Error was encountered
Severity: Parsing Error
Message: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION)
Filename: controllers/Cpages.php
Line Number: 34
Backtrace:

I have a new error that appearing that I am a little bit confuse. 


Line 34 is:  if ( ! function_exists('array_to_object'))


This is a little bit confusing to me.  Why the error appearing?

Because an if statement is not valid in the body of a class. You can only define methods, properties, and constants in a class. An if statement would have to be either inside a method or outside of the class. One option would be to do something like this:

PHP Code:
protected function array_to_object($data)
{
    if (function_exists('array_to_object')) {
         return array_to_object($data);
    }
    // ... your existing array_to_object method here


Note that I've declared the method as protected, because leaving the visibility off of the method declaration is the same as declaring a public method, and public methods in controllers can be routed or accessed via the URL.