pass values from 2 tables to a view codeigniter - php

im having function that will display both buy and sell listings. Im trying to display both of these in one page.
im getting the values from 2 different tables and i wanr to pass both the values into the same template
Can someone please suggest how to do it?
controller
function leads(){
$this->load->model('listings');
$data['mylists']=$this->member_functions->mine();
$data['mylists2']=$this->member_functions->mine();
$data['heading']='headings/heading_view';
$data['body']='listbody';
$data['nav']='right';
$this->load->view('includes/template',$data);
}
Model
function mine(){
$mylists=$this->db->get('buy');
if ($mylists->num_rows()>0){
foreach ($mylists->result() as $a)
{
$data[]=$a;
}
return $data;
}
$mylists2=$this->db->get('sell');
if ($mylists2->num_rows>0)
{
foreach ($mylists->result() as $b)
{
$data[]=$b;
}
return $data;
}
}
View
<h2>Buy leads</h2>
<?php foreach ($mylists as $mylist):?>
<p><?php echo "$mylist->type1 in $mylist->country as $mylist->buyid" ?></p>
<?php endforeach;?>
</div>
<br />
<h2>Sell leads</h2>
<?php foreach ($mylists2 as $mylist2):?>
<p><?php echo "$mylist2->type1 in $mylist2->country" ?></p>
<?php endforeach;?>

You cannot use 2 return statements within the same function, since whenever the first is encountered...well, the function returns, and stops there. Try returning a single array with the 2 results instead, such as:
Model:
function mine(){
$mylists=$this->db->get('buy');
if ($mylists->num_rows()>0){
foreach ($mylists->result() as $a)
{
$data['res1'][]=$a;
}
}
$mylists2=$this->db->get('sell');
if ($mylists2->num_rows>0)
{
foreach ($mylists->result() as $b)
{
$data['res2'][]=$b;
}
}
return $data;
}
Controller:
$data['lists']=$this->member_functions->mine();
In your view, this array should be called like $lists['res1'] adn $lists['res2']

Related

How to make a search in codeiginiter without "like" paramameter?

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; ?>>

how to get the data from array when multiple model functions pass to the view from the controller

