• 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
retrieving and showing data

#11
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;
        }
    }

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#12
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(); ?>
" If I looks more intelligence please increase my reputation."
Reply

#13
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> 
" If I looks more intelligence please increase my reputation."
Reply

#14
You place the functions in a CI helper say utility_helper

Then load the helper
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#15
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.
Keep calm.
Reply

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

#17
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?
" If I looks more intelligence please increase my reputation."
Reply

#18
(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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.