I have problem - El Forum - 07-18-2011
[eluser]ibnu drim[/eluser]
I’m sure this is something trivial but I’ve spent the last hour scratching my head on this one!
I have several functions in my Model that all look like this (that find various things):
this is my controller :
Code: function post( $slug = FALSE )
{
if( empty( $slug ) )
show_404();
$this->load->model('model_posts');
$this->load->model('model_gallery');
//$posts = $this->model_gallery->getPosts();
$posts = $this->model_gallery->getChild( $slug );
$data = array(
'contentPage' => 'gallery',
'breadcrumb' => $this->model_posts->createBreadCrumb(),
'advTop' => FALSE,
'posts' => $posts
);
?>
<pre><?php //echo print_r($posts); ?></pre>
<?php
if( $posts )
$data['show'] = $posts->title;
$this->load->view('index', $data);
}
this is my Model :
Code: function getChild( $slug = FALSE )
{
$this->db->where('status', 1)
->where('parent', 0)
->where('slug', $slug)
->from('gallery');
$query = $this->db->get();
$posts = array();
foreach( $query->result() as $result )
{
$galler = array();
#has Child?
$child = $this->pagesHierarchy( $result->ID );
if( $child )
{
foreach( $child->result() as $c )
$galler['child'][] = $c;
}
$galler['parent'] = $result;
$posts[] = $galler ;
}
//return $posts ;
return !count( $posts )? FALSE:$posts;
}
function pagesHierarchy( $idParent = FALSE )
{
if( !$idParent || empty( $idParent ) )
return FALSE;
$this->db->where('parent', $idParent)
->from('gallery')
->order_by('ID', 'ASC');
$RES = $this->db->get();
return $RES->num_rows() <= 0? FALSE:$RES;
}
this is my View :
Code: <?php
if( $posts ):
?>
<table class="gallery-lists">
<?php
$count = 1; $tCount = 1;
foreach( $posts as $post ):
$primaryImg = $this->models->_unserialize( $post->image_primary );
$secondaryImg = $this->models->_unserialize( $post->image_secondary );
if( $count <= 1 ):
?>
<?php
endif;
?>
<?php $slug = $post->slug ;
echo site_url( array('gallery', $slug ) ); ?>
please help me solve this error.
Trying to get property of non-object & uninitialized string offset :0
Thank you
Ibnudrim
I have problem - El Forum - 07-18-2011
[eluser]nZac[/eluser]
Is there any errors from PHP, or are you just not getting the expected result?
I have problem - El Forum - 07-18-2011
[eluser]TheBaron[/eluser]
The first error is simple.
Code: $foo = '';
$foo->boo;
It appears on a cursory scan that you are assigning your posts to an array. You are then treating that array like an object.
Code: $post->title; // Wrong
$post['title'] // Correct
As for the second problem... I don't know where it is. Don't these errors have line numbers?
It is caused by using array syntax on a non-array... i.e.
Code: $foo = '';
echo $foo[0];
Hopefully this should help you find the problems.
I have problem - El Forum - 07-19-2011
[eluser]ibnu drim[/eluser]
this is my error in php :
Code: A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: libraries/Loader.php(673) : eval()'d code
and Code: A PHP Error was encountered
Severity: Notice
Message: Uninitialized string offset: 0
Filename: libraries/Loader.php(673) : eval()'d code
Line Number: 17
I just getting the expected result array : Code: Array
(
[0] => Array
(
[child] => Array
(
[0] => stdClass Object
(
[ID] => 6
[title] => test-child3
[content] =>
[excerpt] =>
[image_primary] => a:13:{s:9:"file_name";s:16:"banner_kiri1.jpg";s:9:"file_type";s:10:"image/jpeg";s:9:"file_path";s:49:"D:/Webxampp/xampp/htdocs/citralandkendari/upload/";s:9:"full_path";s:65:"D:/Webxampp/xampp/htdocs/citralandkendari/upload/banner_kiri1.jpg";s:8:"raw_name";s:12:"banner_kiri1";s:9:"orig_name";s:16:"banner_kiri1.jpg";s:8:"file_ext";s:4:".jpg";s:9:"file_size";s:5:"62.85";s:8:"is_image";s:1:"1";s:11:"image_width";s:3:"250";s:12:"image_height";s:3:"700";s:10:"image_type";s:4:"jpeg";s:14:"image_size_str";s:24:"width="250" height="700"";}
[image_secondary] =>
[article_post] => multiple
[slug] => test-child3
[is_dynamic] => 1
[allow_edit] => 1
[author] => 1
[author_modified] => 1
[parent] => 3
[status] => 1
[date_add] => 2011-06-23 15:09:48
[date_modified] => 2011-07-13 15:59:32
)
)
[parent] => stdClass Object
(
[ID] => 3
[title] => test 3
[content] =>
[excerpt] =>
[image_primary] => a:13:{s:9:"file_name";s:16:"banner_kiri1.jpg";s:9:"file_type";s:10:"image/jpeg";s:9:"file_path";s:49:"D:/Webxampp/xampp/htdocs/citralandkendari/upload/";s:9:"full_path";s:65:"D:/Webxampp/xampp/htdocs/citralandkendari/upload/banner_kiri1.jpg";s:8:"raw_name";s:12:"banner_kiri1";s:9:"orig_name";s:16:"banner_kiri1.jpg";s:8:"file_ext";s:4:".jpg";s:9:"file_size";s:5:"62.85";s:8:"is_image";s:1:"1";s:11:"image_width";s:3:"250";s:12:"image_height";s:3:"700";s:10:"image_type";s:4:"jpeg";s:14:"image_size_str";s:24:"width="250" height="700"";}
[image_secondary] =>
[article_post] => multiple
[slug] => test-3
[is_dynamic] => 0
[allow_edit] => 1
[author] => 1
[author_modified] => 1
[parent] => 0
[status] => 1
[date_add] => 2011-06-05 16:02:24
[date_modified] => 2011-07-13 18:24:29
)
)
)
and why do I get the error ..
ask the experts to help me ... ?!
I have problem - El Forum - 07-19-2011
[eluser]Aken[/eluser]
If the array you just posted is your $posts data, you're accessing the data incorrectly.
When you loop through it in your view via foreach, that single $post variable will contain this data structure:
Code: Array
(
[child] => Array
(
[0] => stdClass Object
(
[ID] => 6
[title] => test-child3
[content] =>
[excerpt] =>
[image_primary] => a:13:{s:9:"file_name";s:16:"banner_kiri1.jpg";s:9:"file_type";s:10:"image/jpeg";s:9:"file_path";s:49:"D:/Webxampp/xampp/htdocs/citralandkendari/upload/";s:9:"full_path";s:65:"D:/Webxampp/xampp/htdocs/citralandkendari/upload/banner_kiri1.jpg";s:8:"raw_name";s:12:"banner_kiri1";s:9:"orig_name";s:16:"banner_kiri1.jpg";s:8:"file_ext";s:4:".jpg";s:9:"file_size";s:5:"62.85";s:8:"is_image";s:1:"1";s:11:"image_width";s:3:"250";s:12:"image_height";s:3:"700";s:10:"image_type";s:4:"jpeg";s:14:"image_size_str";s:24:"width="250" height="700"";}
[image_secondary] =>
[article_post] => multiple
[slug] => test-child3
[is_dynamic] => 1
[allow_edit] => 1
[author] => 1
[author_modified] => 1
[parent] => 3
[status] => 1
[date_add] => 2011-06-23 15:09:48
[date_modified] => 2011-07-13 15:59:32
)
)
[parent] => stdClass Object
(
[ID] => 3
[title] => test 3
[content] =>
[excerpt] =>
[image_primary] => a:13:{s:9:"file_name";s:16:"banner_kiri1.jpg";s:9:"file_type";s:10:"image/jpeg";s:9:"file_path";s:49:"D:/Webxampp/xampp/htdocs/citralandkendari/upload/";s:9:"full_path";s:65:"D:/Webxampp/xampp/htdocs/citralandkendari/upload/banner_kiri1.jpg";s:8:"raw_name";s:12:"banner_kiri1";s:9:"orig_name";s:16:"banner_kiri1.jpg";s:8:"file_ext";s:4:".jpg";s:9:"file_size";s:5:"62.85";s:8:"is_image";s:1:"1";s:11:"image_width";s:3:"250";s:12:"image_height";s:3:"700";s:10:"image_type";s:4:"jpeg";s:14:"image_size_str";s:24:"width="250" height="700"";}
[image_secondary] =>
[article_post] => multiple
[slug] => test-3
[is_dynamic] => 0
[allow_edit] => 1
[author] => 1
[author_modified] => 1
[parent] => 0
[status] => 1
[date_add] => 2011-06-05 16:02:24
[date_modified] => 2011-07-13 18:24:29
)
)
If you are trying to access the parent data, you'll need to do so like this:
Code: $post['parent']->title;
$post['parent']->image_primary;
I have problem - El Forum - 07-19-2011
[eluser]ibnu drim[/eluser]
I want to access the child data ,,
and I get same array ,,,
this is my views ....,
Code: <?php
$count = 1; $tCount = 1;
foreach( $posts as $post ):
$primaryImg = $this->models->_unserialize( $post['child']->image_primary );
$secondaryImg = $this->models->_unserialize( $post['child']->image_secondary );
if( $count <= 1 ):
?>
?>
<tr>
<?php
endif;
?>
<td>
<a href="<?php echo base_url(); ?>img.php?act=view&src=<?php echo $primaryImg['file_name']; ?>" rel="group" class="colorbox">title; ?>">
<span class="img"><span><?php echo auto_resize( !$secondaryImg? $primaryImg:$secondaryImg, 100, 0, base_url(), FALSE ) ?></span></span>
<span class="description">
<span class="title"><?php echo $post['child']->title; ?></span>
<span class="text"><?php echo $post['child']->content; ?></span>
</span>
<br class="clear" />
</a>
</td>
<?php
//echo count( array($post) ,COUNT_RECURSIVE);
if( $count >= 2 || $tCount >= count( array($post), COUNT_RECURSIVE ) ):
?>
</tr>
<?php
endif;
?>
<?php
$count = $count >= 2? 1:( $count + 1 );
$tCount++;
endforeach;
?>
</table>
<?php
endif;
?>
</div>
<?php if( isset( $show ) ): ?>
[removed]$(function(){ cl.galleryShow( '<?php echo $show; ?>' ) });[removed]
<?php endif; ?>
this is my controller ..,,,,
Code: <?php
class Gallery extends Controller {
function Gallery()
{
parent::Controller();
$this->load->helper('image_helper');
}
function index( $slug = FALSE )
{
#show lists or post?
if( empty( $slug ) )
$this->lists();
else
$this->album( $slug );
}
function lists()
{
$this->load->model('model_posts');
$this->load->model('model_gallery');
$posts = $this->model_gallery->getPosts();
// $posts = $this->model_gallery->getChild();
$data = array(
'contentPage' => 'gallerys',
'breadcrumb' => $this->model_posts->createBreadCrumb(),
'advTop' => FALSE,
'posts' => $posts,
);
$this->load->view('index', $data);
}
function child()
{
$this->load->model('model_posts');
$this->load->model('model_gallery');
$posts = $this->model_gallery->getPosts();
$data = array(
'contentPage' => 'gallerychild',
'breadcrumb' => $this->model_posts->createBreadCrumb(),
'advTop' => FALSE,
'posts' => $posts,
'data' => $this->model_gallery->getByID( $this->input->get('id') )
);
$this->load->view('index', $data);
}
function album( $slug = FALSE )
{
if( empty( $slug ) )
show_404();
$this->load->model('model_posts');
$this->load->model('model_gallery');
$posts = $this->model_gallery->getPost();
$post = $this->model_gallery->getChild( $slug );
$data = array(
'contentPage' => 'gallery',
'breadcrumb' => $this->model_posts->createBreadCrumb(),
'advTop' => FALSE,
'posts' => $post
);
//if( $post )
//$data['show'] = $posts->title ;
$this->load->view('index', $data);
}
}
this is my models ..,,,,
function getChild( $slug = FALSE )
{
$this->db->where('status', 1)
->where('parent', 0)
->where('slug', $slug)
->from('gallery');
$query = $this->db->get();
$posts = array();
foreach( $query->result() as $result )
{
$galler = array();
#has Child?
$child = $this->pagesHierarchy( $result->ID );
if( $child )
{
foreach( $child->result() as $c )
$galler['child'][] = $c;
$galler['parent'] = $result;
$posts[] = $galler;
}
}
return $posts;
}
function pagesHierarchy( $idParent = FALSE )
{
if( !$idParent || empty( $idParent ) )
return FALSE;
$this->db->where('parent', $idParent)
->from('gallery')
->order_by('ID', 'ASC');
$RES = $this->db->get();
return $RES->num_rows() <= 0? FALSE:$RES;
}
Code: and I get same error in that php ,,
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: libraries/Loader.php(673) : eval()'d code
Line Number: 10
help me , anyone master ...
|