CodeIgniter Forums
Undefined valuable - 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: Undefined valuable (/showthread.php?tid=68007)

Pages: 1 2


Undefined valuable - kayinja.denis - 05-09-2017

Hello guys,
I've two models mark_m and setting_m and am try to get some data from setting table and display it in mark view
but when i try i get this kind of error


Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined variable: schoolTitle
Filename: mark/view.php
Line Number: 23


[font=OpenSans Regular]A PHP Error was encountered[/font]
[font=OpenSans Regular]Severity: Notice[/font]
[font=OpenSans Regular]Message: Trying to get property of non-object[/font]
[font=OpenSans Regular]Filename: mark/view.php[/font]
[font=OpenSans Regular]Line Number: 23[/font]

Below is my code;
setting_m.php -- model

PHP Code:
public function get_school() {
 
  $query $this->db->query("SELECT sname FROM setting");
 
  return $query->result();


mark.php -- controller
PHP Code:
function __construct() {
        
parent::__construct();
        
$this->load->model("student_m");
        
$this->load->model("mark_m");
 
               $this->load->model('setting_m');
        
$language $this->session->userdata('lang');
        
$this->lang->load('mark'$language);
    }

public function 
sch_title(){
 
       $this->data["schoolTitle"]     = $this->setting_m->get_school();
 
       $this->data["subview"] = "error";
 
       $this->load->view('_layout_main'$this->data);
 
   

view.php -- views
Code:
<section class="panel"><?=$schoolTitle->sname?></h1>
           <div class="profile-view-head">
               <a href="#"><h1>
                   <?=img(base_url('uploads/images/'.$student->photo))?>
               </a>

               <h1><?=$student->name?></h1>
               <p><?=$this->lang->line("mark_classes")." ".$classes->classes?></p>

           </div></section>

where am i going wrong please help me


RE: Undefined valuable - neuron - 05-09-2017

in get_school method you are returning object of row objects, you need to fetch only one row. 
update your method:
PHP Code:
public function get_school() {
 
  $query $this->db->query("SELECT sname FROM setting");
if(
$query){
 return 
$query->row();
}
 
 return false//or return empty array


use print_r or var_dump functions for debugging your code


RE: Undefined valuable - php_rocs - 05-09-2017

@kayinja.denis

I agree with @neuron. My only question is are you sure that there is only one record in the table SETTING. Also, it never hurts to test your sql statement on the database first just to make sure it works.


RE: Undefined valuable - neuron - 05-10-2017

(05-09-2017, 08:13 AM)php_rocs Wrote: @kayinja.denis

I agree with @neuron.  My only question is are you sure that there is only one record in the table SETTING.  Also, it never hurts to test your sql statement on the database first  just to make sure it works.

As I know, You dont have to be sure, if it 1 or more rows retrieved, to use row() function, it retrieves first row.

You right, better it is better to write sql code and test it in db, and then apply in php code


RE: Undefined valuable - kayinja.denis - 05-10-2017

@neuron  

when i use your code
PHP Code:
public function get_school() {
 
       $query $this->db->query("SELECT sname FROM setting");
 
       if($query){
 
           return $query->result();
 
       }
 
       return false;
 
   
and dump
 
PHP Code:
<?=var_dump($schoolTitle)?>
 i get bool(false)

but when i change 
$query = $this->db->query("SELECT sname FROM setting");
to
$query = $this->db->query("SELECT * FROM setting");

and dump again, I get all the results

so when i try to retrieve <h1><?php echo $schoolTitle->sname?></h1> 
i get the following error...
Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined index: sname
Filename: mark/view.php
Line Number: 23

thanks


RE: Undefined valuable - InsiteFX - 05-10-2017

Show a dump of the object or array that is returned from your query.


RE: Undefined valuable - kayinja.denis - 05-10-2017

(05-10-2017, 03:48 AM)InsiteFX Wrote: Show a dump of the object or array that is returned from your query.
Quote:array(25) { [0]=> array(2) { ["fieldoption"]=> string(7) "address" ["value"]=> string(22) "p.o box 2 njeru uganda" } [1]=> array(2) { ["fieldoption"]=> string(10) "attendance" ["value"]=> string(3) "day" } [2]=> array(2) { ["fieldoption"]=> string(10) "automation" ["value"]=> string(1) "5" } [3]=> array(2) { ["fieldoption"]=> string(21) "auto_invoice_generate" ["value"]=> string(1) "0" } [4]=> array(2) { ["fieldoption"]=> string(13) "backend_theme" ["value"]=> string(5) "basic" } [5]=> array(2) { ["fieldoption"]=> string(13) "currency_code" ["value"]=> string(3) "UGX" } [6]=> array(2) { ["fieldoption"]=> string(15) "currency_symbol" ["value"]=> string(2) "/=" } [7]=> array(2) { ["fieldoption"]=> string(5) "email" ["value"]=> string(30) "[email protected]" } [8]=> array(2) { ["fieldoption"]=> string(16) "fontendorbackend" ["value"]=> string(1) "1" } [9]=> array(2) { ["fieldoption"]=> string(13) "fontend_theme" ["value"]=> string(5) "basic" } [10]=> array(2) { ["fieldoption"]=> string(13) "fontorbackend" ["value"]=> string(1) "0" } [11]=> array(2) { ["fieldoption"]=> string(6) "footer" ["value"]=> string(28) "Copyright ©florasjunior" } [12]=> array(2) { ["fieldoption"]=> string(16) "google_analytics" ["value"]=> string(0) "" } [13]=> array(2) { ["fieldoption"]=> string(8) "language" ["value"]=> string(7) "english" } [14]=> array(2) { ["fieldoption"]=> string(6) "mark_1" ["value"]=> string(1) "1" } [15]=> array(2) { ["fieldoption"]=> string(6) "mark_3" ["value"]=> string(1) "0" } [16]=> array(2) { ["fieldoption"]=> string(4) "note" ["value"]=> string(1) "1" } [17]=> array(2) { ["fieldoption"]=> string(5) "phone" ["value"]=> string(13) "+256789960188" } [18]=> array(2) { ["fieldoption"]=> string(5) "photo" ["value"]=> string(132) "a8240c1de32628b9ad6a2fae89c2eb4013da24573fd810e49248eae383bf15442178739622ae9f9f9ab93c3f329f6e284cb239942fc9225b24b6c1c1978700c5.png" } [19]=> array(2) { ["fieldoption"]=> string(13) "purchase_code" ["value"]=> string(36) "f541d688-9d40-40db-99fb-65e6f80692ab" } [20]=> array(2) { ["fieldoption"]=> string(11) "school_type" ["value"]=> string(9) "classbase" } [21]=> array(2) { ["fieldoption"]=> string(11) "school_year" ["value"]=> string(1) "1" } [22]=> array(2) { ["fieldoption"]=> string(5) "sname" ["value"]=> string(12) "florasjunior" } [23]=> array(2) { ["fieldoption"]=> string(17) "student_ID_format" ["value"]=> string(1) "1" } [24]=> array(2) { ["fieldoption"]=> string(13) "updateversion" ["value"]=> string(4) "3.00" } }



RE: Undefined valuable - mirivlad - 05-10-2017

(05-10-2017, 04:17 AM)kayinja.denis Wrote:
(05-10-2017, 03:48 AM)InsiteFX Wrote: Show a dump of the object or array that is returned from your query.
Quote:array(25) { [0]=> array(2) { ["fieldoption"]=> string(7) "address" ["value"]=> string(22) "p.o box 2 njeru uganda" } [1]=> array(2) { ["fieldoption"]=> string(10) "attendance" ["value"]=> string(3) "day" } [2]=> array(2) { ["fieldoption"]=> string(10) "automation" ["value"]=> string(1) "5" } [3]=> array(2) { ["fieldoption"]=> string(21) "auto_invoice_generate" ["value"]=> string(1) "0" } [4]=> array(2) { ["fieldoption"]=> string(13) "backend_theme" ["value"]=> string(5) "basic" } [5]=> array(2) { ["fieldoption"]=> string(13) "currency_code" ["value"]=> string(3) "UGX" } [6]=> array(2) { ["fieldoption"]=> string(15) "currency_symbol" ["value"]=> string(2) "/=" } [7]=> array(2) { ["fieldoption"]=> string(5) "email" ["value"]=> string(30) "[email protected]" } [8]=> array(2) { ["fieldoption"]=> string(16) "fontendorbackend" ["value"]=> string(1) "1" } [9]=> array(2) { ["fieldoption"]=> string(13) "fontend_theme" ["value"]=> string(5) "basic" } [10]=> array(2) { ["fieldoption"]=> string(13) "fontorbackend" ["value"]=> string(1) "0" } [11]=> array(2) { ["fieldoption"]=> string(6) "footer" ["value"]=> string(28) "Copyright ©florasjunior" } [12]=> array(2) { ["fieldoption"]=> string(16) "google_analytics" ["value"]=> string(0) "" } [13]=> array(2) { ["fieldoption"]=> string(8) "language" ["value"]=> string(7) "english" } [14]=> array(2) { ["fieldoption"]=> string(6) "mark_1" ["value"]=> string(1) "1" } [15]=> array(2) { ["fieldoption"]=> string(6) "mark_3" ["value"]=> string(1) "0" } [16]=> array(2) { ["fieldoption"]=> string(4) "note" ["value"]=> string(1) "1" } [17]=> array(2) { ["fieldoption"]=> string(5) "phone" ["value"]=> string(13) "+256789960188" } [18]=> array(2) { ["fieldoption"]=> string(5) "photo" ["value"]=> string(132) "a8240c1de32628b9ad6a2fae89c2eb4013da24573fd810e49248eae383bf15442178739622ae9f9f9ab93c3f329f6e284cb239942fc9225b24b6c1c1978700c5.png" } [19]=> array(2) { ["fieldoption"]=> string(13) "purchase_code" ["value"]=> string(36) "f541d688-9d40-40db-99fb-65e6f80692ab" } [20]=> array(2) { ["fieldoption"]=> string(11) "school_type" ["value"]=> string(9) "classbase" } [21]=> array(2) { ["fieldoption"]=> string(11) "school_year" ["value"]=> string(1) "1" } [22]=> array(2) { ["fieldoption"]=> string(5) "sname" ["value"]=> string(12) "florasjunior" } [23]=> array(2) { ["fieldoption"]=> string(17) "student_ID_format" ["value"]=> string(1) "1" } [24]=> array(2) { ["fieldoption"]=> string(13) "updateversion" ["value"]=> string(4) "3.00" } }

You dont have column with name "sname". Column names is fieldoption and value. So you need to create a function that will get the value of the column "valuae" for the line where the fieldoption equal to the value specified by you. Example:

PHP Code:
<?php
public function get_school() {
        return $this->get_setting("sname");
}
 
public function get_setting ($fieldoption){
    $this->db->where('fieldoption '$fieldoption );
        $data $this->db->get('setting');
        if($query){
        return $data->row()->value;
    }
    return false;
}
?>


AND render:

Code:
<h1><?=get_school()?</h1>



RE: Undefined valuable - InsiteFX - 05-10-2017

Here is a nice method to format var_dump values.

PHP Code:
/**
 * varDebug Helper - place in a helper file.
 * 
 * Formats the output of var_dump();
 * 
 */
if ( ! function_exists('varDebug'))
{
    
/**
     * Debug Helper
     * ----------------------------------------------------------------------------
     * 
     * Outputs the given variable(s) with color formatting and location
     * 
     * UUSAGE: varDebug(array);
     *
     * @param    mixed    - variables to be output
     */
    
function varDebug()
    {
        list(
$callee) = debug_backtrace();

        
$arguments func_get_args();

        
$total_arguments func_num_args();

        echo 
'<div><fieldset style="background: #fefefe !important; border:1px red solid; padding:15px">';
        echo 
'<legend style="background:lightgrey; padding:5px;">'.$callee['file'].' @line: '.$callee['line'].'</legend><pre><code>';

        
$i 0;
        foreach (
$arguments as $argument)
        {
            echo 
'<strong>Debug #'.++$i.' of '.$total_arguments.'</strong>: '.'<br>';
            
var_dump($argument);
        }

        echo 
"</code></pre></fieldset><div><br>";
        exit;
    }


Will make it easier to read in the forum's hers.


RE: Undefined valuable - kayinja.denis - 05-11-2017

(05-10-2017, 12:06 PM)InsiteFX Wrote: Here is a nice method to format var_dump values.

PHP Code:
/**
 * varDebug Helper - place in a helper file.
 * 
 * Formats the output of var_dump();
 * 
 */
if ( ! function_exists('varDebug'))
{
    
/**
     * Debug Helper
     * ----------------------------------------------------------------------------
     * 
     * Outputs the given variable(s) with color formatting and location
     * 
     * UUSAGE: varDebug(array);
     *
     * @param    mixed    - variables to be output
     */
    
function varDebug()
    {
        list(
$callee) = debug_backtrace();

        
$arguments func_get_args();

        
$total_arguments func_num_args();

        echo 
'<div><fieldset style="background: #fefefe !important; border:1px red solid; padding:15px">';
        echo 
'<legend style="background:lightgrey; padding:5px;">'.$callee['file'].' @line: '.$callee['line'].'</legend><pre><code>';

        
$i 0;
        foreach (
$arguments as $argument)
        {
            echo 
'<strong>Debug #'.++$i.' of '.$total_arguments.'</strong>: '.'<br>';
            
var_dump($argument);
        }

        echo 
"</code></pre></fieldset><div><br>";
        exit;
    }


Will make it easier to read in the forum's hers.
bro i don't understand how to use your function...