How to get an id of the user in Codeigniter? - php

This is actually very easy but yet I am confused how to do this:
I have Reseller that can request X-number of users to admin. So that value gets saved to database. Now when admin logs in he can see the requests of resellers. Like this :
Now Admin Can either approve or reject the request. If He approves the request the code creates users in users table mapped to that reseller. The mapping is done with a unique field called Key Now Creating users works perfectly but my problem is :
When I click on Approve it creates users but for only for the reseller wit id = 1. I want to create something like when I click on first record it creates users for 1st reseller and when on 2nd same as above[This is as per the image].
This is my view:
<?php if(count($users)): foreach($users as $user): ?>
<tr class="odd gradeX">
<td><?php echo $user->id; ?></td>
<td><?php echo $user->user_requested; ?></td>
<td><?php echo $user->key; ?></td>
<td><?php echo $user->date_requested; ?></td>
<td>Yes&nbsp<i class="glyphicon glyphicon-ok"></i></td>
<td>No&nbsp<?php echo'<i class="glyphicon glyphicon-trash"></i>';?></td>
<td><?php echo $user->status; ?></td>&nbsp</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">There are no new Requests.</td>
</tr>
<?php endif; ?>
The Controller
public function create_user()
{
$this->load->model('more_m');
$id= $this->session->userdata('id');
$rajan =$this->more_m->get(array('user_requested',$id));
$request=$rajan->user_requested;
$this->load->model('reseller_m');
$query=$this->db->select('key');
$query=$this->db->get('reseller');
if($query->num_rows() > 0)
{
$row = $query->row_array();
$key= $row['key'];
}
for($i=1; $i<=$request;$i++)
{
$userdata=array('key'=>$key);
$this->db->insert('users',$userdata);
}
$this->load->model('more_m');
$id = $this->session->userdata('id');
$this->db->set('status', "'approved'",FALSE);
$this->db->where('id',$id);
$this->db->update('user_request');
redirect('admin/new_user');
}
My mistake is in controller where I define $id. As when admin logs in his id would be fetched I don't know how to fetch the id of the reseller as per the record.

Its so simple with Codeigniter
What you have to do is pass the ID with the URL (You are wrong Use base_url()as well with the pointing controller)
ex <td><a href="<?php echo base_url()?>reseller/create_user/<?php echo $user->key; ?>"
so now your URL looks like (assume id is equal to 2)
http://localhost/<path to project>/reseller/create_user/2
so in Controller
function create_user($id)//So this know incoming Value to this
{
//then in here
echo $id; //this will show 2
//Continue rest of your code with $id
}

If you wanna do it in php way,You can pass key in your url and get key in your controller using $this->uri->segment(n)
<?php if(count($users)): foreach($users as $user): ?>
<tr class="odd gradeX">
<td><?php echo $user->id; ?></td>
<td><?php echo $user->user_requested; ?></td>
<td><?php echo $user->key; ?></td>
<td><?php echo $user->date_requested; ?></td>
<td>Yes&nbsp<i class="glyphicon glyphicon-ok"></i></td>
<td>No&nbsp<?php echo'<i class="glyphicon glyphicon-trash"></i>';?></td>
<td><?php echo $user->status; ?></td>&nbsp</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">There are no new Requests.</td>
</tr>
<?php endif; ?>
If you want jquery+php than use ajax.
add value attr. to button pass your key.
<button id="some_id" value="YOURKEY">Click Me</button
This is your script to update :
<script>
$('body').on('click',"#some_id", function(){
var key =$(this).attr("value");
//Now you got ur key you can use ajax to update and change location of page on success
});
</script>

Related

multiple id in sql how to expload and get the id values in codeigniter

