Welcome Guest, Not a member yet? Register   Sign In
Undefined valuable
#1

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


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

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

Attached Files
.php   exam.php (Size: 5.39 KB / Downloads: 104)
.php   view.php (Size: 23.62 KB / Downloads: 127)
.php   setting_m.php (Size: 2.16 KB / Downloads: 85)
Reply
#2

(This post was last modified: 05-09-2017, 01:10 AM by neuron.)

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
Reply
#3

@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.
Reply
#4

(This post was last modified: 05-10-2017, 02:52 AM by neuron.)

(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
Reply
#5

(This post was last modified: 05-10-2017, 03:32 AM by kayinja.denis.)

@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
Reply
#6

Show a dump of the object or array that is returned from your query.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(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" } }
Reply
#8

(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>
Reply
#9

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB