Get city value based on user_id from database Laravel - php

I need to get the value from the table - privacy_settings, column - city, based on user_id which is on my view {{ $user['id'] }} from table users, column id.
Here is my controller :
public function findUser($role = null,$search = null,$name = null,$country = null,$city = null,$industry = null,$department = null,$function_name = null,$timeframe =null,$language_skills = null,$organization_type = null,$contact_level = null,$page = null)
{
// if($page == "")
// {
// return redirect('find-user/'.Input::get('role').'/'.Input::get('search').'/1');
// }
// return $role;
// if($page == "")
// {
// $page = 1;
// }
$data = $this->data;
$data['users'] = User::all();
$page = Input::get('page'); // Get the current page or default to 1, this is what you miss!
$perPage = 12;
$offset = ($page * $perPage) - $perPage;
$data['role'] = Input::get('role');
$search = Input::get('search');
$name = Input::get('name');
$country = Input::get('country');
$city = Input::get('city');
$industry = Input::get('industry');
$language_skills = Input::get('language_skills');
$department = Input::get('department');
$function_name = Input::get('function_name');
$company = Input::get('company');
$timeframe = Input::get('timeframe');
$organization_type = Input::get('organization_type');
$contact_level = Input::get('contact_level');
$data['search'] = $search;
// DB::enableQueryLog();
$users = User::with('country','industry','organization_type','career_path','career_path.industry','career_path.department','career_path.functions','role');
$private = DB::table('privacy_settings')->get();
How can I do that? I guess it should be something like $private = DB::table('privacy_settings')->where('id', '=', 'city')->get();, but this doesn't working.
So, now I get the id o the user with {{ $user['id'] }} , from table Users, column id.
In table privacy_settings I have column user_id which has id of users and city with 2 values : 0 for public, and 1 for private. I need to get these values based on every user_id

So basically, fetch the collection from column user_id where user_id = the id you passed in your blade. replace $user['id'] with that value.
$temparr = [];
$buffer = 0;
foreach($users as $user){
$arr = [];
$privacy = PrivancySetting::where('user_id', $user['id'])->first();
$city = $privacy['city'];
if($city == 0){
$buffer = 0;
$arr['id'] = $user['id'];
$arr['perm'] = $buffer;
array_push($temparr, $arr);
}else{
$buffer = 1;
$arr['id'] = $user['id'];
$arr['perm'] = $buffer;
array_push($temparr, $arr);
}
}
Pass $temparr to your view and do a loop in it to cross check and hide what you need to hide!

Related

unable to insert multiple array data from dynamic generated field?

I am unable to enter multiple data, it enter only single data. I have tried using for loop and then entering data, using 3 user and 2 task, there is an error previously offset.
public function add($postData)
{
// dd($postData);
$c = count($postData['user_name']);
$t = count($postData['task_name']);
for ($i = 0; $i < $c; $i++) {
$user_name = $postData['user_name'][$i];
$user_email = $postData['user_email'][$i];
$data['insert']['user_name'] = $user_name;
$data['insert']['user_email'] = $user_email;
}
for ($j = 0; $j < $t; $j++) {
$task_name = $postData['task_name'][$j];
$data['insert']['task_name'] = $task_name;
}
$data['insert']['name'] = $postData['name'];
$data['insert']['description'] = $postData['description'];
$data['insert']['customer_name'] = $postData['customer_name'];
$data['insert']['billing_method'] = $postData['billing_method'];
$data['insert']['dt_created'] = DT;
$data['table'] = PROJECT;
$result = $this->insertRecord($data);
if ($result == true) {
$response['status'] = 'success';
$response['message'] = 'Project created';
} else {
$response['status'] = 'danger';
$response['message'] = DEFAULT_MESSAGE;
}
return $response;
}
As Per lack of question details attached, I am supposing that you want to insert multiple task entry with project name, description etc.
Here is updated code:
<?php
// dd($postData);
$username = $postData['username'];
$user_email = $postData['user_email'];
$task_name = $postData['task_name'];
foreach ($username as $key => $value) {
$data['insert']['name'] = $postData['name'];
$data['insert']['description'] = $postData['description'];
$data['insert']['customer_name'] = $postData['customer_name'];
$data['insert']['billing_method'] = $postData['billing_method'];
$data['insert']['username'] = $value;
$data['insert']['user_email'] = $user_email[$key];
$data['insert']['task_name'] = $task_name[$key];
$data['insert']['dt_created'] = DT;
$data['table'] = PROJECT;
$result = $this->insertRecord($data);
}

How to insert in to two tables from an online from?

Is it possible to insert in to two tables at once? I need to insert some data in to a table (students) and then based on the primary key insert in to another (enrollments). Is this possible?
The enrollments table however pulls other primary key from another table called schools (id).
public function add()
{
if(isset($_POST['submit'])) {
$this->student_validation();
if($this->form_validation->run() === TRUE) {
$data = $this->_get_posted_student_data();
$insert_id = $this->student->insert('students', $data);
if($insert_id) {
$this->__insert_enrollment($insert_id);
success($this->lang->line('insert_success'));
redirect('web/index/' . $data['front_school_id']);
} else {
error($this->lang->line('insert_failed'));
redirect('web/admission');
}
} else {
$this->data['post'] = $_POST;
}
}
$this->data['schools'] = $this->schools;
$this->data['add'] = TRUE;
$this->layout->title($this->lang->line('add') . ' ' . $this->lang->line('student') . ' | ' . SMS);
$this->layout->view('web/index', $this->data);
}
private function __insert_enrollment($insert_id)
{
$data = array();
$school = $this->student->get_school_by_id($this->input->post('front_school_id'));
$data['student_id'] = $insert_id;
$data['school_id'] = $this->input->post('front_school_id');
//$data['class_id'] = $this->input->post('class_id');
//$data['section_id'] = $this->input->post('section_id');
$data['academic_year_id'] = $school->academic_year_id;
//$data['roll_no'] = $this->input->post('roll_no');
$data['created_at'] = date('Y-m-d H:i:s');
//$data['created_by'] = logged_in_user_id();
$data['status'] = 1;
$this->db->insert('enrollments', $data);
}
private function _get_posted_student_data()
{
$items = array();
$school_id = $this->session->userdata('front_school_id');
$items[] = 'first_name';
$items[] = 'middle_name';
$items[] = 'last_name';
$items[] = 'gender';
$items[] = 'religion';
$items[] = 'dobpin';
$items[] = 'address';
$items[] = 'immunization_update';
$items[] = 'nationality';
$items[] = 'valid_permit';
$items[] = 'relation_father';
$items[] = 'father_name';
$items[] = 'father_phone';
$items[] = 'father_email';
$items[] = 'father_address';
$items[] = 'relation_mother';
$items[] = 'mother_name';
$items[] = 'mother_phone';
$items[] = 'mother_email';
$items[] = 'mother_address';
$items[] = 'other_sibling';
$items[] = 'name_sibling';
$items[] = 'class_sibling';
$items[] = 'name';
$items[] = 'relation';
$items[] = 'child';
$items[] = 'application_name';
$data = elements($items, $_POST);
$data['dob'] = date('Y-m-d', strtotime($this->input->post('dob')));
$data['admission_date'] = date('Y-m-d', strtotime($this->input->post('admission_date')));
$data['age'] = floor((time() - strtotime($data['dob'])) / 31556926);
$data['modified_at'] = date('Y-m-d H:i:s');
//$data['modified_by'] = logged_in_user_id();
$data['created_at'] = date('Y-m-d H:i:s');
//$data['created_by'] = logged_in_user_id();
$data['status'] = 1;
// create user
//$data['user_id'] = $this->student->create_user();
if($_FILES['birth_certificate']['name']) {
$data['birth_certificate'] = $this->_upload_birth_certificate();
}
if($_FILES['bill']['name']) {
$data['bill'] = $this->_upload_bill();
}
if($_FILES['immunization']['name']) {
$data['immunization'] = $this->_upload_immunization();
}
if($_FILES['photo']['name']) {
$data['photo'] = $this->_upload_photo();
}
return $data;
}
You need $this->db->insert_id() to get last inserted id which will get from this.
$this->db->insert('students', $data);
$insert_id = $this->db->insert_id();
if(!empty($insert_id)) {
$this->__insert_enrollment($insert_id);
success($this->lang->line('insert_success'));
redirect('web/index/' . $data['front_school_id']);
} else {
error($this->lang->line('insert_failed'));
redirect('web/admission');
}

want to ask why the data that enter only the upper part only(insert multiple)

The data does not want to loop. When it is stored that is stored only the topmost data only. I want to change the model in codeigniter not allowed to use the code.
public function saveTrans1(){
$product = $this->input->post('product');
$product_id = $this->input->post('product_id');
$salutation = $this->input->post('salutation');
$fullname = $this->input->post('fullname');
$email = $this->input->post('email');
$company = $this->input->post('company');
$jobtitle = $this->input->post('jobtitle');
$address = $this->input->post('address');
$phonenumber = $this->input->post('phonenumber');
$personal = $this->input->post('personal');
$industry = $this->input->post('industry');
$y = count($salutation);
$nom=0;
$nom++;
for ($i = 0; $i < $y; $i++) {
$postData['salutation'] = $salutation[$i];
$postData['fullname'] = $fullname[$i];
$postData['email'] = $email[$i];
$postData['company'] = $company[$i];
$postData['jobtitle'] = $jobtitle[$i];
$postData['address'] = $address[$i];
$postData['phonenumber'] = $phonenumber[$i];
$postData['personal'] = $personal[$i];
$postData['industry'] = $industry[$i];
$postData['product_name']=$product;
$postData['product_id']=$product_id;
$postData['password']=$this->randomKey(6);
//print ($i);
$this->v2->inject_transaction_member($postData);
}
}

Unknown column in where clause in codeigniter query

I have a codeigniter project. I have some dropdowns where i can select campus, session, class, group. I want filter data according to these dropdown values. Here is the function i call-
function student_feeConfig()
{
if ($this->session->userdata('admin_login') != 1)
redirect(base_url(), 'refresh');
$campus_id = -1;
$session_id = -1;
$class_id = -1;
$group_id = -1;
if($this->input->post('campus_id') !=null)
$campus_id = $this->input->post('campus_id');
if($this->input->post('session_id') !=null)
$session_id = $this->input->post('session_id');
if($this->input->post('class_id') !=null)
$class_id = $this->input->post('class_id');
if($this->input->post('group_id') !=null)
$group_id = $this->input->post('group_id');
if($this->input->post('section_id') !=null)
$section_id = $this->input->post('section_id');
$campus = $this->db->get('campus')->result_array();
$page_data['campus'] = array(''=>'Select one');
foreach($campus as $row):
$page_data['campus'][$row['id']] = $row['campus_name'];
endforeach;
$page_data['id'] = $campus_id;
$session = $this->db->get('session')->result_array();
$page_data['sessions'] = array(''=>'Select one');
foreach($session as $row):
$page_data['sessions'][$row['id']] = $row['uniqueCode'];
endforeach;
$page_data['session_id'] = $session_id;
$classinfo = $this->db->get_where('class', array('campus_id' => $campus_id))->result_array();
$page_data['classes'] = array(''=>'Select one');
foreach($classinfo as $row):
$page_data['classes'][$row['class_id']] = $row['name'];
endforeach;
$page_data['allclass']=$page_data['classes'];
$page_data['class_id'] = $class_id;
$groups = $this->db->get_where('class_group', array('class_id' => $class_id))->result_array();
$page_data['groups'] = array(''=>'Select one');
foreach($groups as $row):
$page_data['groups'][$row['id']] = $row['group_name'];
endforeach;
$page_data['group_id'] = $group_id;
if($class_id != -1) {
$this->db->select('enroll.group_id, student_feeConfig.*');
$this->db->from('enroll');
$this->db->join('student_feeConfig', 'enroll.student_id = student_feeConfig.student_id');
$this->db->where('enroll.class_id', $class_id);
$feeInfo = $this->db->get()->result_array();
$page_data['feeInfo'] = $feeInfo;
}
$page_data['page_name'] = 'student_feeconf';
$page_data['page_title'] = get_phrase('fee_management');
$this->load->view('backend/index', $page_data);
}
But i get error:
Error Number: 1054
Unknown column 'enroll.class_id' in 'where clause'
UPDATE `ci_sessions` SET `timestamp` = 1478060781 WHERE `enroll`.`class_id` = '4' AND `id` = 'a351278bf35ab714c6f3de3479627cb5c009c7a6'
Filename: libraries/Session/drivers/Session_database_driver.php
Line Number: 243
Your code is :
if($class_id != -1) {
$this->db->select('enroll.group_id, student_feeConfig.*');
$this->db->from('enroll');
$this->db->join('student_feeConfig', 'enroll.student_id = student_feeConfig.student_id');
$this->db->where('enroll.class_id', $class_id);
$feeInfo = $this->db->ge()->result_array();
$page_data['feeInfo'] = $feeInfo;
}
bellow line you used ->db->ge() like this
$feeInfo = $this->db->ge()->result_array();
Correction is bellow :
$feeInfo = $this->db->get()->result_array();
and add your enroll.class_id like bellow after if($class_id != -1) { condition:
$this->db->select('enroll.group_id, enroll.class_id, student_feeConfig.*');

Previous/next button in PHP

I´m pretty much entirely new to PHP, so please bear with me.
I´m trying to build a website running on a cms called Core. I'm trying to make it so that the previous/next buttons cycle through tags rather than entries. Tags are stored in a database as core_tags. Each tag has it own tag_id, which is a number. I've tried changing the excisting code for thep previous/next buttons, but it keeps giving me 'Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/core/functions/get_entry.php on line 50'.'
Any help would be greatly appreciated.
Get_entry.php:
<?php
$b = $_SERVER['REQUEST_URI'];
if($entry) {
$b = substr($b,0,strrpos($b,"/")) . "/core/";
$id = $entry;
$isPerma = true;
} else {
$b = substr($b,0,mb_strrpos($b,"/core/")+6);
$id = $_REQUEST["id"];
}
$root = $_SERVER['DOCUMENT_ROOT'] . $b;
$http = "http://" . $_SERVER['HTTP_HOST'] . substr($b,0,strlen($b)-5);
require_once($root . "user/configuration.php");
require_once($root . "themes/".$theme."/configuration.php");
require_once($root . "functions/session.php");
if(is_numeric($id)) {
$type = "entry";
} else {
$type = "page";
}
$id = secure($id);
if($type == "page") {
$data = mysql_query("SELECT p.* FROM core_pages p WHERE p.page_title = \"$id\"");
$page_clicks = 0;
while($p = mysql_fetch_array($data)) {
$url = $p["page_url"];
$path = $root . "user/pages/" . $url;
$page_clicks = $p['hits']+1;
require($path);
}
mysql_query("UPDATE core_pages p SET
p.hits = $page_clicks
WHERE p.page_title = $id");
}
if($type == "entry") {
// queries the dbase
$data_tags = mysql_query("SELECT entry_id,entry_title FROM core_entries WHERE entry_show = 1 ORDER BY entry_position DESC") or die(mysql_error());
$navArr=array();
while($tmparray = mysql_fetch_array($data_entries,MYSQL_ASSOC)){
array_push($navArr,$tmparray['entry_id']);
}
function array_next_previous($array, $value) {
$index = array_search($value,$array);
//if user clicked to view the very first entry
if($value == reset($array)){
$return['prev'] = end($array);
$return['next'] = $array[$index + 1];
//if user clicked to view the very last entry
}else if($value == end($array)){
$return['prev'] = $array[$index - 1];
reset($array);
$return['next'] = current($array);
}else{
$return['next'] = $array[$index + 1];
$return['prev'] = $array[$index - 1];
}
return $return;
}
$data = mysql_query("SELECT e.* FROM core_entries e WHERE e.entry_id = $id AND e.entry_show = 1");
$entry_clicks = 0;
if(#mysql_num_rows($data) < 1) {
die("Invalid id, no entry to be shown");
}
while($e = mysql_fetch_array($data)) {
$nextPrevProject = array_next_previous($navArr,$id);
$entry_id = $e['entry_id'];
$entry_title = $e['entry_title'];
// DATE
$t = $e["entry_date"];
$y = substr($t,0,4);
$m = substr($t,5,2);
$d = substr($t,8,2);
$entry_date = date($date_format,mktime(0,0,0,$m,$d,$y));
$entry_text = $e['entry_text'];
$entry_extra1 = $e['entry_extra1'];
$entry_extra2 = $e['entry_extra2'];
$entry_client = $e['entry_client'];
$entry_position = $e['entry_position'];
$entry_hits = $e['hits']+1;
$entry_new = $e['entry_new'];
if($entry_new == 1) {
$isNew = true;
} else {
$isNew = false;
}
if($nice_permalinks) {
$entry_perma = "$http".$entry_id;
} else {
$entry_perma = "$http"."?entry=$entry_id";
}
$data_e2t = #mysql_query("SELECT e2t.tag_id FROM core_entry2tag e2t WHERE e2t.entry_id = $entry_id");
$tag_str = "";
while($e2t = #mysql_fetch_array($data_e2t)) {
$tag_id = $e2t["tag_id"];
$data_tags = #mysql_query("SELECT t.tag_text FROM core_tags t WHERE t.tag_id = $tag_id");
while($t = #mysql_fetch_array($data_tags)) {
$tag_text = $t["tag_text"];
$tag_str = $tag_str . "<a class=\"tag-link\" name=\"tag".$tag_id."\" href=\"#tag-"._encode($tag_text)."\">".$tag_text."</a>".$separator_tags;
}
}
$entry_tags = substr($tag_str,0,strlen($tag_str)-strlen($separator_tags));
$layout_path = $root . "user/uploads/" . treat_string($entry_title) . "/layout.php";
if(is_file($layout_path) && (#filesize($layout_path) > 0)) {
require($layout_path);
} else {
require($theme_path . "parts/entry.php");
}
}
mysql_query("UPDATE core_entries e SET
e.hits = $entry_hits
WHERE e.entry_id = $id");
}
if($isPerma) {
echo "<a class=\"index-link\" href=\"$http\">back to index</a>";
}
?>
You have not defined $data_entries, before using it here:
while($tmparray = mysql_fetch_array($data_entries,MYSQL_ASSOC)){
array_push($navArr,$tmparray['entry_id']);
}
That is why you get the very descriptive error message.
Did you mean to use $data_tags?
Use: "SELECT p.* FROM core_pages p WHERE p.page_title = '".$id."'
Note: mysql_connect is not sql-injection save. If you use mysql_connect, change to PDO.
$data_entries is not defined on line 50, then mysql_fetch_array return an exception of null value given.
Try to change $tmparray = mysql_fetch_array($data_entries,MYSQL_ASSOC) to $tmparray = mysql_fetch_array($data_tags,MYSQL_ASSOC).
Hope this help!

Categories