Model:
function search()
{
$cari=$this->db->query("select * from uhd_user_order where user_order_reference ");
return $cari->result();;
}
Controller:
function search_keyword()
{
$this->input->GET('cari', TRUE);
$beda['cari'] = $this->order_test_m->search();
$this->load->view('admin/a',$beda);
}
View:
<?php if(count($cari)>0){
foreach ($cari as $row => $test) {
}?>
<?= $test->sendername ?>
<?php
} ?>
I need to make a search, and the result of the view will show up if I put the right code only. Like, if I only search "s", the data from my database will not show up.
How can this be done?
I am not sure what you want, I have some questions about your code, but I can not comment yet. I have to guess what happend:
I think your code works as: Whenever the 'search' button is clicked, all of your user's name in your database will be displayed, right?
Because your model does not use the 'get' data to search in the database
//Your function does not recieve the search string.
//This function uses $string to search in the 'users' table, to finds the name field == $string, and returns only 'name' row.
function search($string){
$query = $this->db->select('name')
->where('name',$string)
->get('users');
//If the result is empty, return false, do nothing.
if($query->num_rows() === 0){
return false;
}
return $query->result();;
}
Controller
function search_keyword()
{
// Call search function with the input data.
// I think here will have some validation
//for example:
//if($this->form_validation->run() == false){
// $this->load->view('errors/error');
//}
$search_string = $this->input->GET('cari', TRUE);
$beda['cari'] = $this->order_test_m->search($search_string);
$this->load->view('admin/a',$beda);
}
View:
// This will ensure the ul will only display when $cari is not empty.
<?php if(count($cari)>0):?>
<ul>
<?php foreach($cari as $row):?>
<li><?php echo $row->name;?></li>
<?php endforeach;?>
</ul>
<?php endif; ?>>
Related
I want to create a dynaic page, I have created model and controller and also data subitted in database successfully. Now, i'm having problem while displaying that data on front end.
Here is my Modal:
function getcorporate(){
$q="SELECT * from corporate";
$query=$this->db->query($q);
return $query->result_array();
}
Here is my Controller:
function corporate()
{
$popular['popular'] = $this->auth_model->getPopularcourses();
$data1['corporate'] = $this->auth_model->getcorporate();
$data["institute_details"] = $this->auth_model->getInstitutedetails();
$data1['course'] = $this->auth_model->getcoursesdetailes();
$this->load->view('nulearnFront/header', $data);
$this->load->view('nulearnFront/corporate', $data1);
$this->load->view('nulearnFront/footer', $popular);
}
Try this
First you can print_r() the data you receive.
print_r($corporate);
After that you can use foreach to display all the data
foreach($corporate as $value)
{
////do code according to your requirement
}
I hope this may be help out to solve your problem
Add View file this code
<?php
if (isset($corporate) && !empty($corporate)) {
foreach ($corporate as $cdata) {
echo $cdata->YourValue(db column name);
}
}
?>
You are so close to the answer. You are passing the data from your Controller class. So what you have to do is just get that data as the follows,
I get the corporate values as it is returning an array data. So here you go,
In your view.php file,
<?php
if (isset($corporate)) { // Check if the data is set or not
foreach ($corporate as $corporateData) {
?>
// Your HTML goes here, table or etc.
<?php echo $corporateData->databaseColumnName // Value that need to print from the database ?>
<?php
}
}
?>
Hope this helps you.
print the query and run it to check
function getcorporate(){
$q="SELECT * from corporate";
$query=$this->db->query($q);
print_r($this->db->last_query());die();
return $query->result_array();
}
if query works fine then you can foreah the query
foreach($corporate as $corporate)
{
echo corporate;
}
if it does not return result then change result_array() to result() in model
I'm working on a similar thing as this one. But I'm trying to assign button either "Join" or "Enter" based on if someone joined the group. The problem is that I'm not sure how I can pass the variable from the category ID ($cats_id) to the view file.
I created a function in the model that checks if the row exists and returns true.
// check if joined the group
public static function checkIfJoined($cats_id)
{
$database = DatabaseFactory::getFactory()->getConnection();
$users_id = Session::get('user_id');
$sql = "SELECT cats_id,users_id FROM categories_joined WHERE users_id = :users_id AND cats_id = :cats_id";
$query = $database->prepare($sql);
$query->execute(array(':users_id' => $users_id, ':cats_id' => $cats_id));
// fetchAll() is the PDO method that gets all result rows
if ($query->rowCount() >= 1 || Session::get('user_account_type') == 7) {
return true;
}
}
Then in Controller I render the model to the view.
public function index()
{
$cats_id = ""; // this doesn't work right obviously
$this->View->render('dashboard/index', array(
'categories' => DashboardModel::getAllCategories(),
'joined' => DashboardModel:: checkIfJoined($cats_id)
));
}
in the view I pass the variable from the preview function 'categories'.
<?php if ($this->categories) { ?>
<?php foreach($this->categories as $key => $value) { ?>
...
<?php $cats_id = $value->cat_id; if ( $this->joined == true ): ?>Enter
<?php else: ?>Join
<?php endif; ?>
You can never pass anything from view to controller because view is parsed after controller.
What you can do here is use model directly by calling DashboardModel::checkIfJoined($cats_id) in your view but that's not perfect approach.
It'll be better to prepare that data in the controller and then pass it to view.
Example controller
public function index()
{
$this->View->render('dashboard/index', array(
'categories' => DashboardModel::getAllCategories(),
'userCategories' => DashboardModel::getUserCategories()
));
}
Example view
<?php
if ($this->categories) {
foreach ($this->categories as $key => $value) {
if (in_array($value->id, $this->userCategories) {
echo 'Joined';
} else {
echo 'Join';
}
}
?>
In this example DashboardModel::getUserCategories() should return results from SELECT cats_id FROM categories_joined WHERE users_id = :users_id.
My database definitely contains values and I am trying to read these values for a specific column (name) into an array like so:
public function listcli()
{
$this->db->distinct();
$this->db->select('name');
}
}
then referencing to this function like so:
public function clist() {
$this->load->model('list_model');
$fields = $this->list_model->listcli();
$fieldl = $fields;
$data= array();
$data['fieldl'] = $fieldl;
$this->load->view('clientlist', $data);
}
This basically completes the database query in the model, passes the information into the controller where it puts the array into another array with a key (key being the same name as the array) so that I can pass it into my view, then my view which looks like this :
<html>
<body>
<p> <?php print_r($fieldl); ?></p>
<ul>
<?php
$fieldl = array();
?>
<p> <?php print_r($fieldl); ?> </p>
<?php
foreach($fieldl as $l) {
?>
<li>
<?php echo $l;?>
</li>
<?php
}
Then lists it
but even though I KNOW I have data in "name" the print_r is showing that my array is empty? Help please and thank you!
Are you sure your model is returning any value? If not, the return some:
public function listcli()
{
$this->db->distinct();
$this->db->select('name');
return $this->db->get('table_name')->result_array(); // return value
}
Please try the following
public function listcli(){
$this->db->distinct();
$this->db->select('name');
$query = $this->db->get('table_name');
return ($query->num_rows > 0 ? $query->result() : array());
}
If it does not help ...
Please give me table structure with some dummy date, So that I can help you resolve this.
I'm stacked on this matter. I have a model that retrieves data from a mysql database
class load_model extends CI_Model{
function __construct(){
parent::__construct();
}
Function loadsuppliers()
{
$this->db->select('SupplierID, Name');
$records=$this->db->get('supplier');
$data=array();
foreach ($records->result() as $row)
{
$data[$row->SupplierID] = $row->Name;
}
return ($data);
}
}
?>
This model submits value to a function in my controller
public function getSupplier()
{
$this->load->model('load_model');
$data['unit'] = $this->load_model->loadsuppliers();
$this->load->view('SupplierMGT', $data);
}
and I want to display the retrieved data to my view as a combo box. I tried to check if I am able to retrieve database values using echo json_encode($data) and it returns {"unit":{"2":"test","3":"Delta"}} ,
Could you help me with this? I tried using
<?php foreach($unit as $result):
print_r($result);
endforeach;?>
to check if i am able to pass the value but i failed.
Small changes in the model:
function loadsuppliers()
{
$this->db->select('SupplierID, Name');
$records=$this->db->get('supplier');
$data=array();
if($records->num_rows() > 0){
$data = $records->result_array();
}
return ($data);
}
In your view SupplierMGT.php write this:
<select name="" id="" multiple="">
<?php
if(isset($unit) && is_array($unit) && count($unit) > 0){
foreach($unit as $key=>$each){
?>
<option value="<?=$each['SupplierID']?>"><?=$each['Name']?></option>
<?php
}
}
?>
</select>
I'm trying to delete multiple private messages from my database by selecting multiple checkboxes in the inbox and clicking submit to delete. I have the code below but nothing happens. I'm not sure what I'm missing..
View:
<?php echo form_open('pm/remove_checked'); ?>
<?php foreach ($query as $row): ?>
<input type="checkbox" name="msg[]" value="<?php echo $row->id; ?>" />
<?php echo $row->from; ?>
<?php echo $row->subject; ?>
<?php echo date("m/d/Y",strtotime($row->msg_date)); ?>
<?php endforeach; ?>
<?php echo form_submit('delete', 'Delete'); ?>
</form>
Controller:
function remove_checked()
{
//validation rules
$this->form_validation->set_rules('msg[]', 'Private Message', 'required|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$data['query'] = $this->Pm_model->received_msg();
$this->load->view('pm/inbox', $data);
}
else //success
{
$checked_messages = $this->input->post('msg'); //selected messages
$this->Pm_model->delete_checked($checked_messages);
//redirect to inbox
}
}
Model:
function delete_checked($checked_messages)
{
$checked_messages = array();
foreach ($checked_messages as $msg_id):
$this->db->select('id');
$this->db->from('user_msg');
$this->db->where('id', $msg_id);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() > 0) //if message exists
{
$this->db->where('id', $msg_id);
$this->db->where('recipient', $this->users->get_user_id()); //verify if recipient id is equal to logged in user id
$this->db->delete('user_msg');
}
else
{
return FALSE;
}
endforeach;
}
In your current delete_checked() method, you are returning FALSE as soon as the first message is "found" that doesn't exist, this will prevent the rest of the messages from being deleted as return will stop execution of the loop. If you want to do it this way, use continue instead and consider using transactions.
If you don't particularly care about generating individual errors for each message, your model function can be simplified a bit:
function delete_checked($message_ids)
{
$this->db
->where_in('id', $message_ids)
->where('recipient', $this->users->get_user_id())
->delete('user_msg');
return $this->db->affected_rows() > 0;
}
This will just attempt to delete the records. If they don't exist they will be ignored, and $this->db->affected_rows() should return the number of messages deleted. You can compare it to count($message_ids) if you want to ensure that all messages selected were deleted, or use this example method that only checks if at least one message was deleted. If the message doesn't exist, you don't need to delete it anyways.
All the stuff Chris Schmitz mentioned is correct and important as well, you have some very basic errors. You may want to cast to array instead of assigning $checked_messages to an empty array if you expect you may be passing a single id (integer or string) to this function. Like this:
$message_ids = (array) $message_ids;
You are assigning $checked_msg to the inputs that are checked, but then you are passing a different variable called $checked_messages to the model. You'll want to pass the $checked_msg var to the model.
Also, in your model, you are redeclaring the $checked_messages var and setting it to an empty array. You'll need to remove that otherwise it will overwrite the info you are passing to the method.