Multiple id in mysql table how to expload and get the particular id values in another mysql table for codeigniter,
Pages view images added,
View page
`<?php foreach($company_branch as $loop){ ?>
<tr>
<td></td>
<td><?=$loop->branch_name ?></td>
<td><?=$loop->branch_head ?></td>
<td><?=$loop->departments_list_id ?></td>
<td><?=$loop->write_date ?></td>
</tr>
<?php } ?>`
Controller Page
`public function company_settings() {
$data['company_branch'] = $this->settings_model->company_branch();
$this->load->view('settings/company_settings',$data);`
Model Page
`function company_branch(){
$this->db->select('company_branch.*,
company_departments.department_name as departments_name ')-
>from('company_branch');
$this->db- >join('company_departments','
company_branch.departments_list_id = company_departments.id');
$query = $this->db->get();
return $query->result();
}`
How to display the `departments_list_id ` to department names in view
page,
The image is company_branch and company_department mysql table view,
browser View page,
View Page
`<?php foreach($company_branch as $loop){ ?>
<tr>
<td></td>
<td><?=$loop->branch_name ?></td>
<td><?=$loop->branch_head ?></td>
<td>
<?php
foreach(explode(",",$loop->departments_list_id) as $department)
{ $depart=$this->db->query("select department_name from company_departments where id=".$department)->row()->department_name;
echo $depart.',<br />';
}
?>
</td>
<td><?=$loop->write_date ?></td>
</tr>
<?php } ?>`
You can't enter some id into 1 field. You can add some fields to another id. Example field: departement_id_1, departement_id_2, departement_id_4, departement_id_5 etc depending on need.
you can change your sql select to this :
select branc.*,
(select dept.departement_name as departement_name_1 from company_departement as dept where branc.departement_id_1=dept.id),
(select dept.departement_name as departement_name_2 from company_departement as dept where branc.departement_id_2=dept.id),
(select dept.departement_name as departement_name_3 from company_departement as dept where branc.departement_id_2=dept.id)
from company_branc as b
then you can use script from your form
<?php foreach($company_branch as $loop){ ?>
<tr>
<td></td>
<td><?=$loop->branch_name ?></td>
<td><?=$loop->branch_head ?></td>
<td><?=$loop->departments_name_1 ?></br>
<?=$loop->departments_name_2 ?></br>
<?=$loop->departments_name_3 ?>
</td>
<td><?=$loop->write_date ?></td>
</tr>
<?php } ?>
i hope this helps

Passing Parameters from View to Controller when a hyperlink is clicked and return parameters from Controller to view

This is View file. I am trying to pass- "$row['ISBN'] . ':SearchByAuthor:' . $Author"- abc:SearchByAuthor:aka - value to function increment_author to controller file.
<?php
?>
<tr>
<td><?php
echo $row['ISBN'];
?></td>
<td><?php
echo $row['name'];
?></td>
<td><?php
echo $row['title'];
?></td>
<td><?php
echo $row['year'];
?></td>
<td><?php
echo $row['price'];
?></td>
<td><?php
echo $row['publisher'];
?></td>
<td><?php
echo $row['number'];
?></td>
<td> <a href="<?php
echo base_url();
?>/increment_author/<?php
echo $row['ISBN'] . ':SearchByAuthor:' . $Author;
?>">
Add to cart</a></td>
</tr>
<?php
?>
This is contoller file and I am doing some calculations and trying the same to return to above search page, which is view. However, it's not working. I am trying to convert native php code to codeIgniter framework. Please help me out in passing parameters from Controller to View and vice versa. Thanks in Advance
public function increment_author($SearchByAuthor)
{
session_start();
$db = new mysqli('localhost','root','','cheapbook') or die('Error connecting to MySQL server.');
mysqli_set_charset($db, 'utf8');
if(isset($SearchByAuthor))
{
// echo $_GET['add'];
$pieces=explode(":", $SearchByAuthor);
$quantity="SELECT D.ISBN, D.title, sum(D.number) number FROM
(SELECT A.ISBN, title, number number FROM
book A, Stocks B WHERE A.ISBN='$pieces[0]' and
A.ISBN=B.ISBN)D
GROUP BY D.ISBN, D.title";
$result = mysqli_query($db,$quantity);
while($quantity_row = mysqli_fetch_array($result))
{
if($_SESSION['cart_'.$pieces[0]])
{
$chan=0;
}
else
{
$_SESSION['cart_count']+=1;
}
if($quantity_row['number']!=$_SESSION['cart_'.$pieces[0]] )
{
$_SESSION['cart_'.$pieces[0]]+=1;
$_SESSION["cartTotal"]+=1;
}
}
$data['SearchByAuthor']=$pieces[0];
}
//header("location:search_vi?SearchByAuthor=".$pieces[0]);
//$data['SearchByAuthor']=$pieces[0];
$this->load->view('search',$data);
}
You don't have to use session_start actually you should use Session Library.
Also there is a whole Database layer in CI.
Try to debug your problem in more details. Check if your $data variable contains proper result, and in your view this will be visible via $SearchByAuthor variable.

