Codeigniter and Tank_auth sending validation to view not working - codeigniter

Hi I am new to Codeigniter but have hit a brick wall.
I am trying to see if a user already exists.
First I upload the data via a form to a controller which does its validation etc but breaks on only that issue. I managed to find where it breaks but cant fix it from there.
Prior to all this it querys the database finds there is in fact a match username and then reaches the snippet below
$errors = $this->tank_auth->get_error_message();
//find the correct error msg
foreach ($errors as $k => $v) $data['errors'][$k] =$this->lang->line($v);
//loop and find etc
$temp_mess = $data['errors'][$k];
//stores relevant stuff in the string
}
}
//echo $temp_mess; it outputs to the html so i can see it "says user exists"
$data['temp_mess'] = $tempmess; /// put this into a array
$this->load->view('layout', $data); ///send
}
}
Now for the view, it then calls the layout view etc but alas there is no output
$username1 = array(
'name' => 'username1',
'id' => 'username1',
'value' => set_value('username1'),
'maxlength' => $this->config->item('username_max_length', 'tank_auth'),
'size' => 30,
);
<?php echo form_open('register', $form_reg_id ); ?>
<fieldset>
<legend align="center">Sign up</legend>
<?php echo form_label('Username', $username1['id']); ?>
<?php echo form_input($username1); ?>
<?php $tempmess; ?>
<div class="error"><?php echo form_error($username1['name']); ?>
<?php echo isset($errors[$username1['name']])?$errors[$username1['name']]:''; ?>
<?php echo form_close(); ?>
</fieldset>
Thanks for any help on this
also could some one explain this line please. (for a really dumb person)
<?php echo isset($errors[$username1['name']])?$errors[$username1['name']]:''; ?>

Tank_auth automatically returns an error when the username exists. Juste have to check the errors array.

Related

Codeigniter 4 Passing Array to view

Fairly new to codeigniter and i just cant find the right way to load an array into a view.
for example lets say i have an array like
$data = [
'title' => 'my title,
'desc' => 'my desc,
];
i can pass that to my view like
return view('myview',$data);
then simply echo it out in my view like
<h1><?= $title ?></h1>
<p><?= $desc ?></p>
That works fine. but now lets say i have another array like :
$moredata =[
'more_data' => 'some more data',
'even_more_data' => 'even more data',
];
if i try to add that to my data array like
$data[] = $moredata
when i try to access 'more_data' or 'even_more_data' in my view like
<?= $more_data ?>
i get a undefined variable error for $moredata. So how do i access the variables within that new array? am i declaring them properly?
also if i wanted to loop through the secondary array how do i do that. trying
<?php foreach($moredata as $items){ ?>
<li><?php echo $items; ?></li>
<?php } ?>
also gives me an undefined variable error for $moredata
any help on how to do this correctly in codeigniter 4 appreciated.
Codeigniter uses the key of the array you're giving him to create variables name.
You should init it this way :
$moredata =[
'more_data' => 'some more data',
'even_more_data' => 'even more data',
];
// key of your array will be a variable name in your view
$data['my_var_name_in_view'] = $moredata
return view('myview',$data);
Then in your view you will be able to perform this :
<?php foreach($my_var_name_in_view as $items){ ?>
<li><?php echo $items; ?></li>
<?php } ?>

Set options for JHtmlTabs with Bootstrap for joomla

According to Using the JHtmlTabs class in a component, set options may be specified for the tabs being rendered
I have checked and find that is the solution which works for me!
<?php
$options = array(
'active' => 'tab1_id' // Not in docs, but DOES work
);
echo JHtml::_('bootstrap.startTabSet', 'ID-Tabs-Group', $options);
echo JHtml::_('bootstrap.addTab', 'ID-Tabs-Group', 'tab1_id', JText::_('COM_BOOTSTRAPTABS_TAB_1')); ?>
<p>Content of the first tab.</p>
<?php echo JHtml::_('bootstrap.endTab');
echo JHtml::_('bootstrap.addTab', 'ID-Tabs-Group', 'tab2_id', JText::_('COM_BOOTSTRAPTABS_TAB_2')); ?>
<p>Content of the second tab.</p>
<?php echo JHtml::_('bootstrap.endTab');
echo JHtml::_('bootstrap.endTabSet');?>

