-
marksman Member
  
-
Posts: 92
Threads: 6
Joined: Apr 2016
Reputation:
1
Hi,
Im new in CodeIgniter,
I just want to know how to perform this in codeigniter in correct and clean way.
PHP Code: <?php $db = new mysqli("blah blah blah");
$query = $db->query("SELECT * FROM `users`");
while($data = $query->fetch_array()) { $FirstName = $data['FirstName']; $FirstName = str_replace("aaa","***",$FirstName); echo $FirstName."<br>"; }
I just want to perform while loop in my view with some methods like str_replace inside result_array();
God Bless CI Contributors
-
marksman Member
  
-
Posts: 92
Threads: 6
Joined: Apr 2016
Reputation:
1
05-07-2016, 06:18 PM
(This post was last modified: 05-07-2016, 08:25 PM by marksman.)
Hey guys, I just got it, I don't know if this is the proper way. Please correct me if I did it wrong or please suggest a better way.
Controller:
PHP Code: $query = $this->db->query("SELECT * FROM `users`"); foreach($query->result_array() AS $row) { $FirstName = $row['FirstName']; $FirstName = str_replace("aaa","***",$FirstName); $users[] = array("FirstName"=>$FirstName); } $data = array("users"=>$users); $this->load->view('myview',$data);
View:
PHP Code: <html> <body> <?php foreach($users AS $users) { echo $users['FirstName']."<br>"; } ?> </body> </html>
This will replace all " aaa" to "***" to the entire rows of column FirstName.
Did I perform it right? or there's a better way to do this?
God Bless CI Contributors
-
Wouter60 Posting Freak
    
-
Posts: 851
Threads: 38
Joined: Feb 2015
Reputation:
77
05-07-2016, 11:27 PM
(This post was last modified: 05-07-2016, 11:31 PM by Wouter60.)
Your approach isn't wrong, but I would prefer to replace the "aaa" in the foreach loop in the view.
That makes your controller code a lot easier:
PHP Code: $query = $this->db->query("SELECT * FROM `users`"); $data['users'] = $query->result_array(); $this->load->view('myview',$data);
In the view:
PHP Code: foreach($users AS $user) { echo str_replace('aaa','***',$user['FirstName']) . "<br />"; }
Also note that I changed "as $users" to "as $user", to make it clear that every row in the array is a single user.
-
marksman Member
  
-
Posts: 92
Threads: 6
Joined: Apr 2016
Reputation:
1
05-08-2016, 12:13 AM
(This post was last modified: 05-08-2016, 01:13 AM by marksman.)
(05-07-2016, 11:27 PM)Wouter60 Wrote: Your approach isn't wrong, but I would prefer to replace the "aaa" in the foreach loop in the view.
That makes your controller code a lot easier:
PHP Code: $query = $this->db->query("SELECT * FROM `users`"); $data['users'] = $query->result_array(); $this->load->view('myview',$data);
In the view:
PHP Code: foreach($users AS $user) { echo str_replace('aaa','***',$user['FirstName']) . "<br />"; }
Also note that I changed "as $users" to "as $user", to make it clear that every row in the array is a single user.
Hi, thanks for your reply, I put it in controller because str_replace is only an example, what if I want to create my own method/function, is it also advisable to put it in your view?? and if it is, how to do that with codeigniter?
God Bless CI Contributors
-
Wouter60 Posting Freak
    
-
Posts: 851
Threads: 38
Joined: Feb 2015
Reputation:
77
That's where the model concept comes in.
Create a model like this:
Filename: application/models/User_model.php
PHP Code: class User_model extends CI_Model { public function get_names_without_aaa() { $users = array(); $query = $this->db->get('users'); //get is a function in the query builder! foreach ($query->result_array() AS $row) { $FirstName = str_replace('aaa','***',$row['FirstName']); $users[] = array("FirstName"=>$FirstName); } return $users; } }
In the controller:
PHP Code: $this->load->model('user_model'); $data['users'] = $this->user_model->get_names_without_aaa(); $this->load->view('myview',$data);
Now, you can load the model in any controller you want, or even autoload it in application/config/autoload.php.
-
marksman Member
  
-
Posts: 92
Threads: 6
Joined: Apr 2016
Reputation:
1
05-08-2016, 05:58 AM
(This post was last modified: 05-08-2016, 06:08 AM by marksman.)
(05-08-2016, 04:05 AM)Wouter60 Wrote: That's where the model concept comes in.
Create a model like this:
Filename: application/models/User_model.php
PHP Code: class User_model extends CI_Model { public function get_names_without_aaa() { $users = array(); $query = $this->db->get('users'); //get is a function in the query builder! foreach ($query->result_array() AS $row) { $FirstName = str_replace('aaa','***',$row['FirstName']); $users[] = array("FirstName"=>$FirstName); } return $users; } }
In the controller:
PHP Code: $this->load->model('user_model'); $data['users'] = $this->user_model->get_names_without_aaa(); $this->load->view('myview',$data);
Now, you can load the model in any controller you want, or even autoload it in application/config/autoload.php.
(05-08-2016, 04:24 AM)InsiteFX Wrote: If it is going to be a common method that is used in all of your models then place it in a MY_Model
Wow, I have a lot of things to learn about CI  thanks for knowledgeable information you share. I'm not aware about autoload. but now I know how to use it.. before I use to initiate __construct() to all of my classes now I will use autoload  I'm so happy that I learn this thing
God Bless CI Contributors
|