Dynamic href when generating buttons?

I have a table that is displaying data to the user and each row in the table has a unique id, in the table I am generating a button for each row to get more information on the content in the row. However, i need to generate a unique href for each button based on the Unique ID of the data in the row. The way I have done it it correctly goes to the link in the href but without passing the Unique ID.
<tbody>
<?php while($row = $query->fetch()) { ?>
<tr>
<td><?php echo $row['car_id']; ?></td>
<td><?php echo $row['car_make']; ?></td>
<td><?php echo $row['car_model']; ?></td>
<td><?php echo $row['number_available']; ?></td>
<td><?php echo $row['rent_cost_per_day']; ?></td>
<td><a class="btn btn-default" href="cars.php?"<?php $row['car_id']; ?>>View</a></td>
</tr>
<?php } ?>
</tbody>
Any help?
This is because you put the php after you closed the href quotes. You also have not put an echo function for the php, and have not put a get variable. You have:
href="cars.php?"<?php $row['car_id']; ?>
What you want to have is:
href="cars.php?id=<? echo $row['car_id']; ?>"

Get error "You do not have sufficient permissions to access this page" While creating custom plugin

I have created custom plugin in wordpress for get data from the database. And I have added two link to perfrom action on records .One is Edit and another is Delete. I have written code for delete link like this
Delete
When I click on delete link it will show error like
You do not have sufficient permissions to access this page
My plugin code is
function submit_review()
{
add_options_page("Submit Review","Submit Reviews",1,"submit_review","submit_review");
global $wpdb;
$id = get_current_user_id();
$q = "select * from wp_submit_review where user_id = " . $id;
$result = $wpdb->get_results($q,OBJECT);
if(!empty($result))
{
?>
<div class="wrap">
<h1>Submitted Reviews</h1>
<div style="width: 100%;">
<table cellpadding="5" class="wp-list-table widefat fixed pages">
<thead>
<tr>
<th><b>Review Id</b></th>
<th><b>User Name</b></th>
<th><b>Submitted Category</b></th>
<th><b>New Listing Name</b></th>
<th><b>New Listing Address</b></th>
<th><b>New Listing City</b></th>
<th><b>New Listing Description</b></th>
<th><b>Image</b></th>
<th><b>R-option</b></th>
<th><b>New Listing Rating</b></th>
<th><b>New Listing Review</b></th>
<th><b>Actions</b></th>
</tr>
</thead>
<tbody>
<?php
foreach($result as $res)
{
?>
<td><?php echo $res->review_id; ?></td>
<td><?php echo get_the_author_meta('user_login',$res->user_id); ?></td>
<td><?php
if($res->submit_category == 1)
{
echo "Service";
}
else if($res->submit_category == 2)
{
echo "Restaurant";
}
else
{
echo "Product";
}
?>
</td>
<td><?php echo $res->newlistingname; ?></td>
<td><?php echo $res->newlistingaddress; ?></td>
<td><?php echo $res->newlistingcity; ?></td>
<td><?php echo $res->newlistingdesc; ?></td>
<td><img src="<?php echo home_url()."/uploads/".$res->image; ?>" height="50px" width="50px"/></td>
<td><?php echo $res->roption; ?></td>
<td><?php echo $res->newlistingrating; ?></td>
<td><?php echo $res->newlistingreview; ?></td>
<td>Edit | Delete</td>
<?php
}
?>
</tbody>
</table>
</div>
</div>
<?php
}
else
{
echo "No review find";
}
}
function delete_my_review()
{
echo $_GET['id'];
}
add_action("admin_menu","delete_my_review");
function submit_review_menu()
{
add_menu_page("Submit Review","Submit Review","manage_options","submit_review", submit_review);
}
//add_action("admin_menu","submit_review");
add_action("admin_menu","submit_review_menu");
So how to call delete_my_review() function to perform delete action?
simply create a function with ajax and php. following example worked for me.
html delete button
<td><button onclick="delete_ab(<?php echo $results->id; ?>)">Delete</button></td> // pass your id to delete
javascript function
function delete_ab(str)
{
jQuery.post(
// see tip #1 for how we declare global javascript variables
MyAjax.ajaxurl,
{
// here we declare the parameters to send along with the request
// this means the following action hooks will be fired:
// wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
action : 'myajax_submit',
// other parameters can be added along with "action"
postID : str
},
function(response)
{
document.getElementById(str).style.display='none';
}
);
}
main function in php
function myajax_submit() {
// get the submitted parameters
$postID = $_POST['postID'];
global $wpdb;
$qry=$wpdb->query("DELETE FROM `price_table` WHERE id=$postID");
// generate the response
echo "hello";
// IMPORTANT: don't forget to "exit"
exit;
}

