Fetch specific gender in Codeigniter using mysql Database - codeigniter

Hello guys i am developing matrimony web application my problem is i have database field gender. if user is logged in then check the user gender if user is male then fetch all the female from database and if user is female then fetch all the male.
controller
public function profile() {
$this->load->view('header');
$uname = $this->session->userdata('logged_in');
if ($uname['gender'] == 'male')
{
// $this->model_name->get_male();
echo 'male';
}
// $this->model_name->get_female();
else
{
echo 'female';
}
}
updated code but it's fail condition every time . i have compare session user name to check user is male or female but it fail
This is model
class brid_groom_fetch extends CI_Model {
function get_program_specific_gender() {
$result = $this->db->get('bride_groom_register');
return $result->result_array();
}

First thing is you need to check the user currently logged in gender.
Example:
if($user['gender'] == 'male'){
//run get male gender from database
$this->model_name->get_male();
} esle {
$this->model_name->get_female();
}
Model:
class brid_groom_fetch extends CI_Model {
function get_male() {
$result = $this->db->get('bride_groom_register');
$this->db->where('gender' = 'male');
return $result->result_array();
}
function get_female() {
$result = $this->db->get('bride_groom_register');
$this->db->where('gender' = 'female');
return $result->result_array();
}
}
Note: This is just an example to give you an idea. Hope this could help.

Related

Find user by name and surname in USER collection

This is my code:
public function search_clients($names){
$user = Auth::user();
$all_clients = $user->clients; // Retrieve object of all clients of user
$names = ["Pera","Peric"]; // Pera - name, Peric - surname
$collect = collect($all_clients);
$klijenti = $collect->filter(function($query) use ($names){
$query->whereIn('name', $names);
$query->orWhere(function($query) use ($names) {
$query->whereIn('surname', $names);
});
return $query;
});
return $klijenti;
}
I want to search in object for name and surname and return it. Like if I have 100 users in $all_clients, i want to return only users name or surname LIKE "PERA" or "PERIC".
Currently it is returning everyone.
An option would be to define a search scope on the Client model
class Client extends Model
{
public function scopeSearch($query, $searchTerm)
{
$query->where("name", "ilike", "%$searchTerm%")
->orWhere("surname", "ilike", "%$searchTerm%");
}
//rest of the code
}
Then in the controller method you can use the scope
public function search_clients($names)
{
$user = auth()->user();
$all_clients = $user->clients()->search($names)->get();
return $all_clients;
}

Trying to get referenced Laravel Eloquent data from another table

I'm just learning Laravel, and this is probably super easy for someone experienced with the framework, but I'm trying to get data from a different table and not sure what I'm doing wrong.
I have two tables, Users, which has fields first_name, last_name, and pair_id. The pair_id column is programmatically restricted to either null or an integer in two rows that designates a "pair". The there's the confirmed_pair table that has one pair per row and has a unique pair_id column, that refers to the one in the Users table.
In the ConfirmedPair model, I did this:
class ConfirmedPair extends Model
{
public function pair() {
return $this->hasMany(User::class, 'pair_id');
}
public function pair_names() { // returns an array of names of the players in the confirmed pair
$names = User::where("pair_id", $this->get(["pair_id"])[0]->pair_id)->get(['first_name','last_name']);
if (count($names) == 2) { // if this pair exists
return [
$names[0]['first_name']." ".$names[0]['last_name'],
$names[1]['first_name']." ".$names[1]['last_name']
];
}
return null;
}
}
In the controller, I did this:
class PageController extends Controller
{
public function index()
{
$confirmed_pair = new ConfirmedPair;
return view('page', compact('confirmed_pair'));
}
}
And in the blade view if I do this:
#for ($i = 1; $i < 10; $i++)
{{ $confirmed_pair->find($i)->pair_names()[0] }}<br>
#endfor
No matter what $i's value is, it returns the values of confirmed_pair id #1.
What am I doing wrong? Thanks!
If pair_id is assigned to 2 users
class ConfirmedPair extends Model {
public function pairs() {
return $this->hasMany(User::class, 'pair_id', 'pair_id');
}
public function pair_names() {
if ($this->pairs->count() != 2) return null;
// $this->pairs returns you a Collection of Users
// try to dd($this->pairs) to see in action
$pairs = $this->pairs->toArray();
return [
$pairs[0]['first_name']." ".$pairs[0]['last_name'],
$pairs[1]['first_name']." ".$pairs[1]['last_name']
];
}
}
Can be done better but for your case this should work.

how to view order submitted by customer to the seller?

A customer can post an order to the seller. The problem is how can seller(ps) can view his order.Because each order may be submitted to different seller.
SLotController.php
public function order(Request $request)
{
$slotorder = new Slotorder;
$slotorder->name = $request->name;
$slotorder->user_name = Auth::user()->name;
$slotorder->user_id = Auth::user()->id;
$slotorder->type = $request->type;
$slotorder->quantity = $request->quantity;
$slotorder->size = $request->size;
$slotorder->ps_id = ? // i dont know how to get seller id
$slotorder->save();
return view('home');
}
User model
public function slotorder()
{
return $this->hasMany('Slotorder::class');
}
SlotOrder model
public function user()
{
return $this->belongsTo('User::class');
}
public function user()
{
return $this->belongsTo('Ps::class');
}
Ps Model
public function slotorder()
{
return $this->hasMany('Slotorder::class');
}
Update
After user click make an order, it will go to this page according to their id. For this screenshot the id for the seller is 1. So back to my question , how can i get the seller id when user submit the order. Therefore he can view the order in his dashboard.
You are using a get route, which uses the seller id, so in the method which handles this route send the variable to the view, example:
Route
Route::get('giveorder/{seller_id}',Controller#method);
Controller Method
public function method($seller_id){
return view('giveorder',compact('seller_id'));
}
Create a hidden input inside your form:
<input type="hidden" value="{{$seller_id}}" name="seller_id">
So now you can use this seller_id in your order method:
public function order(Request $request)
{
$slotorder = new Slotorder;
$slotorder->name = $request->name;
$slotorder->user_name = Auth::user()->name;
$slotorder->user_id = Auth::user()->id;
$slotorder->type = $request->type;
$slotorder->quantity = $request->quantity;
$slotorder->size = $request->size;
$slotorder->ps_id = $request->seller_id;
$slotorder->save();
return view('home');
}

how to pass session variable into view using laravel4

I want to pass logged in id into my view page.i got the id in the function of user_login_submits.
Actually i want to get the id in one more function in the same controller.
how to get the session id in controller..
Normally session put its enough i did like that.
Here is my code anyone can check and tel me what need to change here
Controller
public function user_login_submits()
{
$inputs = Input::all();
$uname = Input::get('username');
$password = Input::get('password');
$logincheck=Userlogin::login_checks($uname,$password);
if($logincheck == 1)
{
$id=Session::get('customer_id');
return Redirect::to('businessprio/create_news?p=1');
}
else if($logincheck == 0)
{
//echo "fail";
return Redirect::to('businessprio/create');
}
}
Model
public static function login_checks($uname,$password)
{
$check = DB::table('customer_login')
->where('username','=',$uname)
->where('password','=',$password)->get();
if($check)
{
//Session::put(['customer_id'=>'value']);
Session::put('customer_id', $check[0]->customer_id);
Session::put('username', $check[0]->username);
return 1;
}
else
{
return 0;
}
}
I won't pass it to model, instead i would do it in controller itself,
public function user_login_submits()
{
$uname = Input::get('username');
$password = Input::get('password');
$check = DB::table('customer_login')
->where('username','=',$uname)
->where('password','=',$password)->count();
if($check==1)
{
$id=Session::get('customer_id');
return Redirect::to('businessprio/create_news?p=1');
}
else
{
return Redirect::to('businessprio/create');
}
}
Recommendation :
But i would strongly recommend you to do it by Auth::attempt i.e., to follow the clean one
public function user_login_submits()
{
if (Auth::attempt(['email' => $userEmail, 'password' => $userPassword])) {
return Redirect::to('businessprio/create_news?p=1');
}
else
{
return Redirect::to('businessprio/create');
}
}
If you do so, then you can access the Default checking for authenticated user
Auth::check()
Get the Logged in user details by
Auth::user()->id
Auth::user()->username
Note : To use default Auth::attempt you should use the User Model too.

GroceryCrud + CodeIgniter change value inside field

I'm using GroceryCrud for save data. User register is from website itself. When select their sex, i'm saving 1 for male, 2 for female. Database field is tinyint. So problem is, when admin view their data from backend, it's obvious 1 or 2 will appear on Sex field. How to change it into male, female depending on value?
You can use the callback_column for this.
In your case you can do:
public function webpages()
{
$c = new grocery_CRUD();
$c->set_table('users');
$c->columns('firstname','lastname','sex','age');
$c->callback_column('sex',array($this,'_callback_sex'));
$output = $c->render();
$this->_view_output($output);
}
public function _callback_sex($value, $row)
{
if ($value == '2') {
return 'male'
} elseif ($value == '1') {
return 'female';
} else {
return "-";
}
}

Resources