I have a controller it take multiple model function results and pass it to controller I did it like this
adpreview_ctrl.php
public function showBusinessReviews($vehicleid){
$data=array();
$data['details']=$this->ads_model->getBusinessReviews($vehicleid);
$data['noOfReviews']=$this->ads_model->countReviews($vehicleid);
$this->load->view('pages/templates/header');
$this->load->view('pages/viewReviews',$data);
$this->load->view('pages/templates/footer');
}
public function countBusinessReviews($vehicleid){
$data['details']=$this->ads_model->countReviews($vehicleid);
$this->load->view('pages/templates/header');
$this->load->view('pages/viewReviews',$data);
$this->load->view('pages/templates/footer');
}
}
viewReviews.php
<?php
foreach($noOfReviews as $reviewAmount){
echo $reviewAmount.'Reviews';
}
foreach($details as $review){
$Breview=$review->rating;
if($details==null)
{?>
<?php echo '<center><b><h3>No any reviews has been posted yet!</h3></b></center>';?>
<input type="submit" name="ok" class="btn btn-primary btn-lg" value="ok">
<?php }
else{
?>
Ads_model.php
public function getBusinessReviews($Vehicleid){
$status="Approved";
$this->db->select("*");
$query=$this->db->where('Vehicleid',$Vehicleid);
$query=$this->db->where('Status',$status);
$query=$this->db->get('businessreviews');
return $query->result();
}
public function countReviews($Vehicleid){
$status="Approved";
$this->db->select("*");
$query=$this->db->where('Vehicleid',$Vehicleid);
$query=$this->db->where('Status',$status);
$query=$this->db->get('businessreviews');
return $query->num_rows();
}
what I need to know is, it gives error saying it cannot identify $noOfReviews.
foreach($noOfReviews as $reviewAmount){
echo $reviewAmount.'Reviews';
}
I need to know how to retrieve multiple model function data in view,
and $noOfReviews only gives the no of reviews, a user has given. So their, without using a foreach loop how can i get the value of that, using a foreach loop is not necessary here.
Try this:
model:
public function countReviews($Vehicleid) {
$status = "Approved";
$this->db->select("*");
$query = $this->db->where('Vehicleid', $Vehicleid);
$query = $this->db->where('Status', $status);
// it will return with only the number of data
return $query->count_all_results('businessreviews');
}
viewReviews.php
<?php
// show number of reviews
echo $noOfReviews . 'Reviews';
// show details if exists
if(!empty($details)) {
foreach($details as $review) {
// echo what you want
}
} else {
echo 'No details.';
}
?>

How to display 'tree' data in the view in Cake PHP

I am doing the tree tutorial and would like to display the data in a view and wrap it in html etc. I am new to Cake PHP and this has been a bit of a pain. Below is an example of what I've tried. In short, I figure I could assign the output to a variable using set. I am doing everything wrong.
Controller
<?php
class CategoriesController extends AppController {
public function index() {
$this->set('output', $this->Category->generateTreeList(null, null, null, ' '));
}
}
?>
View
<?php foreach ($output as $data): ?>
<div><?php echo $data['Category']['name']; ?></div>
<?php endforeach; ?>
<?php unset($data); ?>
unlike other find methonds, generateTreeList doesen't return a named array but a plain array with numeric indexes
try print_r($output) and you'll see how the array is formatted and how you can use it in your view
to show your data, in your foreach cicle you just have to do this
echo $data;

Populating <select> with mysql data dynamically using CodeIgniter

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>

How to get field names of mysql table in codeigniter?

i am new in codeigniter. And i am trying to get field name of a table with a query.
I have write a query
"select * from user"
and pass it to $this->db->query() function. i am getting records. But i want to get field names of table. so how can i get that?
Can anybody help me please. Thanks in advance.
using database library write this code to list all fields:
$this->db->list_fields('table')
take a look here: https://codeigniter.com/userguide3/database/results.html#CI_DB_result::list_fields
Some Times this may helpful
$fields = $this->db->field_data('table_name');
foreach ($fields as $field)
{
echo $field->name;
echo $field->type;
echo $field->max_length;
echo $field->primary_key;
}
What you did is to get the datas from the table....
here your table is user so
in your model function do this...
function get_field()
{
$result = $this->db->list_fields('user');
foreach($result as $field)
{
$data[] = $field;
return $data;
}
}
in your controller do this
function get_field()
{
$data['field'] = $this->model_name->get_field();
$this->load->view('view_name',$data);
}
in your view do this
foreach($field as $f)
{
echo $f."<br>"; //this will echo all your fields
}
hope this will help you
you can use the below code to fetch the field from db
$fields = $this->db->list_fields('table_name');
foreach ($fields as $field)
{
echo $field;
}
with the help of this code we can fetch the field names from db
include('db.php');
$col="SHOW COLUMNS FROM `camera details`";
$output=mysqli_query($db,$col);
$kal=array();
while($row=mysqli_fetch_array($output))
{
if($row['Field']!='id')
{
?><div class="values"><?php echo $row['Field'];
echo "<br>";?></div><br><br><?php
array_push($kal, $row['Field']);
}
}
?>
<?php**
The answer given above by Anwar needs modification, there is no need for foreach loop in model function as it will only give the first element despite using foreach, which means the foreach loop is not bringing the fields using $data[], below code should give you 100% result
in your model function do this...
function get_field()
{
$result = $this->db->list_fields('user');
return $result();
}
in your controller do this
function get_field()
{
$data['field'] = $this->model_name->get_field();
$this->load->view('view_name',$data);
}
in your view do this
foreach($field as $f)
{
echo $f."<br>"; //this will echo all your fields
}

Categories