I have this PHP code where I am loading a quiz into the view. When I select and submit an answer the view is changed and displays only the correct message or the wrong one.
What I am trying to achieve is when clicking the submit button for the answer, to keep the question and just display under if correct or not.
This is the code that I have:
function guess()
{
$guess = $this->input->get('name',false);
$personid = $this->input->get('id',false);
if ($guess === null) {
$newguess['data'] = $this->Starmodel->getQuestion();
$this->load->view('starview',$newguess);
}
else {
$res= $this->Starmodel->isCorrectAnswer($personid,$guess);
$person = $this->load->view('starview', array('iscorrect' => $res,
'name' => $guess));
}
}
My main view is starview.php. Can I use divs and target a specific load a view inside the specific div? How can I do this?
Thank you
Let say this your startview view...
//Your question goes here
//Answer part
<?php if($this->input->post()):
echo $answer_view_data
endif; ?>
In your function guess()
function guess()
{
$guess = $this->input->get('name',false);
$personid = $this->input->get('id',false);
if ($guess === null) {
$newguess['data'] = $this->Starmodel->getQuestion();
$this->load->view('starview',$newguess);
}
else {
$res= $this->Starmodel->isCorrectAnswer($personid,$guess);
$answer_data = 'Sample answer data';
$person = $this->load->view('starview', array('iscorrect' => $res,
'name' => $guess, 'answer_data' => $answer_data));
}
}
Related
I have created a system where users can search for payment solutions for their business here: http://104.236.45.72/
When you click an answer for a question, you'll notice that as you click through on chrome, you get a list at the top of previously answered questions.
When you view this same thing on your mobile device, you'll notice that the questions as the top only show ONE. It's like the session is not persisting on mobile. I have no idea why and I've tried changing the session driver from database to file and tried replacing all of the "$request->session->" references with "Session::", which leads to the same thing, it working on desktop and only showing one previous answer on mobile.
Here is the function being called when you answer a question on the fontend.
public function questions(Request $request, $id, $parentId, $question)
{
//Get the industry...
$data['industry'] = Industries::find($id);
//Get the parent level question and one child...
$data['parentQuestion'] = Questions::where('parentId', $parentId)->where('industryId', $id)->get();
if(count($data['parentQuestion']) > 0) {
foreach($data['parentQuestion'] as $pq) {
if($pq->parentId != 0) {
//How many steps are currently in the array?
if ($request->session()->has('lastSteps')) {
$hasLastSteps = TRUE;
}else {
$hasLastSteps = FALSE;
}
$stepCount = 1;
if($hasLastSteps == TRUE) {
$stepCount += count(Session('lastSteps'));
}
//Get the last steps ready...
$lsQuestion = Questions::find($question);
$lsAnswer = Questions::find($parentId);
$lsQ = array(
'unique' => uniqid(),
'questionId' => $question,
'question' => $lsQuestion->question,
'answer' => $lsAnswer->question,
'url' => '/'.$request->path(),
'stepCount' => $stepCount
);
$dup = 0;
//see if the session has a lastSteps array...
if ($hasLastSteps == TRUE) {
$temp = Session('lastSteps');
foreach($temp as $t) {
if($t['questionId'] == $question) {
$dup = 1;
}
}
}
//No duplicates exist, go ahead and add it to the session!
if($dup === 0) {
$request->session()->push('lastSteps', $lsQ);
}
}
//Grab its children...
if($pq->type === 'question') {
$data['children'] = Questions::where('industryId', $id)->where('parentId', $pq->id)->get();
}else {
//its an endpoint / answer!
}
}
}
//see if the session has a follow up array...
if ($request->session()->has('followUp')) {
$data['followUp'] = Session('followUp');
}else {
$data['followUp'] = array();
}
//see if the session has a lastSteps array...
if ($request->session()->has('lastSteps')) {
$data['lastSteps'] = Session('lastSteps');
}else {
$data['lastSteps'] = array();
}
return view('frontend.questions', $data);
}
im new in codeigniter and get some trouble with my function
here is my model
public function kode_unik(){
$q = $this->db->query("select MAX(RIGHT(id_obat,5)) as code_max from obat");
$code = "";
if($q->num_rows()>0){
foreach($q->result() as $cd){
$tmp = ((int)$cd->code_max)+1;
$hitung = strlen($tmp);
if ($hitung == 1 ){
$a = "0000".$tmp;
} elseif ($hitung == 2) {
$a = "000".$tmp;
}elseif ($hitung == 3) {
$a = "00".$tmp;
}elseif ($hitung == 4) {
$a = "0".$tmp;
}else{
$a = $tmp;
}
$code = sprintf("%s", $a);
}
}else{
$code = "0001";
}
$kodenyami = "E".$code;
return $kodenyami;
}
and then i wanna get the result of my models to show in my view.
here is my controller
public function add_data()
{
$this->load->helper( array('fungsidate', 'rupiah', 'url') );
$this->load->model('obat');
$this->load->database();
$data['a'] = $this->obat->tampil_data();
$data['b'] = $this->obat->kode_unik();
$componen = array(
"header" => $this->load->view("admin/header", array(), true),
"sidebar" => $this->load->view("admin/sidebar", array(), true),
"content" => $this->load->view("admin/add_obat", array("data" => $data), true)
);
$this->load->view('admin/index', $componen);
}
and my view goes here.
<i class="fa fa-medkit fa-5x"></i></div>
<div class="col-xs-9 text-right">
line20-> <div class="huge"><?php echo $b; ?></div>
<div>ID Obat</div>
the code give me an errors Message: Undefined variable: b
just don't know how to put the value of my models $kode_unik into my view ..
thanks
$data is already an array. Maybe like this :
"content" => $this->load->view("admin/add_obat", $data, TRUE)
CodeIgniter will extract the keys in $data so you will be able to use them in your view as $b
BTW instead of passing array() as a parameter of the load->view you can use NULL
P.S If you set the last parameter to TRUE, you will return the populated content of that view as a String (HTML in that case)
Here is something to start with :
public function __construct() {
parent::construct();
$this->load->helper(‘array(‘fungsidate’, ‘rupiah’, ‘url’);
$this->load->model(‘obat’);
$this->load->database(); // Should be already loaded depending on your config
}
public function add_data() {
// Set the data
$data[‘a’] = $this->obat->tampil_data();
$data[‘b’] = $this->obat->kodeunik();
// Load the views
$componen = array(
‘header’ => $this->load->view(‘admin/header’, NULL, TRUE);
‘sidebar’ => $this->load->view(‘admin/sidebar’, NULL, TRUE);
‘content’ => $this->load->view(‘admin/add_obat', $data, TRUE);
);
$this->load->view(‘admin/index’ ,$componen);
}
From what I understand about your code, in your templates you will need to echo the array keys
in admin/header you will echo $header;
in admin/sidebar you will echo $sidebar;
in admin/add_obat you will need to echo $content
and then you admin/index should be populated. I won’t do that if I were you to be honest I would probably load each template as is instead of outputting them as HTML string inside the final index. I don’t know exactly how you did your structure so it’s kinda hard for me to guess well. But in my case I would have use something like this instead of the componen array
public function add_data() {
// Set the data
$data[‘a’] = $this->obat->tampil_data();
$data[‘b’] = $this->obat->kodeunik();
// Load the views
$this->load->view(‘admin/header’);
$this->load->view(‘admin/sidebar’);
$this->load->view(‘admin/add_obat, $data); // This should be the body template of your page
$this->load->view(‘admin/footer’); // I guess you have a footer template
}
I hope this will help you!
I am struggling to workout a good method to update one column of my wcx_options table.
The new data is sent fine to the controller but my function isn't working at all.
I assumed i could loop through each column by option_id updating with the values from the array.
The database:
I update the option_value column with the new information via a jQuery AJAX Call to a controller which then calls a function from the backend class.
So far i have the following code:
if(isset($_POST['selector'])) {
if($_POST['selector'] == 'general') {
if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && isset($_POST['token'])
&& $_POST['token'] === $_SESSION['token']){
$site_name = $_POST['sitename'];
$site_url = $_POST['siteurl'];
$site_logo = $_POST['sitelogo'];
$site_tagline = $_POST['sitetagline'];
$site_description = $_POST['sitedescription'];
$site_admin = $_POST['siteadmin'];
$admin_email = $_POST['adminemail'];
$contact_info = $_POST['contactinfo'];
$site_disclaimer = $_POST['sitedisclaimer'];
$TimeZone = $_POST['TimeZone'];
$options = array($site_name, $site_url, $site_logo, $site_tagline, $site_description, $site_admin, $admin_email,$contact_info, $site_disclaimer, $TimeZone);
// Send the new data as an array to the update function
$backend->updateGeneralSettings($options);
}
else {
$_SESSION['status'] = '<div class="error">There was a Problem Updating the General Settings</div>';
}
}
}
This is what i have so far in terms of a function (It doesnt work):
public function updateGeneralSettings($options) {
$i = 1;
foreach($options as $option_value) {
$where = array('option_id' => $i);
$this->queryIt("UPDATE wcx_options SET option_value='$option_value' WHERE option_id='$where'");
$i++;
}
if($this->execute()) {
$_SESSION['success'] = 'Updated General Settings Successfully';
}
}
With the given DB-layout i'd suggest to organize your data as assiciative array using the db fieldnames, like:
$option = array(
'site_name' => $_POST['sitename'],
'site_url' => $_POST['siteurl'],
// etc.
'timeZone' => $_POST['TimeZone']
);
And than use the keys in your query:
public function updateGeneralSettings($options) {
foreach($options as $key => $value) {
$this->queryIt("UPDATE wcx_options SET option_value='$value' WHERE option_name='$key'");
if($this->execute()) {
$_SESSION['success'] = 'Updated General Settings Successfully';
}
}
}
(However, are you sure, you do not want to have all options together in one row?)
Change your query, you try to use an array as where condition. In the syntax you used that won't work. Just use the counter as where condition instead of define a $where variable. Try this:
public function updateGeneralSettings($options) {
$i = 1;
foreach($options as $option_value) {
$this->queryIt("UPDATE wcx_options SET option_value='$option_value' WHERE option_id='$i'");
$i++;
}
if($this->execute()) {
$_SESSION['success'] = 'Updated General Settings Successfully';
}
}
I've been doing this for almost a month but I cannot figure it out on how to get the "details" of every row in my table. My table in my view is foreach. And post it in another page. By clicking the link named "Details..." on first page.
for example: ive searched in first page and this is the result:
column1 column1 column3 column4 column5 column6
name desc age num add Details...
name desc age num add Details...
When i click the first details... it would go to second page and post name, desc, age, num, add into textbox. same as the second details...
Any help would be greatly appreciated. Thanks.
here are my codes:
my model model.php - this is my code for search data in my database table
public function search_equip() {
if ($this->input->post('category') == "All")
{
$match = $this->input->post('search');
$array = array('column1' => $match);
$this->db->like($array);
$query = $this->db->get('equip');
return $query->result();
}
elseif ($this->input->post('category') == "Appliance")
{
$match = $this->input->post('search');
$array = array('column1' => $match, 'category' => "Appliance");
$this->db->like($array);
$query = $this->db->get('equip');
return $query->result();
}
elseif ($this->input->post('category') == "Furniture")
{
$match = $this->input->post('search');
$array = array('column1' => $match, 'category' => "Furniture");
$this->db->like($array);
$query = $this->db->get('equip');
return $query->result();
}
elseif ($this->input->post('category') == "Equipment")
{
$match = $this->input->post('search');
$array = array('column1' => $match, 'category' => "Equipment");
$this->db->like($array);
$query = $this->db->get('equip');
return $query->result();
}
}
my controller search.php - my code with set rules
function search_equipment()
{
//If searchbox is empty
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('search', 'Search', 'required|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('auth/index', 'refresh');
}
else
{
$data['query'] = $this->model->search_equip();
$this->load->view('auth/search_view_equipment', $data);
}
}
my view search_view_equipment.php - this is where i search data and show results with the link every row
<?php foreach($query as $item):?>
echo "<tr>";
echo "<td>".<?= $item->Column1 ?>."</td>";
echo "<td>".<?= $item->Column2 ?>."</td>";
echo "<td>".<?= $item->Column3 ?>."</td>";
echo "<td>".<?= $item->Column4 ?>."</td>";
echo "<td>".<?= $item->Column5 ?>."</td>";
echo "<td>".anchor("auth/view_equipment_details/".$item->Column6, 'Details...', array('class' => 'detail'))."</td>";
echo "</tr>";
<?php endforeach;?>
my second view view_equipment.php - this is where to post the data coming from first page (specific row)
Note: This td's are input, i dunno how to make a code here using input :)
<?php foreach($equipid as $item):?>
echo "<tr>";
echo "<td>".<?= $item->Column1 ?>."</td>";
echo "<td>".<?= $item->Column2 ?>."</td>";
echo "<td>".<?= $item->Column3 ?>."</td>";
echo "<td>".<?= $item->Column4 ?>."</td>";
echo "<td>".<?= $item->Column5 ?>."</td>";
echo "<td>".anchor("auth/view_equipment_details/".$item->Column6, 'Details...', array('class' => 'detail'))."</td>";
echo "</tr>";
<?php endforeach;?>
Is it possible to get that whole data of that row and show it in another page? Specifically, I'm confused on what am I going to do, because the table is in foreach. I cannot get the value of the specific link in first page, here's my code on getting the value of links in first page but im not succesful:
controller: auth.php
function view_equipment_details()
{
$data['equipid'] = $this->model->get_equipment_id();
$this->load->view('auth/view_equipment', $data);
}
model: addition in model.php
public function get_equipment_id() {
$match = $this->input->post(); //This line is where i want to put the specific links ( any links that i click in first page) VALUE or NAME or whatsoever that is EXACTLY THE SAME AS THE ID OF THAT ROW I WILL CLICK to search using (next line): all i need is the value equal to the id of a row in table in database.
$array = array('id' => $match);
$this->db->where($array);
$equipid = $this->db->get('equip');
return $equipid->result();
}
Thank you in advance for any help. Hoping for a feed back. :)
What a mess here budy ...
Your question is really confuse, but I think you are trying to make a link:
<?php echo anchor("auth/view_equipment_details/".$item->id, 'Details...', array('class' => 'detail')))?>
NOTE: its better to pass an id in the url (or a slug maybe), but not a field, Because you have to do a search in the requested controller using that id.
and then in AuthController the function called view_equipment_details:
public function view_equipment_details($id){
$this->data['equipment'] = $this->db->where('id', $id)->get('table_name')->row();
$this->load->view(auth/view_equipment', $this->data)
}
You are trying to get something from post, and thereś nothing to do with post. The id should come as a parameter.
This way you can access your individual row details on another page.
For example you have page1 with table listing all the rows and a link as you shown for listing details in another page.
So attach ID to your details column within your url,so as per your code it goes like this
echo "<td>".anchor("auth/search_details/".$id_of_row, 'Details...', array('class' => 'detail'))."</td>";
Now in your controller function named 'search_details' will go like this :
function search_details ($row_id)
{
//code here to get/fetch row from database using $row_id
}
This way you can pass specific row details to another page.
Let me know if you any doubts further.
I figured it out. Thanks for your answers guys special.
My Model:
public function get_equipment_id($id) {
$array = array('id' => $id);
$this->db->where($array);
$equipid = $this->db->get('equip');
return $equipid->result();
}
My Controller:
function view_equipment_details($id)
{
$data['equipid'] = $this->model->get_equipment_id($id);
$this->load->view('auth/view_equipment', $data);
}
My View:
echo "<td>".anchor("auth/view_equipment_details/".$item->id, 'Details...', array('class' => 'detail'))."</td>";
I am new to laravel4, I am working basically with SOAP webservices in laravel 4 using Nusoap library.
My problem is with pagination, at controller my code is ast follow
function AjaxProductList()
{
$isAjax = Request::ajax();
if($isAjax)
{
//instantiate the NuSOAP class and define the web service URL:
$client = new nusoap_client('http://stage.ws.greenbook.net/gbwswcf/servicegb.svc?wsdl', 'WSDL');
//check if there were any instantiation errors, and if so stop execution with an error message:
$error = $client->getError();
if ($error) {
die("client construction error: {$error}\n");
}
if(Input::get('term'))
{
Session::put('term', Input::get('term'));
$term = Input::get('term');
}
else
$term = Session::get('term');
if(Input::get('pg_number')!='')
{
Session::put('page_num', Input::get('pg_number'));
$page_num = Input::get('pg_number');
}
else
$page_num = Session::get('page_num');
if(Input::get('per_pg_result') !='')
{
Session::put('result_per_page', Input::get('per_pg_result'));
$result_per_page = Input::get('per_pg_result');
}
else
$result_per_page = Session::get('result_per_page');
$param = 'SearchParam=|category#'.Session::get('type').'|searchterm#'.$term;
//$value = Session::get('key');
$params = array( 'ClientID' => Config::get('globals.ClientID'),
'strPwd' => Config::get('globals.password'),
'Params' => $param ,
'PageNum' =>$page_num,
'NumOfResultsPerPage' =>$result_per_page
);
$client->soap_defencoding = 'UTF-8';
$answer = $client->call('GetResultsV2',$params);
//check if there were any call errors, and if so stop execution with some error messages:
$error = $client->getError();
if ($error) {
echo 'some error occured , please try again later';
die();
}
$ResultNumbers = Common::find_key_array('ResultNumbers',$answer);
$data['SearchParams'] = Common::find_key_array('SearchParams',$answer);
$data['products'] = Common::find_key_array('Product',$answer);
$data['total_result'] = $ResultNumbers['TotalNum'];
$data['paginator'] = **Paginator::make($data['products'],$data['total_result'],$result_per_page)**;
$return["prd_listing_bot"] = View::make('frontend.search.ajax_product_listing',$data)->render();
echo json_encode($return);
exit;
}
else
{
echo 'not allowed';
}
}
here i am using Paginatior class and providing the parameters (returned records,total items,perpage items).
here is my view code:
$paginator->setBaseUrl('search');
echo $paginator->links();
NOW its creating links successfully
My URL structure after clicking 5 is
'http://mydomain/search?page=5'.
and in 'routes.php' i have
Route::any('search', array('uses' => 'SearchController#QuickSearch'));
when the page view is loaded an ajax call is initiated for function AjaxProductList();
when i click on any link in pagination, it fatches data successfully, but not updating the active link. i.e if i click on page number 5 it will fetch the correct data but active link will be still at page "1".
tell me please if i am doing anything wrong?
thanks in advance.
Just solved it by placing
Paginator::setCurrentPage($page_num);
above the line
Paginator::make($data['products'],$data['total_result'],$result_per_page);
Anyway thanks to everyone who participated here.
In the Paginator::make() methos the first parameter is an array of items that are already paginated. Check this example:
$perPage = 15;
$currentPage = Input::get('page', 1) - 1;
$pagedData = array_slice($items, $currentPage * $perPage, $perPage);
$matches = Paginator::make($pagedData, count($items), $perPage);
In this example I use the array_slice methos to get the items of the current page. To get the page in your controller you can use Input::get('page', 1) so if there is no page selected to default would be 1