Yii2 update related model makes insert

Depending on this question: Yii2 updating two related models does not show data of the second. I have manged calling the related model InvoiceItems to the Invoices model it hasMany relation.
However, updating leads to insert new records in invoice_items table instead of updating the current related records to the invoices table.
I tried to add the id field of each InvoiceItems record in the _form view to solve this issue, but it still exist.
The following is actionUpdate of the InvoicesController:
public function actionUpdate($id)
{
$model = $this->findModel($id);
//$invoiceItems = new InvoiceItems();
$count = count(Yii::$app->request->post('InvoiceItems', []));
//Send at least one model to the form
$invoiceItems = [new InvoiceItems()];
//Create an array of the products submitted
for($i = 1; $i < $count; $i++) {
$invoiceItems[] = new InvoiceItems();
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
//$invoiceItems->invoice_id = $model->id;
if (Model::loadMultiple($invoiceItems, Yii::$app->request->post())){
foreach ($invoiceItems as $item){
$item->invoice_id = $model->id;
//$item->id = $model->invoiceItems->id;
$item->save(false);
}
return $this->redirect(['view', 'id' => $model->id]);
}
else{
return var_dump($invoiceItems);
}
} else {
//$invoiceItems->invoice_id = $model->id;
$invoiceItems = $this->findInvoiceItemsModel($model->id);
return $this->render('update', [
'model' => $model,
'invoiceItems' => $invoiceItems,
]);
}
}
This is the code of _form view of InvoicesController:
<div class="invoices-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'created')->textInput() ?>
<?= $form->field($model, 'type')->textInput(['maxlength' => true]) ?>
<hr />
<?php if (is_array($invoiceItems)): ?>
<?php foreach ($invoiceItems as $i => $item): ?>
<?= $form->field($item, "[$i]id")->textInput();?>
<?= $form->field($item, "[$i]item_id")->textInput();?>
<?= $form->field($item, "[$i]unit_id")->textInput();?>
<?= $form->field($item, "[$i]qty")->textInput();?>
<?php endforeach; ?>
<?php else: ?>
<?= $form->field($invoiceItems, "item_id")->textInput();?>
<?= $form->field($invoiceItems, "unit_id")->textInput();?>
<?= $form->field($invoiceItems, "qty")->textInput();?>
<?php endif; ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
The following screen shot, visually, demonstrates what I have got:
if you in update you don't need new record you should remove
this part
$count = count(Yii::$app->request->post('InvoiceItems', []));
//Send at least one model to the form
$invoiceItems = [new InvoiceItems()];
//Create an array of the products submitted
for($i = 1; $i < $count; $i++) {
$invoiceItems[] = new InvoiceItems();
}
The $invoiceItems you create in this way are obviuosly "new" and then are inserted ..for an update the model must to be not new..
You have already the related models to save they came from post.. and from load multiple
If you however need to manage new model (eg: because you added new record in wide form update operation) you can test
$yourModel->isNewRecord
and if this is new check then if the user have setted properly the related fields you can save it with
$yourModel->save();
otherwise you can simple discart.. (not saving it)
I am pretty sure that this code below of course fills a array named $invoiceItems with the submitted data.
if (Model::loadMultiple($invoiceItems, Yii::$app->request->post())){
foreach ($invoiceItems as $item){
$item->invoice_id = $model->id;
//$item->id = $model->invoiceItems->id;
$item->save(false);
}
}
But all $items have the scenario "insert" (could be that you allow the setting of the attribute 'ID' but normally this isn't allowed in the Gii generated code. and then you get "new items" whenever you save.
if you add a return var_dump($invoiceItems);after the loadMultiple you will see that only the safe attributes are filled with the submitted data.
You are also not validating them before saving, which is also kind of bad.
if (Model::loadMultiple($invoiceItems, Yii::$app->request->post()) && Model::validateMultiple($invoiceItems)) {
http://www.yiiframework.com/doc-2.0/guide-input-tabular-input.html
According to scaisEdge answer and this widget documentation. I could able to determine the problem solution.
Simply, in my code I neglected the relation between the two models, when I say:
$count = count(Yii::$app->request->post('InvoiceItems', []));
//Send at least one model to the form
$invoiceItems = [new InvoiceItems()];
The value of $invoiceItems should be obtained using the relation like the following:
$invoiceItems = $model->invoiceItems;
However, I still have another issue with adding new records to the related model InvoiceItems during the update.

cakephp 2.1 how to upload an image in a simple form and viewing it

Its so crazy how something so simple can be so difficult.
Please calling all cakephp 2.1 gurus. I have a form, it has a name and text image (var) field.
I would like to simply post an image, no resizing, no thumbnail, not anything special, just a simple mvc example of posting up an image and viewing it in the view.ctp.
Thats it. I have gone through the /en/2.1 book and api on cakephp.org but I just can't seem to get the mvc working as far as seeing it upload into a folder and viewing it in ctp.
My controller
public function add() {
if ($this->request->is('post')) {
$this->ListShExPainImage->create();
if ($this->ListShExPainImage->save($this->request->data)) {
$this->Session->setFlash(__('The list sh ex pain image has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The list sh ex pain image could not be saved. Please, try again.'));
}
}
}
public function view($id = null) {
$this->ListShExPainImage->id = $id;
if (!$this->ListShExPainImage->exists()) {
throw new NotFoundException(__('Invalid list sh ex pain image'));
}
$this->set('listShExPainImage', $this->ListShExPainImage->read(null, $id));
}
My add.ctp
<?php echo $this->Form->create('ListShExPainImage');?>
<fieldset>
<legend><?php echo __('Add List Sh Ex Pain Image'); ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('image', array('type' => 'file'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
My view.ctp
<h2><?php echo __('List Sh Ex Pain Image');?></h2>
<dl>
<dt><?php echo __('Id'); ?></dt>
<dd>
<?php echo h($listShExPainImage['ListShExPainImage']['id']); ?>
</dd>
<dt><?php echo __('Name'); ?></dt>
<dd>
<?php echo h($listShExPainImage['ListShExPainImage']['name']); ?>
</dd>
<dt><?php echo __('Image'); ?></dt>
<dd>
<?php echo h($listShExPainImage['ListShExPainImage']['image']); ?>
</dd>
</dl>
Your help would be greatly appreciated, thanking you in advance.
In your controller, you're not doing anything with the file upload. Even though Cake works magic sometimes, automagically guessing what you want to do with that upload is not one of them.
As stated in the book, the file upload field returns a data array with several keys. You should take the name key and save it as the image field (as that's what you seem to be calling). For example:
$filename = $this->request->data['ListShExPainImage']['image']['name'];
// Move the image
move_uploaded_file(
$this->request->data['ListShExPainImage']['image']['tmp_name'],
'/path/to/your/image/dir/' . basename($filename)'
);
// Set the filename in the database
$this->request->data['ListShExPainImage']['image'] = $filename;
Furthermore, you'll need to have a so called multipart/form-data enctype set to your form in order to be able to parse image uploads, by adding the file type to the Form create options.
And also, don't forget to move the file from your temp folder to a final location.
You also need to add a 'type' to your form:
<?php echo $this->Form->create('ListShExPainImage', array('type' => 'file'));?>
Then in the Controller do something like this:
if (is_uploaded_file($data['ListShExPainImage']['image']['tmp_name']))
{
move_uploaded_file(
$this->request->data['ListShExPainImage']['image']['tmp_name'],
'/path/to/your/image/dir/' . $this->request->data['ListShExPainImage']['image']['name']
);
// store the filename in the array to be saved to the db
$this->request->data['ListShExPainImage']['image'] = $this->request->data['ListShExPainImage']['image']['name'];
}
Then do your create and save routine.
In your View you would do someting like this to show the image:
<?php echo $this->Html->image('/path/to/your/image/dir/' . $listShExPainImage['ListShExPainImage']['image']); ?>

Issue with active record update in CodeIgniter

I have a form that submits data to a database in a CodeIgniter app (CI v. 2.0.2). We are allowing users to "edit" records by having them resubmit a record with new values and then doing an update. On submit, the form calls the vote controller's create method. Inside the create method, we check to see if there's already a record based on an entry code and the user's ID. If there is, we update; otherwise we create a new record. The creation works just fine; it's only the update I'm having an issue with. Here's the code.
view
<div id="vote_form">
<?php
$hidden = array('dot_judge_id' => $this->session->userdata('dot_judge_id'));
echo form_open('vote/create');
$entry_code_data = array(
'name' => 'entry_code',
'id' => 'entry_code',
'value' => set_value('entry_code')
);
echo form_hidden($hidden);
$score_options = array('1'=>'1 (lowest)', '2'=>'2','3'=>'3', '4'=>'4','5'=>'5 (highest)');
?>
<p><label for="entry_code">Entry code: </label><?php echo form_input($entry_code_data); ?></p>
<p><label for="q1">Question 1: </label><?php echo form_dropdown('q1', $score_options, ''); ?></p>
<p><label for="q2">Question 2: </label><?php echo form_dropdown('q2', $score_options, ''); ?></p>
<p><label for="q3">Question 3: </label><?php echo form_dropdown('q3', $score_options, ''); ?></p>
<p><label for="q4">Question 4: </label><?php echo form_dropdown('q4', $score_options, ''); ?></p>
<p><label for="q5">Question 5: </label><?php echo form_dropdown('q5', $score_options, ''); ?></p>
<p><?php echo form_submit('submit', 'Submit vote'); ?></p>
<?php echo form_close(); ?>
<?php echo validation_errors(); ?>
</div>
controller
function create() {
$id = $this->input->post('entry_code');
$judge_id = $this->input->post('dot_judge_id');
$data = array(
'entry_code' => $id,
'dot_judge_id' => $judge_id,
'q1' => $this->input->post('q1'),
'q2' => $this->input->post('q2'),
'q3' => $this->input->post('q3'),
'q4' => $this->input->post('q4'),
'q5' => $this->input->post('q5'),
);
//first check to see if there's already a record for this judge/entry
//if so, update. Otherwise, insert
$vote_id = $this->vote_model->getEntryById($id, $judge_id);
if($vote_id) {
log_message('debug', 'vote id exists: '.$vote_id);
$this->vote_model->updateRecord($data, $vote_id);
}
else {
log_message('debug', 'vote id does not exist; creating new');
$this->vote_model->createRecord($data);
}
/*
after submission, go to another page that gives choices - review entries, submit another entry, log out
*/
$data['msg'] = "Entry submitted";
$this->menu();
}
model
function getEntryByID($id, $judge_id) {
//determine if record already exists for entry/judge
$sql = 'SELECT vote_id from dot_vote WHERE entry_code = ? AND dot_judge_id = ?';
$query = $this->db->query($sql, array($id, $judge_id));
if($query->num_rows() == 1) {
$row = $query->row();
return $row->vote_id;
}
else {
return false;
}
}
function createRecord($data) {
$this->db->insert('dot_vote', $data);
return;
}
function updateRecord($data, $vote_id) {
log_message('debug', 'vote id is passed: '.$vote_id);
$this->db->where('vote_id', $vote_id);
$this->update('dot_vote', $data);
}
I know it's making it into the updateRecord method because the log_message output is in my log file displaying the correct vote_id (the auto-increment field of the returned record). But what I get in the browser is the following:
Can anyone point me in the right direction here? I have error display on in my config file, and have gotten SQL errors when they occur, so I don't think this is a SQL error. But I have no idea what could be happening in that tiny little function that's causing this. My next step is to skip the active record update and just use a standard query, but I'd like to know what's causing the problem here, even if I can get it to work the other way.
This line stood out:
$this->update('dot_vote', $data);
Do you mean this?
$this->db->update('dot_vote', $data);

Resources