I have a database table worksheet(qID varchar(5), answer varchar(50), wsheetid varchar(5)) with qID as the primary key. One worksheet has many questions.
I want to fetch all the questions where wsheetid =1 and display them as a form so that user can enter the answers.
And i want to check the user entered ans with the answer column in the database.
How can i do that in Yii.
Tried google and the guide to yii, couldnt find any solution. Any suggestion would be helpful.
Update:
I have the below view where i am am needed to get the attributes into a array and display the form. Is there a better way to do this? and for activeTextArea since model has the data, the text area contains data from database but i need a blank text area.
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'wdetails-form',
'enableAjaxValidation'=>false,
)); ?>
<?php echo CHtml::beginForm(); ?>
<?php foreach($questions as $i=>$questions): ?>
<?php $array = $questions->getAttributes();
echo CHtml::activeLabel($questions,"[$i]question",array('label'=>"$array[question]"));
echo CHtml::activeTextArea($questions,"[$i]answer",array('id'=>"$array[question_ID]"));
endforeach;
?>
</br>
<?php echo CHtml::submitButton('Submit'); ?>
<?php echo CHtml::endForm(); ?>
<?php $this->endWidget(); ?>
</div>
and in the controller, i need to insert the data that i got from the above form and insert into a different table(worksheetResults). DO i need to get the data into an array and use Yii DAO or is there any better way to do this.
table worksheetResults (username, worksheetID,question_ID,submitted_ans)
I think you should have a Worksheet model, a Worksheet controller and a Worksheet view.
Here is a skeleton for the controller part:
class WorksheetController extends CController
{
public function actionQuestions()
{
$criteria = new CDbCriteria;
$criteria->addCondition('(wsheetid = :id)');
$criteria->params[':id'] = '1';
$questions = Worksheet::model()->findAll($criteria);
$this->render('questions_form', array('questions'=>$questions));
}
public function actionAnswers()
{
//check the contents of $_POST['Worksheet']
}
}
Did you use Gii to create the scaffolding? That would get you the model and CRUD files and you can then customize the look and functionality. If your data model is good, this will save you a lot of time, even if you end up redoing the form view, you can still use Yii to create and maintain the model and controller classes.
See: http://www.yiiframework.com/doc/guide/1.1/en/topics.gii
Related
EDIT : I solved it. I'm leaving it here as a reference, I hope it helps someone in the future.
I'm trying to echo out a comment's owner like below, but it gives an error.
Trying to get property of non-object
$comments = ThingComment::where('thing_id', $id)>orderBy('comment_date', 'desc')->get();
blade template
#foreach($comments as $comment)
#foreach($comment->thingCommentOwner as $owner)
{{$owner->user_name}}
#endforeach
#endforeach
I have three models : User, Thing and ThingComment Thing probably isn't important, so I didn't include it.
My tables are : users and thing_comment
user_id and thing_id are foreign keys in the thing_comment table. I should be able to access the users table columns through the thing_comment table, that's what I'm trying to achieve here.
Models are below.
ThingComment.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ThingComment extends Model {
protected $table = 'thing_comment';
protected $primaryKey = 'comment_id';
protected $fillable = ['comment', 'comment_date', 'upvotes', 'downvotes', 'user_id', 'thing_id', 'parent_comment_id'];
public function thingCommentOwner() {
return $this->belongsTo('\App\Models\User','user_id');
}
public function commentThing() {
return $this->belongsTo('\App\Models\Thing','thing_id');
}
}
They need to be used like below. thingCommentOwner allows us to JUMP to the users table, which is cool.
#foreach($comments as $comment)
{{$comment->comment}}
{{$comment->thingCommentOwner->name}}
{{$comment->thingCommentOwner->user_image}}
<p></p>
#endforeach
I'm having a fundamental problem in understanding the concept of MVC and displaying more than one form at a time. I've tried a variety of methods but I'm still stuck - and that's because I don't think I'm understanding CI and MVC correctly.
I tried using 2 different views for the two different forms. Didn't work. I tried using one function per form in my controller. That didn't work either. I don't know what to do.
Should I be doing this;
Create a controller and have an index() function in it.
Build up my form elements for each form within this index()
Create 1 view that displays both forms and call it from within index()
Use form_open to direct the submit action to another function - call it validate()
Validate everything that comes in, send back errors
Somehow, and this is the main bit I don't get, complete an action if the form has been filled in correctly.
6 Is my biggest problem. Don't know how to do that. For example, on successful completion of the form I want my user to have created a directory at a chosen location - so I'm using mkdir() - so do I need an if statement within the validate() function or what??
UPDATE
Here is the code I have created so far;
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// Forms CodeIgniter controller
class Admin extends CI_Controller {
// Controller constructor
public function __construct()
{
parent::__construct();
// Load form helper required to validate forms
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
}
//*************************************************//
// Prepare data for the view to output the forms
public function index()
{
//*****************************************************//
//returns a drop down list of radio buttons, one for each directory
$map_one = $this->recursive_model->iterate_add_folder_names();
$data['folder_list_add'] = $map_one;
//****************************************************//
//*****************************************************//
//also returns a drop down list of radio buttons (slightly different), one for each directory
$map_two = $this->recursive_model->iterate_folder_names();
$data['folder_list_select'] = $map_two;
//****************************************************//
//load the views and the forms
$this->load->view('templates/header.php');
$this->load->view('admin/add_new_folder.php', $data);
$this->load->view('admin/add_new_file.php', $data);
$this->load->view('templates/small_footer.php');
}
//*************************************************//
//function if adding a new directory to the current structure
public function add_folder()
{
//need to select a directory for it to go under
$this->form_validation->set_rules('new_folder', 'New Folder', 'required');
//and name the new directory
$this->form_validation->set_rules('new_folder_name', 'New Folder Name', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->index();
}
else
{
if($this->input->post())
{
$new_folder = $this->input->post('new_folder');
$new_folder_name = $this->input->post('new_folder_name');
$folder_path = "/var/www/html/mike/content".$new_folder."/".$new_folder_name;
mkdir($folder_path, 0777);
$this->index();
}
}
}
//*************************************************//
public function add_file()
{
//folder location and name of file
$folder_name = $this->input->post('folder_name');
$new_folder_name = $this->input->post('file_name');
//validation rules
$this->form_validation->set_rules('folder_name', 'Folder Name', 'required');
$this->form_validation->set_rules('file_name', 'File Name', 'required');
//if there is an error with validation
if ($this->form_validation->run() === FALSE)
{
//gets stuck here every time when trying to upload a new folder :(
$this->index();
}
//if there is not an error with validation
else
{
//$folder_name will be something like "http://www.example.com/publications/people/reports"
$config['upload_path'] = $folder_name;
$config['allowed_types'] = 'gif|jpg|png|html|pdf|xls';
$this->load->library('upload', $config);
//if file cannot be loaded (due to $config perhaps?)
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->index();
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->index();
}
}
}
//*************************************************//
}
Here is one view (add_new_file.php);
<div id="container">
<h1>Upload A File/Publication</h1>
<div id="body">
<?php //echo $error;?>
<?php echo form_open_multipart('admin/add_file');?>
<?php echo $folder_list_select; ?>
<input type="file" name="file_name" size="20" />
<input type="submit" value="upload" />
</form>
</div>
Here is the other (add_new_folder.php)
div id="container">
<h1>Add A New Folder</h1>
<div id="body">
<?php echo validation_errors(); ?>
<?php echo form_open('admin/add_folder');?>
<?php echo $folder_list_add; ?>
New Folder Name: <input type="text" name="new_folder_name">
<input type="submit" value="upload" />
</form>
</div>
I hope this helps answer this thread.
Basically, I can get the first section to work - adding a folder - but I cannot get the adding a file to work. This is because if ($this->form_validation->run() === FALSE) is always returning false. I think it might be looking at the form elements in the other form - which it shouldn't do. What am I missing?
Should I be doing this;
1 . Create a controller and have an index() function in it.
[let's, for the sake of conversation, call this controller Users thx -ed]
Sure. That's cool. You could also have a function in that Controller called edit, or banana or whatever; either way works. With using just the index method (function), the url might look like http://example.com/index.php/users whereas if you add another method to the controller like banana, the url might look like http://example.com/index.php/users/banana.
2 . Build up my form elements for each form within this index()
Well, typically form elements are not created in the controllers. This is where the V in MVC comes in -- stuff you view goes into a view.
So, one might do something like
// Users Controller
class Users extends CI_Controller{
function index(){
//index method
}
function banana(){
$this->load->view('banana_view');
}
}
then in application/views/banana_view.php, you create your form. When you visit http://example.com/users/banana, you will see the form you created in banana_view.php.
3 . Create 1 view that displays both forms and call it from within index()
Sure, that'd work just fine. But remember that each <form></form> needs its own <input type="submit" name="Lets GO"> inside and thusly needs somewhere to send each forms data. This is the action="". You can leave it out, but beware that it will then send the form to whatever page you are currently on (in our case here, http://example.com/index.php/users/banana), so you have to have something in the banana() method to handle the form data. But, typically, it will be set via form_open(). Something like form_open('index.php/users/eat_banana'); will generate <form action="index.php/users/eat_banana"...
4 . Use form_open to direct the submit action to another function - call it validate()
Just don't call it late_for_dinner. But seriously, validate is a bit broad -- validate what? Validate why? As to validation, https://www.codeigniter.com/user_guide/libraries/form_validation.html. But you should cross that bridge after you grok the fundamentals of CodeIgniter (won't take long).
5 . Validate everything that comes in, send back errors
See last question.
6 . Somehow, and this is the main bit I don't get, complete an action if the form has been filled in correctly.
Many times people will display a success message
class Users extends CI_Controller{
function index(){
//index method
}
function banana(){
$this->load->view('banana_view');
}
// assuming form_open('index.php/users/eat_banana'); in banana_view
function eat_banana(){
//make sure that this is a POST
if($this->input->post()){
// do things with the data
// typically it gets saved to a database
// via a model (the M in MVC)
// http://ellislab.com/codeigniter/user-guide/general/models.html
if($saved_to_db){
// set message to send to the view
$data['message'] = "Everything went OK";
}else{
$data['message'] = "but who was database? data didn't save :(";
}
// load the view and send the data
$this->load->view('eat_banana', $data);
}
}
application/views/eat_banana.php:
<!DOCTYPE html>
<html>
<head></head>
<body>
<div>
<b>Form submitted.</b><br />
The message is: <?php echo $message; ?>
</div>
</html>
other times, one might instead prefer to redirect
class Users extends CI_Controller{
function index(){
//index method
}
function banana(){
$this->load->view('banana_view');
}
// assuming form_open('index.php/users/eat_banana'); in banana_view
function eat_banana(){
//make sure that this is a POST
if($this->input->post()){
// do things with the data
if($saved_to_db){
// just send them to the homepage
redirect('/');
}else{
// send them back to the form
redirect('index.php/users/banana');
}
}
}
So,
M is for model. Models are used to talk to the database.
V is for Vend view. Views render the text, forms, pictures, gifs, whatever to the screen. That's the idea anyway. There's nothing stopping you from echo'ing out an enormous unminimized javascript application from your controller. That would totally not be MVC tho.
C is for controller. Controllers call and send data to the views, receive data sent from views, take that data and send it to a model to be saved in the database (although CodeIgniter doesn't enforce this in any way either; you could if you wanted to save the data to a database directly from the controller, but this obviously defeats the MVC principal as well), retrieves data from the database and sends it to a view for display. Those are the basics anyway.
Hi I am testing and trying to insert a record in relationship table but without success. I am trying to enter new row in relationship table using logic hook when I do Save in Contacts module but instead of that I am getting blank index.php page.
Where is the problem?
thank you
<?php
include_once('modules/Contacts/ContactOpportunityRelationship.php');
class User_hook {
function insert()
{
$instance=new ContactOpportunityRelationship();
$instance->$contact_id='96163e07-f55a-eb87-251f-513482e4a1da';
$instance->$opportunity_id='c99178c5-fed5-7092-4a79-4e1da40a1eea';
$instance->save();
}
}
?>
The correct syntax should be
<?php
$contactId = '96163e07-f55a-eb87-251f-513482e4a1da';
$oppId = 'c99178c5-fed5-7092-4a79-4e1da40a1eea';
$oContact = BeanFactory::getBean('Contacts', $contactId);
$oContact->load_relationship('opportunities');
$oContact->opportunities->add($oppId);
I am looking for auto-complete to work by showing name in auto-complete text-field and storing the hidden field id value.
I am getting the names and id's when I inspect the networks..but able to show the names but its not picking the id for the record so unable to store the id
can anyone please give me any link/code which is working for auto-complete. is there any link/code which is working please..
class for auto-complete:::
class EAutoCompleteAction extends CAction{
public $model;
public $attribute;
public $id;
private $results = array();
public $returnVal = '';
public function run()
{
if(isset($this->model) && isset($this->attribute)) {
$criteria = new CDbCriteria();
$criteria->compare($this->attribute, $_GET['term'], true);
$model = new $this->model;
foreach($model->findAll($criteria) as $m)
{
$this->results[] = $m->{$this->attribute};
$this->results[] = $m->{$this->id};
//$this->results[] = array(
// 'name' => $m->{$this->attribute},
// 'id'=> $m->id
//);
}
}
echo CJSON::encode($this->results);
}
}
I am using controller/action like this::
public function actions()
{
return array(
'aclist'=>array(
'class'=>'application.extensions.EAutoCompleteAction',
'model'=>'Organisation', //My model's class name
'attribute'=>'name', //The attribute of the model i will search
)
}
and in my view form.php.
<div class="row">
<?php echo $form->labelEx($model,'organsiation'); ?>
<?php echo $form->hiddenField($model,'organisation_id',array()); ?>
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'attribute'=>'organisation_id',
'model'=>$model,
'sourceUrl'=>array('benefit/aclist'),
'value'=>'Please select',
'name'=>'name',
'id'=>'organisation_id',
'options'=>array(
'minLength'=>'2',
'select'=>"js:function(event, ui) {
alert(ui.item.id);
$('#organisation_id').val(ui.item.id);
}",
),
'htmlOptions'=>array(
'size'=>45,
'maxlength'=>45,
),
)); ?>
<?php echo $form->error($model,'organisation_id'); ?>
Here my code for autocomplete
Create an action that return json
public function listaItemMarcaAction(){
$cmd = Yii::app()->db->createCommand();
$cmd->select('id, nombre as value, nombre as label, pais_origen');
$cmd->from('item_marca');
$cmd->where('nombre like :term', array(':term'=>'%'.request()->getParam('term').'%'));
$data = $cmd->queryAll();
header('Content-type: application/json');
echo CJavaScript::jsonEncode($data);
Yii::app()->end();
}
Create autocomplete field and hidden field (in view files, example _form.php)
<?php echo $form->labelEx($model,'marca_id'); ?>
<?php echo $form->hiddenField($model,'marca_id'); ?>
<?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>"Item[marca]",
'value'=>$model->isNewRecord ? null : $model->marca->nombre,
'sourceUrl'=>Yii::app()->createUrl('/item/listaitemmarca'),
'options'=>array(
'minLength'=>1,
'change'=>'js:function(event,ui){fn_item_data(event,ui)}'
),
)); ?>
Create a javascript function to set values retreives by autocomplete. Nota that: only use change event, no need any more. In the example, the ActiveRecord is 'Item', then, the input's id wil be Item_marca_id and Item_marca.
function fn_item_data(event,ui){
if(!ui.item){
$("#Item_marca_id").val("");
$("#Item_marca").val("");
}else{
$("#Item_marca_id").val(ui.item.id);
$("#Item_marca").val(ui.item.value);
//and use ui.item.pais_origen for another things
if(ui.item.pais_origen == 'EEUU') alert('ok');
}
}
In your situation I would start with plain JQuery instead of CJuiAutoComplete on the client side. JQuery UI docs have a nice demo with working source code at http://jqueryui.com/autocomplete/#custom-data. There are three essential steps to get things working:
Supply data as array of JSON objects, not strings.
Use custom _renderItem function to render your JSON objects into readable strings.
Use custom select function to record visible name of selected JSON object into text field and record id of this object into hidden field.
To make step 1 you'll need to uncomment commented out part of EAutoCompleteAction, and remove two lines above it. After that you should be able to see item id and name in your alert() messages.
Step 2 (overriding _renderItem) is particularly tricky to do with CJuiAutoComplete, that is the reason I suggested to go with plain JQuery UI. See the above link for the example with plain JQuery UI. An example with CJuiAutoComplete can be found in the comments section of Yii docs: http://www.yiiframework.com/doc/api/1.1/CJuiAutoComplete#c8376. After step 2 you should be able to see readable autocomplete suggestions.
To make step 3 you'll need to add something like
$('#organisation_name').val( ui.item.name );
into your select function, provided that organisation_name is the id of your text field and organisation_id is the id of hidden form field (you'll need some changes to make it so).
Currently my code below works fine but it's a bit of overkill. In my controller I fetch the categories that have links and all the links in my database.
In my view I loop through all the categories and then when I want to add the links under the category I loop through all the links in my database instead I should only loop through the links that are assigned to the current category but I don't know how to do this with Zend Framework. Can anybody send me into right direction. Thank's for your time.
Controller:
public function indexAction()
{
$this->view->title = App_Translate::translate('links_title');
$this->view->headTitle($this->view->title, 'PREPEND');
$linkCat = Doctrine_Query::create()
->distinct()
->from('LinkCategory lc')
->innerJoin('lc.Link l WITH lc.id = l.link_category_id')
->orderBy('lc.id')
->execute();
$links = Doctrine_Query::create()
->from('Link')
->execute();
$this->view->linkCat = $linkCat;
$this->view->links = $links;
}
}
View:
<?php if(!empty($this->linkCat)): ?>
<ul>
<?php foreach($this->linkCat as $linkCat): ?>
<li><h2><?php echo $this->escape($linkCat['title']); ?></h2>
<?php if(!empty($this->links)): ?>
<ul>
<?php foreach($this->links as $link): ?>
<?php if($link['link_category_id'] == $linkCat['id']): ?>
<li><?php echo $this->escape($link['title']); ?></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No links added</p>
<?php endif; ?>
Your question is not really related to Zend Framework. You are fetching data using Doctrine, not ZF.
In your case, I think you should be able to loop over the links in the specific category using foreach($linkCat->Link as $link), seeing how you use innerJoin to load the relation.
Instead of doing those two queries, couldn't you go with only one ?
I see you have an Inner Join in your first query, so I suppose this could be possible ; I suppose you have some rule that says "One link is in one and only one category", which is a One-To-Many Relation ; maybe the Dealing with Relations part of the manual could help you.
What I would do might be :
remove the distinct from the first query, to get all links + for each one, its category
this is the list you want, isn't it ?
also, order by category and link, so it's easier to display (links being already ordered by category)
remove the second query
I suppose something like this would do :
$linkCat = Doctrine_Query::create()
->from('LinkCategory lc')
->innerJoin('lc.Link l WITH lc.id = l.link_category_id')
->orderBy('lc.name, l.name')
->execute();
(not tested, though : might need a bit more tunning)
Once you have that data, pass it to the views, where you'll do your loop -- remember lonks are already sorted by category :
display the name of the first category ; store it in a variable
loop on the links
if the category of the current link is not the same as the one of the previous link (store in the variable), then it means it's the end of a category, and the beginning of a new one
continue
when you reach the end of the links, it's also the end of the last category
This should work, I'd say (of course, you still have to code -- but the idea is here)
As a sidenote : you are using Doctrine classes, writting DQL, and all that in your Controller -- This is quite not the place : all this should go in your Model Classes, not in a Controller.
Have fun !