How to get the data of a "multiselect option" coming from a cell - cakephp

I created a cell to assemble a "multiple selection option" and inserted it into a view for the user.
Fill in the fields like this:
<select class="form-control" multiple id="product">
<?php foreach ($json["data"] as $product):?>
<option value="<?php echo $product["id"]; ?>"><?php echo $product["name"]; ?></option>
<?php endforeach; ?>
</select>
I added this cell to a user registration form along with other fields!
<?= $this->cell('Products');?>
How can I get the information selected by the user and send it to the controller after submitting the form?
Can I solve this using the cell?

Related

Dropdown is empty when using foreach from DB

My program brings a list of a teacher from a table.
Then I want to show the names in the drop-down but for some reason, it opens an empty dropdown with the number of empty lines like the records in the table.
This is my code for this problem
<div>
<label for="teacher_name"> שם המורה: </label>
<select name="teacher_name" id="teacher_name">
<?php if (empty($teachers)): ?>
<p> No teachers found</p>
<?php else: ?>
<?php if($status == 'new'): ?>
<option value="please_select">נא לבחור</option>
<?php foreach ($teachers as $teacher): ?>
<option value=<?=$teacher['teacher_name']; ?> name="teacher_name" id="teacher_name"></option>
<?php endforeach; ?>
<?php endif; ?>
<?php if($status == 'edit'): ?>
<p> edit</p>
<input type="text" name="teacher_name" id="teacher_name" value="<?= htmlspecialchars($lesson->teacher_name); ?>" >
<?php endif; ?>
<?php endif; ?>
</select>
</div>
If you "view source" of your output I think you will see that the option tags do have data in them - but you haven't set the value to display, it should be more like <option ....>Show this in the list</option>.
Try this (the same code as you were using, but adding the teacher name variable again where it will display)
<option value=<?=$teacher['teacher_name']; ?> name="teacher_name" id="teacher_name"><?=$teacher['teacher_name']; ?></option>

Issue with Check box update or Edit to database in Codeigniter

My function is when I will check then 1 will insert and if not then by default the field will fill with 0.At the time of insert problem is not happen, it store '1' to db but at the time of update with check box it not able to send '1' again to db, it sending 0.
AT THE INSERT TIME
View
.
<?php echo form_checkbox("feature_opd","1", set_checkbox("feature_opd","1")); ?>
<?php echo form_checkbox("feature_gyane","1", set_checkbox("feature_gyane","1")); ?>
<?php echo form_checkbox("feature_iui","1", set_checkbox("feature_iui","1")); ?>
Model
public function add_article($array)
{
return $this->db->insert('table_name',$array);
}
AT THE UPDATE TIME
View
<div>
<?php
if($article->feature_opd == 1)
{?>
<input type="checkbox" name="feature_opd" checked >
<?php
}
else
{?>
<input type="checkbox" name="feature_opd" >
<?php
}
?>
</div>
<div>
<?php
if($article->feature_gyane == 1)
{?>
<input type="checkbox" name="feature_gyane" checked>
<?php
}
else
{?>
<input type="checkbox" name="feature_gyane" >
<?php
}
?>
</div>
<div>
<?php
if($article->feature_iui == 1)
{?>
<input type="checkbox" name="feature_iui" checked>
<?php
}
else
{?>
<input type="checkbox" name="feature_iui" >
<?php
}
?>
</div>
Model
public function update_article($article_id , array $article)
{
return $this->db
->where('id',$article_id)
->update('ivf_productsettings', $article );
}
My Query is how I will update db with check box. I am facing problem to update with check box in Codeigniter, Please suggest me , if my code going to tough then please suggest me in simple way how to update with check box in codeigniter .I am new in ci .
You can create your update checkboxes like so:
<?php echo form_checkbox("feature_iui","1", set_checkbox("feature_iui","1", $article->feature_iui)); ?>
The default value will apply from the db.
Aside from that there is nothing aside from user input preventing the checkboxes from being properly sent. Thus, any code that is causing a 0 being put in the db is probably due to how you are processing the checboxes in your controller update function. As I've said in the comments, checkboxes only submit 1's never 0's.

Retrieve WordPress Database Info and display in form