onclick redirect to url stored in php variable

I have the following table:
foreach ($forwards as $data):
?>
<tr style="cursor: pointer" class="main_row">
<td><?php echo $data['Offer']['id']; ?> </td>
<td><?php echo $data['Offer']['name'];?> </td>
<td><?php echo round($data['Stat']['ltr'],2)."%"; ?></td>
<td><?php echo round($data['Stat']['cpc'],2)."%"; ?></td>
<td><?php echo $data['Category']['name']?></td>
<td><?php echo $data['Country']['name'] ?></td>
</tr>
<?php
endforeach; ?>
now the idea is that when ever you click one of the main_row it should redirect to a new url. the problem is that the url contains an id for instance the url could look like www.example.com/details/2 where 2 is the id.
This id is like all of the other data stored in a php variable: $data['Offer']['id'];
Now my question how can i redirect using both php and javascript? is there a work around? .
Please do note that i am fully aware that php i server side and javascript is not. it is because of this i am asking this question.
<table>
<?php foreach ($forwards as $data): ?>
<tr data-link="http://www.example.com/details/<?php echo $data['Offer']['id']; ?>">
<td><?php echo $data['Offer']['id']; ?> </td>
<td><?php echo $data['Offer']['name'];?> </td>
<td><?php echo round($data['Stat']['ltr'],2)."%"; ?></td>
<td><?php echo round($data['Stat']['cpc'],2)."%"; ?></td>
<td><?php echo $data['Category']['name']?></td>
<td><?php echo $data['Country']['name'] ?></td>
</tr>
<?php endforeach; ?>
</table>
<script>
$('table tr').click(function(){
document.location.href = $(this).data('link');
});
</script>
if you use jQuery though.
You can get the ID from the first column in the row:
$(".main_row").click(function() {
var id = $(this).find("td:first-child").text();
window.location.href = 'www.example.com/details/' + id;
});
If you use jQuery do this:
$('.main_row').click(function(){
window.location.href = "YOUR_URL"+$(this).attr('id');
});
And modify the html for the row to look like:
<tr style="cursor: pointer" class="main_row" id="<?php echo $id ?>">
This way - any time you click on the row it appends the row ID to the url and redirect you there.
If I am understanding you question correctly, your not looking for a redirect. To redirect in php, it must be done before you echo anything to the browser.
Because you want to "redirect" after your display is rendered, you should be using anchors.
<td><a href="www.example.com/details/<?php echo $data['Offer']['id']; ?>">
<?php echo $data['Offer']['id']; ?>
</a></td>
The anchor will direct the browser to the href url.

Categories