I want to load another view using the function within the same controller. I'm new to codeIgniter so Please go easy on me :D
loaded home page by default -> From Home page filled in fields -> submit -> using ajax grabbed data from database -> use data on another page by loading the new view in the same controller as the default.
Controller:
<?php
class logInCon extends CI_Controller
{
public function login()
{
//$data['creds'] = $this->accsModel->getUser();
$this->load->view("Login");
}
public function validate_LogIn()
{
$uname = $this->input->post('uname');
$pass = $this->input->post('pass');
$this->load->model("accsModel");
$data = $this->accsModel->logInCheck($uname, $pass);
$check = $data['verified'];
if ($check <= 0)
{
echo "Not a valid member";
}else
{
$data['logged'] = $this->accsModel->getUser($uname, $pass);
$this->load->view('comms.php');//want this to load
}
}
}
?>
Model
<?php
/**
*/
class accsModel extends CI_Model
{
public function getBps()
{
$query = $this->db->get('bps');
return $query->result();
}
public function getUser($uname, $psswrd)
{
$getCreds = $this->db->query("SELECT `bps`.*, `users`.bps_id
FROM `bps`
LEFT JOIN `users`
ON `bps`.id = `users`.bps_id
AND `users`.`uname` = '$uname'
AND `users`.`pwd` ='$psswrd'
WHERE `users`.bps_id != ''
OR `users`.bps_id IS NOT NULL");
return $getCreds->result();
}
public function logInCheck($uname, $psswrd)
{
$this->db->select('COUNT(*) AS verified');
$this->db->where('uname', $uname);
$this->db->where('pwd', $psswrd);
$this->db->limit(1);
return $this->db->get('users')->row_array();
}
}
?>
View
<body>
<div class="container">
<div class="row">
<div class="LogForm">
<div class="col-md-12">
<input type="text" id="bpCode" name="bpCode">
</div>
<div class="col-md-12">
<input type="password" id="pass" name="pass">
</div>
<div class="btn">
<span id="emptyF" hidden style="color:red;"></span>
<button id="submit" class="btn btn-info btn-lg btn-block">Login</button>
<span id="result"></span>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function()
{
$("#submit").click(function()
{
var uname = $('#bpCode').val();
var pass = $('#pass').val();
if(uname == "" || pass =="")
{
$("#emptyF").attr("hidden", false).html("Please fill in the form");
}else{
$("#emptyF").attr("hidden", true);
$.ajax({//start of ajax function
type: "POST",
url: "<?php echo base_url('logInCon/validate_LogIn'); ?>",
data:
{
uname : uname,
pass : pass
}
//end of ajax function
});
}
});
});
</script>
</body>
View comms want this to be loaded and use the the data from the getUser() function
<body>
<p><?php print_r($logged) ?></p>
<h1>Commissions View</h1>
<table style="width:100%">
<?php
foreach ($logged as $lgdIn) {
?>
<tr>
<th>ID</th>
<th>bpID</th>
<th>pc</th>
<th>up</th>
<th>tmLdr</th>
<th>fName</th>
<th>mName</th>
<th>lName</th>
<th>contactNo</th>
<th>Status-Id</th>
<th>bpclass_id</th>
</tr>
<tr>
<td><?php echo $lgdIn->id; ?></td>
<td><?php echo $lgdIn->bpID; ?></td>
<td><?php echo $lgdIn->pc; ?></td>
<td><?php echo $lgdIn->up; ?></td>
<td><?php echo $lgdIn->tmLdr; ?></td>
<td><?php echo $lgdIn->fName; ?></td>
<td><?php echo $lgdIn->mName; ?></td>
<td><?php echo $lgdIn->lName; ?></td>
<td><?php echo $lgdIn->contactNo; ?></td>
<td><?php echo $lgdIn->status_id; ?></td>
<td><?php echo $lgdIn->bpclass_id; ?></td>
</tr>
<?php
}
?>
</table>
</body>
You need to pass $data in load view just like below code
public function validate_LogIn()
{
$uname = $this->input->post('uname');
$pass = $this->input->post('pass');
$this->load->model("accsModel");
$data = $this->accsModel->logInCheck($uname, $pass);
$check = $data['verified'];
if ($check <= 0)
{
echo "Not a valid member";
}else{
$data['logged'] = $this->accsModel->getUser($uname, $pass);
$this->load->view('comms',$data);//want this to load
}
}
little change in view too
<body>
<p><?php print_r($logged) ?></p>
<h1>Commissions View</h1>
<table style="width:100%">
<tr>
<th>ID</th>
<th>bpID</th>
<th>pc</th>
<th>up</th>
<th>tmLdr</th>
<th>fName</th>
<th>mName</th>
<th>lName</th>
<th>contactNo</th>
<th>Status-Id</th>
<th>bpclass_id</th>
</tr>
<?php if(!empty($logged)) { foreach ($logged as $lgdIn) { ?>
<tr>
<td><?php echo $lgdIn->id; ?></td>
<td><?php echo $lgdIn->bpID; ?></td>
<td><?php echo $lgdIn->pc; ?></td>
<td><?php echo $lgdIn->up; ?></td>
<td><?php echo $lgdIn->tmLdr; ?></td>
<td><?php echo $lgdIn->fName; ?></td>
<td><?php echo $lgdIn->mName; ?></td>
<td><?php echo $lgdIn->lName; ?></td>
<td><?php echo $lgdIn->contactNo; ?></td>
<td><?php echo $lgdIn->status_id; ?></td>
<td><?php echo $lgdIn->bpclass_id; ?></td>
</tr>
<?php } } else { ?>
<tr colspan="11"><td>No record found.</td></tr>
<?php } ?>
</table>
</body>
Related
In database ,The 'status' column includes values 1(Active users) and 0 (Deactive users), But in my view I would like to show this data as string "Active" and "Deactive". below is my code , the problem is it returns all users status "Deactive" in view. i would appreciate any help and guidance
Here is controller
function index()
{
$data['staff'] = $this->staff_model->getStaffDetails();
$data['status'] = $this->staff_model->getStatus();
// print_r($data);
// die;
$this->load->view('admin/staff/index',$data);
}
Here is Model
function getStatus()
{
$query = $this->db->get_where('user_login',array('status' ))->result();
return $query;
}
And here is the view
<tbody>
<?php
foreach($staff as $n)
{
?>
<tr>
<td><?php echo $n->first_name;?></td>
<td><?php echo $n->email;?></td>
<td><?php echo $n->mobile_no;?></td>
<td><?php echo $n->role;?></td>
<td><?php echo $n->company_name;?></td>
<!-- <td><span class="icoact"></span> <?php echo $n->status;?></td> -->
<td><span class="icoact"></span>
<?php
// foreach($status as $n)
{
if('status' == 1)
{
echo "Active";
}elseif('status' == 0)
{
echo "Deactive";
}
};?>
</td>
<td><a class="edit" href="<?php echo site_url('staff_edit/'.$n->id);?>"><i class="fa fa-pencil-square-o"></i></a> 
<a class="delete" href="<?php echo site_url ('staff_delete/'.$n->id);?>" onclick="return confirm('Are you sure want to Delete this Record?')"><i class="fa fa-trash-o"></i></a> </td>
</tr>
<?php
}
?>
</tbody>
use this in your view:-
<?php
{
if($n->status==1){
echo $status = "Active users";
} else if($n->status==0){
echo $status = "Deactive users";
}
}?>
Match and team has many to many relationship but it does not work if i put the associations. and how to get home and away team names from the team table instead of home_id and team_id. please help
MatchesController.php
class MatchesController extends AppController
{
public $uses = array('Match','Team');
public function index()
{
$matches=$this->Match->find("all");
$this->set("matches",$matches);
}
public function add()
{
if ($this->request->is('post'))
{
$this->Match->create();
if ($this->Match->save($this->request->data))
{
$this->Flash->success(__('Match Has Been Added'));
return $this->redirect(array('action' => 'index'));
}
$this->Flash->error(__('Something Went Wrong.'));
}
$teams = $this->Team->find('list');
$this->set('teams',$teams);
}
index.ctp
<?php foreach($matches as $match) { ?>
<tr>
<td><?php echo $match['Match']['id']; ?></td>
<td><?php echo $match['Match']['home_id']; ?></td>
<td><?php echo $match['Match']['away_id']; ?></td>
<td><?php echo $match['Match']['date']; ?></td>
<!-- <td><?php// echo $match['Match']['time']; ?></td> -->
<td><?php echo $match['Match']['location']; ?></td>
<!-- <td><?php //echo $match['Match']['winner_id']; ?></td> -->
<!-- <td><?php //echo $match['Match']['winner_id']; ?></td> -->
<!-- <td><?php //echo $match['Match']['home_goals']; ?></td> -->
<!-- <td><?php //echo $match['Match']['away_goals']; ?></td>-->
<!-- <td><?php //echo $match['Match']['type']; ?></td> -->
<td>
<?php
echo $this->Html->link('View', array('action'=>'view',$match['Match']['id']), array('class'=>'btn btn-primary'));
echo $this->Html->link('Edit', array('action'=>'edit',$match['Match']['id']), array('class'=>'btn btn-success'));
echo $this->Form->postLink('Delete', array('action'=>'delete',$match['Match']['id']),array('confirm'=>'Are You sure you want to delete Student', 'class'=>'btn btn-danger'));?>
</td>
</tr>
<?php } ?>
Match model
class Match extends AppModel {
}
Team Model
class Team extends AppModel {
}
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;
}
I am trying to automate the hallticket process for an institution. This includes a certain verification process.
Iframe is working only for the first row, what should I do to open it for multiple rows based on the row selection
php code
<?php
while($row = mysqli_fetch_assoc($rows)) //Retriving the rows from the database
{
?>
<td><?php echo $app_id ?></td>
<td><?php echo $first_name ?></td>
<td><?php echo $last_name ?></td>
<td><?php echo $email_id ?></td>
<td><?php echo $mobile_number ?></td>
<td><?php echo $dd_number ?></td>
<td><?php echo $course_name ?></td>
<?php
if($dd_submit==1 && $ht_sent==0)
{?>
<td class="verified"><span>Verified</span></td>
<?php }elseif($dd_submit==0 && $ht_sent==0){ ?>
<td class="non_verified"><span>None Verified</span></td>
<?php }else{ ?> <td class="Pending"><span>Hall Ticket Sent</span></td>
<?php } ?>
<!-- I think the problem is here the java script is not repeating -->
<td><button id="dialog" name="verify" value="<?php echo $row['app_id'] ?>" > <img src="images/success_tick.png"></button><img src="images/meassage_table.png"><img src="images/comment.png"></td>
</tr>
<?php
}
?>
<div id="dialogContent" title="DD Verification for Vinay Draksharam">
<iframe src='ddverification_page.php?app_id=<?php echo $app_id ?> '></iframe>
Java Script
<script>
$(function () {
$("#dialogContent").dialog({
autoOpen: false,
modal: true
});
$('#dialog').click(function () {
$("#dialogContent").dialog( "open" );
});
});
</script>
First problem- you have many rows but you only create one dialog. You probably have to put dialog creation inside loop to have separate dialog for each row. You also need some way to create connection between dialog and row. Best idea would probably to use $app_id as id of dialog.
What i would do:
PHP:
<?php
while($row = mysqli_fetch_assoc($rows)) //Retriving the rows from the database
{
?>
<td><?php echo $app_id ?></td>
<td><?php echo $first_name ?></td>
<td><?php echo $last_name ?></td>
<td><?php echo $email_id ?></td>
<td><?php echo $mobile_number ?></td>
<td><?php echo $dd_number ?></td>
<td><?php echo $course_name ?></td>
<?php
if($dd_submit==1 && $ht_sent==0)
{?>
<td class="verified"><span>Verified</span></td>
<?php }elseif($dd_submit==0 && $ht_sent==0){ ?>
<td class="non_verified"><span>None Verified</span></td>
<?php }else{ ?> <td class="Pending"><span>Hall Ticket Sent</span></td>
<?php } ?>
<!-- I think the problem is here the java script is not repeating -->
<td><button class="dialog" name="verify" value="<?php echo $row['app_id'] ?>" >
<div class="dialogContent" id="dialogContent_<?php echo $app_id ?>" title="DD Verification for Vinay Draksharam">
<iframe src='ddverification_page.php?app_id=<?php echo $app_id ?> '></iframe> </div>
<img src="images/success_tick.png"></button><img src="images/meassage_table.png"><img src="images/comment.png"></td>
</tr>
<?php
}
?>
And Javascript:
$(function () {
$(".dialogContent").dialog({
autoOpen: false,
modal: true
});
$('.dialog').click(function () {
$("#dialogContent_"+$(this).val()).dialog( "open" );
});
});
I have a table which is fed dynamically from the database, this works as it should, I've then put the datatables functionality on the top which is working well, the sorting, search, the pagination is again working as it should.
I'm now looking at being able to edit a row and update the data back into the db and refresh the table.
My table structure
<table class="table table-striped table-hover table-bordered" id="tobebookedtable">
<thead>
<tr>
<th style="display:none;">PropID</th>
<th>ProjectNo</th>
<th>Householder</th>
<th>HouseNoName</th>
<th>Street Name</th>
<th>Postcode</th>
<th>Telephone</th>
<th>EPCPlanned</th>
<th>TAM</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$planning = db::getInstance()->query('CALL sp_tobebooked()');
foreach ($planning->results() as $planning) {
?>
<td style="display:none;"><?php echo $planning->PropID; ?></td>
<td><?php echo $planning->ProjectNo; ?></td>
<td><?php echo $planning->Householder; ?></td>
<td><?php echo $planning->HouseNoName; ?></td>
<td><?php echo $planning->StreetName; ?></td>
<td><?php echo $planning->Postcode; ?></td>
<td><?php echo $planning->Telephone; ?></td>
<td><?php echo $planning->EPCPlanned; ?></td>
<td><?php echo $planning->TAM; ?></td>
</tr>
<?php }; ?>
</tbody>
</table>
The EPCPlanned is the only field I want to update.
This here is my code for the jQuery side, the edit ability works, I can click into a cell and edit the record.
<script type="text/javascript">
$(document).ready(function() {
$('#tobebookedtable').dataTable( {
//"bProcessing": true,
//"bServerSide": true,
//"bJQueryUI": true,
"bPaginate": true,
"bStateSave": true
} );
/* Init DataTables */
var oTable = $('#tobebookedtable').dataTable();
/* Apply the jEditable handlers to the table */
oTable.$('td').editable( 'tobebooked_edit.php', {
"callback": function( sValue, y ) {
var aPos = oTable.fnGetPosition( this );
oTable.fnUpdate( sValue, aPos[0], aPos[1] );
//window.location.reload();
},
"submitdata": function ( value, settings ) {
console.log(value);
console.log(settings);
return {
"row_id": this.parentNode.getAttribute('id'),
"column": oTable.fnGetPosition( this )[2]
};
},
"height": "26px",
"width": "100%"
} );
} );
</script>
this is the tobebooked_edit.php
<?php
require 'core/init.php';
$table="temp_tobebooked";
$rawdata = $_POST;
$query = "show columns from $table";
$result= mysql_query($query) or die(mysql_error());
$fields = array();
while ( $row = mysql_fetch_assoc($result) )
{
$fieldname = $row['Field'];
array_push($fields, $fieldname);
}
$id = $rawdata['row_id'];
$value = $rawdata['value'];
$column = $rawdata['column'];
$column = $column + 1;
$fieldname = $fields[$column];
$value = utf8_decode($value);
$query = "update $table set $fieldname = '$value' where PropID = '$id'";
$result = mysql_query($query);
if (!$result) { echo "Update failed"; }
else { echo "UPD: $value"; }
?>
When it all runs and I update a record the field Updates on the screen and shows with the "UPD:" and the entered value.
But when I check my database there is no record updated. No errors show either
How can I get the data to update in my db?
First of all, I think that this
<tr>
<?php
$planning = db::getInstance()->query('CALL sp_tobebooked()');
foreach ($planning->results() as $planning) {
?>
should be
<?php
$planning = db::getInstance()->query('CALL sp_tobebooked()');
foreach ($planning->results() as $planning) {
?>
<tr>
Then, with
"row_id": this.parentNode.getAttribute('id'),
for each td element, you select the tr element and search for the id attribute, that doesn't exists
Firstly as per rsella's comment you nee to move the row inside the loop.
Secondly as suspected you are holding the ID in a hidden cell which is infact a sibling of the editable elements rather than the parent. Try changing the table structure to the following
<tbody>
<?php
$planning = db::getInstance()->query('CALL sp_tobebooked()');
foreach ($planning->results() as $planning) {
?>
<tr id="<?php echo $planning->PropID; ?>" >
<td><?php echo $planning->ProjectNo; ?></td>
<td><?php echo $planning->Householder; ?></td>
<td><?php echo $planning->HouseNoName; ?></td>
<td><?php echo $planning->StreetName; ?></td>
<td><?php echo $planning->Postcode; ?></td>
<td><?php echo $planning->Telephone; ?></td>
<td><?php echo $planning->EPCPlanned; ?></td>
<td><?php echo $planning->TAM; ?></td>
</tr>
<?php };
?>
</tbody>
This should mean that the parent has an ID and thus this.parentNode.getAttribute('id') should be able returning the required value.
As you're only editing the EPCPlanned cell then an alternative would be
<tbody>
<?php
$planning = db::getInstance()->query('CALL sp_tobebooked()');
foreach ($planning->results() as $planning) {
?>
<tr>
<td><?php echo $planning->ProjectNo; ?></td>
<td><?php echo $planning->Householder; ?></td>
<td><?php echo $planning->HouseNoName; ?></td>
<td><?php echo $planning->StreetName; ?></td>
<td><?php echo $planning->Postcode; ?></td>
<td><?php echo $planning->Telephone; ?></td>
<td id="<?php echo $planning->PropID; ?>"><?php echo $planning->EPCPlanned; ?></td>
<td><?php echo $planning->TAM; ?></td>
</tr>
<?php };
?>
</tbody>
and in your JQuery change "row_id": this.parentNode.getAttribute('id') to this.getAttribute('id')