Sorry I am new to WordPress and still having difficulty to retrieve my data from my custom database. So far this is my code for submitting data to my wp_datareg table.
<?php
/*
Template Name: Data-Register
*/
get_header();
?>
<?php
If($_POST['Submit']) {
global $wpdb;
$Data1=$_POST['Data1'];
$Data2=$_POST['Data2'];
if($wpdb->insert(
'wp_datareg',
array(
'Data1' => $Data1,
'Data2' => $Data2
)
) == false) wp_die('Database Insertion Failed');
else echo '<h2>Database Insertion Succesful</h2><p />';
?>
<?php
}
else // else we didn't submit the form so display the form
{
?>
<
<h4>Data Registration Form</h4>
<form action="" method="post" id="addcourse">
<p><label>Put Data1:<input type="text" name="Data1" size="30" /></label></p>
<p><label>Put Data2: <input type="text" name="Data2" size="30" /></label></p>
</div>
<input type="submit" name="Submit" id="addcoursesubmit" value="Submit" />
</form>
What I want to add is put another text form and search button where user can search a data of Data1 and edit its value on the form, (please see the image)
Sorry for being such a noob.
PLEASE SEE THIS IMAGE
Thank you.
br
We can fetch data from wp_datareg table using this code, however putting back the data to the form is my problem.
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM wp_datareg" );
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->Data1;?></td>
</tr>

CakePHP 2.x HABTM saveAll with Add view associated model optgroups

I have a CakePHP Add Photo View that is using optgroups so that the HABTM related Tag model's data is shown in grouped fashion in the view. In the Photo controller I send the related Tag model data as follows to get the optgroups grouping of Tags by Tag Category:
//fields shown to create optgroups eg group tags by category
$this->set('tags', $this->Photo->Tag->find('list', array(
'fields' => array('Tag.id', 'Tag.tagname', 'Tag.category'),
'conditions' => array(
'Tag.account_id' => $this->Session->read('Auth.User.account_id')))));
}
The view shows the Tags nicely grouped by Category. So that part works perfectly.
However, after adding a new Photo and selecting one or more Tags to assign to it, it does save the Photo properly but not the assigned Tags.
I believe that CakePHP saveAll is expecting a data array as would be delivered without the optgroup Category > Tag hierarcy, and is therefore not saving Tag data when new Photo is added and Tags are assigned to the Photo.
What do I need to do to give it the proper array to save the related Tags? Will it be in the controller after Create() where I 'remove' the Category grouping?
A bit of background on this from CakePHP http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
"If you would like to generate a select with optgroups, just pass data in hierarchical format. This works on multiple checkboxes and radio buttons too, but instead of optgroups wraps elements in fieldsets:"
$options = array(
'Group 1' => array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
),
'Group 2' => array(
'Value 3' => 'Label 3'
)
);
echo $this->Form->select('field', $options);
Output:
<select name="data[User][field]" id="UserField">
<optgroup label="Group 1">
<option value="Value 1">Label 1</option>
<option value="Value 2">Label 2</option>
</optgroup>
<optgroup label="Group 2">
<option value="Value 3">Label 3</option>
</optgroup>
</select>
Ok so this problem had nothing to do with the array structure.
It turns out I had 'Photo.Tag' instead of just 'Tag' in the Add Photo View. Here is the old Add Photo View code that didn't work:
<div class="photos form">
<p><?php echo $this->element('admin_nav'); ?></p>
<?php echo $this->Form->create('Photo'); ?>
<fieldset>
<legend><?php echo __('Add Photo'); ?></legend>
<?php
echo $this->Form->input('account_id', array('type'=>'hidden'));
//echo $this->Form->input('filename', array('type'=>'hidden'));
echo $this->Form->input('filename', array('type' => 'file', 'label' => 'Upload photo'));
echo $this->Form->input('desc');
echo $this->Form->input('Photo.Tag' , array('label'=>'Tags', 'multiple'=>'checkbox'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
Here is the new Add Photo View code which now saves the new photo with the tags selected:
<div class="photos form">
<p><?php echo $this->element('admin_nav'); ?></p>
<?php echo $this->Form->create('Photo'); ?>
<fieldset>
<legend><?php echo __('Add Photo'); ?></legend>
<?php
echo $this->Form->input('account_id', array('type'=>'hidden'));
//echo $this->Form->input('filename', array('type'=>'hidden'));
echo $this->Form->input('filename', array('type' => 'file', 'label' => 'Upload photo'));
echo $this->Form->input('desc');
echo $this->Form->input('Tag' , array('label'=>'Tags', 'multiple'=>'checkbox'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
While the Cakephp documentation on Saving Data makes references to how the array should be formatted, this appears to refer to all of the other relationships excluding HABTM. HABTM relationships can be saved in one action with the save on the parent model, in this case the Photo model eg:
$this->Photo->save($this->request->data)
will save the new Photo being added, plus any of the selected Tags, and they will be saved in the join table photostags.
What is a bit unusual, is that $this->Photo->save($this->request->data) appears to work same as $this->Photo->saveAll($this->request->data).

Show selected option in HTML Select menu after page has been reloaded in CakePHP

I'm building a non-javascript version of a website, as there are a number of customers who don't have javascript enabled. On this site, the customer selects what country they will be visiting and then displays the data related to that country accordingly.
I've managed to get this to work. They use a HTML drop down menu to select the country, click on Submit, and the page reloads with the data related to the selected country. However, it does not change the country displayed in the HTML drop down menu, so when the page reloads it reverts back to "Select A Country".
What I would like to have happen is that if you clicked on United Kingdom from the drop down box for example, when the page reloads the drop down should display United Kingdom.
Here is the code I am currently using for the view file:
<form name="countryselect" action="/selected-country/" method="post">
<select id="country-list" name="countryselected">
<?php foreach($countries as $coun) { ?>
<option value="<?php echo $coun['Tariff']['countryslug']; ?>"><?php echo $coun['Tariff']['countryname']; ?></option>
<?php } ?>
<input type="submit" value="Submit" />
</select>
</form>
And in my controller file I am using this:
$countries = $this->Tariff->find('all', array('conditions' => array('Tariff.gsmid' => '1')));
$this->set('countries', $countries);
if (!isset($_POST['countryselected'])) {
} else {
$countryselect = $_POST['countryselected'];
$tarcounselect = $this->Tariff->find('first', array('conditions' => array('Tariff.countryslug' => $countryselect)));
$this->set('tarcounselect', $tarcounselect);
}
Cheers!
If you use Cake, you should not build the form and the select manually but use the Cake FormHelper instead. It will then keep the selected country automatically:
Controller:
$this->set('countries', $this->Tariff->find('list', array('conditions' => array('Tariff.gsmid' => '1'), 'fields' => array('countryslug', 'countryname'))));
View:
<?php
echo $this->Form->create();
echo $this->Form->select('Tariff.countryslug', $countries);
echo $this->Form->end(__('Submit'));
?>
And then to retrieve the selected country in the controller:
if($this->request->is('post'))
{
$countryslug = $this->request->data['Tariff']['countryslug'];
}

Resources