Welcome Guest, Not a member yet? Register   Sign In
what I'm doing wrong
#1

Hello,
I don't if what I'm doing wrong or just bug in CI 4.3.1 (latest version)
here is my problem
My table --
CREATE TABLE `tbposts` (
  `id` int(10) UNSIGNED NOT NULL,
  `user_id` int(11) NOT NULL,
  `title` varchar(128) NOT NULL,
  `slug` varchar(128) NOT NULL,
  `body` text NOT NULL
)  ;


INSERT INTO `tbposts` (`id`, `user_id`, `title`, `slug`, `body`) VALUES
(1, 2, 'my title test1', 'slug test', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac mattis elit.  '),
(2, 2, 'my title test2', 'slug test 2', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac mattis elit.  '),
(3, 2, 'my title test3', 'slug test 3', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac mattis elit.  '),
(4, 2, 'my title test4', 'slug test 4', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac mattis elit.  '),
(5, 6, 'my title test6', 'slug test 6', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac mattis elit.  '),
(6, 6, 'my title test7', 'slug test 7', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac mattis elit.  '),
(7, 4, 'my title test 8', 'slug test 8', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac mattis elit. ');


ALTER TABLE `tbposts`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `slug` (`slug`);


ALTER TABLE `tbposts`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
COMMIT;

My model:

<?php

namespace App\Models;
use CodeIgniter\Model;
use CodeIgniter\Database;


class ListingsModel extends Model
{
protected $table = 'tbposts';
              protected $primaryKey = 'id';

function __construct()
{
parent::__construct();
}


public function getpostsbyuser($user_id)
    {
     
        return $this->where(['user_id' => $user_id])->findall();
    }

}

?>


In my control :

$postModel = new \App\Models\ListingsModel();

  $data  = $postModel->getpostsbyuser(2);

 
      foreach ($data as $news_item):
 
    echo $data[0]['id'] .  $data[0]['id'] . '  '. $data[0]['title']  .'<br>';
 
        endforeach  ;

}


Getting result:
1  my title test1
1  my title test1
1  my title test1
1  my title test1


expected result show be:
1  my title test1
2  my title test2
3  my title test3
4  my title test4

I never had development obstacles like I'm having with CI 4.3, sorry to say lack of good documentation makes harder for new developers like me to continue with CI4 , sure I appreciate all hard work and time they put into this MVC , but all examples on the net old and not compatible with CI 4.3.
I know I got some help in the forum, I suggest put one good complete working example that works with latest version 4.3.1 in documentation with most usable scenarios for starters , for example for model just put one full working model not pieces and user trying to find what, which go where.
I have a lot of time, but I'm not knowledgeable enough to contribute in documentation
Thank you for all your hard work
Reply
#2

Code:
echo $data[0]['id'] .  $data[0]['id'] . '  '. $data[0]['title']  .'<br>';

Will always return the first result

try using 

Code:
echo $news_item['id'] .  $news_item['id'] . '  '. $news_item['title']  .'<br>';
Reply
#3

Thank you so much, that worked, I had that to see if works,
Now trying to get that into paging, but getting error

$postModel = new \App\Models\ListingsModel();
$data = $postModel->getpostsbyuser(2);

$params = [
'listings' => $data->paginate(10),
'pager' => $data->pager,
'total' => $data->countAllResults(),
'page' => isset($_GET['page']) ? $_GET['page'] : 1,
'perPage' => 12
];
return view('default', $params );

Getting error:
Call to a member function paginate() on array

Thanks again for any help
Reply




Theme © iAndrew 2016 - Forum software by © MyBB