CakePHP trouble with posting from select to controller - cakephp

The value of my select tag doesn't seem tto post to my controller, no matter what I try
The select tag
<select name="whatever">
<?php
foreach($packs as $packName => $pack) {
echo " '<option value=" . $packName . '">' . $packName . '</option>';
}
?>
</select>
Where I try to use it in controller
function procedures() {
$errors = array();
$otsing= "";
if (!isset($this->data)) {
App::import('Helper', 'Formatter');
$formatter = new FormatterHelper();
$this->data['start'] =
$formatter->FormatDate($this->Dating->Now());
$this->data['end'] = $formatter->FormatDate($this->Dating->Now());
if(!empty($_POST['whatever']))
{
$otsing = $this->$_POST['whatever'];
}
}
}

The select name should be wriiten like
data[Formname][selectname]
if you want to give it in HTML format or you should use cakephp way to define dropdwon:
<?php
echo $form->select(‘whatever’,$packs)
?>

Related

Return all array values with ACF fields and foreach loop

I created a checkbox field in ACF and want to loop this field again and again and show all these values. First I tried it the way the ACF documentation shows, but the following code results in just the value of the first checked checkbox.
function ms_get_department(){
$departments = get_field('vakgebied');
if($departments):
foreach($departments as $department):
echo '<span class="department-text">' . $department['label'] . '</span>';
endforeach;
endif;
}
I also tried to store all the values inside an array but in the code below it just shows 'Array' and don't know how to show all these data in this case.
function ms_get_department(){
$departments = get_field('vakgebied');
$deps = array();
if($departments):
foreach($departments as $department):
$deps[] = $department['label'];
// $test = '<span class="department-text">' . $department['label'] . '</span>';
endforeach;
return $deps;
endif;
}
Does anyone know how I can solve this problem in a proper way?
It's not clear where you're adding this function. If it's on the single page then the code should work; however, if it's on any other page then you'd need to pass the post ID to ACF Field.
function ms_get_department(){
$departments = get_field('vakgebied', 123); // 123 being the post ID
$deps = array();
if($departments):
foreach($departments as $department):
$deps[] = $department['label'];
// $test = '<span class="department-text">' . $department['label'] . '</span>';
endforeach;
return $deps;
endif;
}
Another thing to check is, make sure ACF field is returning both 'label' and 'value'.

Codeigniter Model: How to use values from a query for further calculations?

i have a function in a model with the following query, and i want to make some calculations with value1, value2 and value3 outside the query. how to get the values out of this result()?
function ($id,$username$) {
$this->db->select("id,value1,value2,value3,value4");
$this->db->from("table1");
$this->db->where("username=$username and id = $id");
$query=$this->get();
$result = $query->result();
$result['$testvalue'] = (value1 * value2/113) + value3;
return $result; }
can someone help? i already found out how to use values from a table, but not from a query!
ok now i know how to use row_array.
i call the row_array in the controller
$data = $this->model_testmodel->testfunction($id, $username);
...
$this->load->view('pages/view_test', $data);
now i wanna know how the view would look like.
i tried many different ways.
in the moment i am stuck with ...
<?php foreach $data as $value: ?>
<label>Value1:</label>
<input type="text" name="value1" value="<?php echo $value['value1']; ?>">
<label>Value2:</label>
<input type="text" name="value2" value="<?php echo $value['value2']; ?>">
...
<?php endforeach; ?>
but i get two errors:
Message: Undefined variable: data
Message: Invalid argument supplied for foreach()
Try this...
function ($id,$username)
{
$this->db->select("id,value1,value2,value3,value4");
$this->db->from("table1");
$this->db->where(array('id'=>$id,'username'=>$username));
$query=$this->db->get();
$result = $query->row();
$testvalue = (($result->value1 * $result->value2/113) + $result->value3);
//test here
echo $testvalue;
$res = array();
$res['testvalue'] = $testvalue;
$res['value1'] = $result->value1;
$res['value2'] = $result->value2;
$res['value3'] = $result->value3;
return $res;
}
It returns $res as array.Easy to execute of you got problem comment.. if it works accept.Lets enjoy..
Then make a function call and accept array in a variable..like this..
first load model then make function call.from your controller
$this->load->model('model_name');
$data= $this->model_name->function_name($id,$username);
//Fetch returned array like this..
echo $data['testvalue'];
echo $data['value1'];
//so on

CodeIgniter Dropdown menu fetching data from database column

I am having a problem. Please let me know if there is a solution. I am new to codeigniter therefore, sorry in advance if there is a silly one!
I am trying to fetch data from a database. Table name is fw_main_cat which have fields (cat_id, cat_parent_id, cat_level, cat_title, cat_menu_order and cat_status). cat_id is unique.
I want a dropdown menu (in view) to take all the data of a column cat_title through cat_level (which are int). So, how can I do?
Here is my code which I have tried so far.
This is Model :
public function cat_level_one($cat_level)
{
$sql = "Select * from fw_main_cat Where cat_level=? ";
$result = $this->db->query($sql, $cat_level);
if($result->num_rows() > 0)
{
return $result->result_array();
}
else { return false;
}
}
This is Controller :
public function getcategory()
{
if ($this->session->userdata('session_status'))
{
$cat_level_one = $this->admin_cat_model->cat_level_one($cat_level);
$data['cat_level_one'] = $this->admin_cat_model->cat_level_one($cat_level);
$this->session->set_userdata('cat_level', $_POST['cat_level']);
$this->laod_view('admin_view/admin_cat/view_add_category', $data);
}else {
redirect ('admin_view/admin_cat/view_category');
}
}
This is View (dropdown menu):
<li class="full-row">
<select name = 'cat_level_one' id = 'cat_level_one'>
<option value="<?php if(isset ($cat_level_one) && $cat_level_one != ''){ foreach ($cat_level_one as $cat_one){ echo $cat_one->cat_id; } } ?>"
selected="selected">------------Select Category------------</option>
Thanks! for the consideration.
Updated code -
// in model
function cat_level_one($cat_level)
{
$this->db->where('cat_level',$cat_level);
$q = $this->db->get('fw_main_cat');
$data = $q->result_array();
return $data;
}
//controller function
public function getcategory()
{
**if ($this->session->userdata('session_status'))
{
$data['cat_level_one'] = $this->admin_cat_model->cat_level_one($cat_level);**
//Where this post data comes from??
$this->session->set_userdata('cat_level', $_POST['cat_level']);
$this->laod_view('admin_view/admin_cat/view_add_category', $data);
}else {
redirect ('admin_view/admin_cat/view_category');
}
}
//in view
<li class="full-row">
<select name = 'cat_level_one' id = 'cat_level_one'>
<option value="">-- Select Category --</option>
<?php foreach($cat_level_one as $cat_one){ ?>
<option value="<?php echo $cat_one->cat_id; ?>"><?php echo $cat_one->cat_title; ?></option>
<?php } ?>
</select>
</li>

detect if core flash messages in cakephp is an error or success message

I have copied the SesionHelper from the core into myapp/View/Helper so I can alter the div structure around the message outputted.
My problem is that I cant seem to detect if the message is an error or success message from the default cakephp message. I know I can set a flash message in my controller and add an attribute. But there doesn't seem to be any extra data that I can see from the core messages.
Example if the data is saved to the database i wish to show the message as green. Or if the data could not be saved then as red message.
public function flash($key = 'flash', $attrs = array()) {
$out = false;
if (CakeSession::check('Message.' . $key)) {
$flash = CakeSession::read('Message.' . $key);
$message = $flash['message'];
unset($flash['message']);
if (!empty($attrs)) {
$flash = array_merge($flash, $attrs);
}
if ($flash['element'] === 'default') {
$class = 'message';
if (!empty($flash['params']['class'])) {
$class = $flash['params']['class'];
}
$out = '<div id="' . $key . 'Message" class="' . $class . '">' . $message . '</div>';
} elseif (!$flash['element']) {
$out = $message;
} else {
$options = array();
if (isset($flash['params']['plugin'])) {
$options['plugin'] = $flash['params']['plugin'];
}
$tmpVars = $flash['params'];
$tmpVars['message'] = $message;
$out = $this->_View->element($flash['element'], $tmpVars, $options);
}
CakeSession::delete('Message.' . $key);
}
return $out;
}
What you are doing is reinventing the wheel as far as CakePHP is concerned.
You can specify an element as the second argument when you set a flash message in your controller method:
$this->Session->setFlash('Your record has been saved', 'flash_success');
Then in elements create an element Element/flash_success.ctp like this:
<div class="alert-success"><?php echo $message;?></div>
And finally in your view:
<?php echo $this->Session->flash()?>
Here is the section that deals with this in detail from the official documentation:
http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#creating-notification-messages

getElementById from select box to text field

Im making a selectbox with the values from my DB, and I want to write the ID of the selected item to a textbox.
How can it be done? I have this code..
<form method="POST" action="" id="submitKon">
<fieldset>
<table class="opretTable" border="0">
<tr>
<td width="180" height="30"><label for="valgtkon" class="labelLeft">Vælg konkurrencetype</label></td>
<td><select id="valgtTitel" name="valgtTitel" onchange="run()">
<?php
$virksomhedsID = $_SESSION['virkID'];
$sql = "SELECT * FROM konkurrenceType ORDER BY konkurrenceType.kontypeID";
$result = mysql_query($sql) or die(mysql_error());
echo '<option value="Vælg type">Vælg type</option>';
while($rowSelect = mysql_fetch_assoc($result))
{
echo '<option value="' . $rowSelect['kontypeID'] . '">' . $rowSelect['kontypeTitel'] . '</option>';
}?>
</select>
and..
<script>
function run() {
document.getElementById("valgtID").value = document.getElementById("valgtTitel").value;
}
</script>
I get this
INSERT INTO `mah1233411190550`.`konkurrence` ( `konID` , `virkID` , `konTitel` , `konBeskriv` , `konMaal` , `konMaaltype` , `konStart` , `konSlut`, `kontypeID`, `holdID` ) VALUES (NULL , '1', '1', 'cykle', '500', 'km', '2013-01-01', '2018-05-03', '1', '' );
So konTitel is the same as kontypeID, why is that?
You can do something like:
var valgtTitel = document.getElementById("valgtTitel");
document.getElementById("valgtID").value = valgtTitel.options[valgtTitel.selectedIndex].value;
Here's fiddle: http://jsfiddle.net/viralpatel/JYHLM/
Edit:
Perhaps you might want to save the title (apart from ID) in some other textbox and retrieve it from request:
var valgtTitel = document.getElementById("valgtTitel");
document.getElementById("valgtTitle").value = valgtTitel.options[valgtTitel.selectedIndex].innerHTML;
Note how we used innerHTML instead of value to get the title.
So something like this??
<tr>
<td width="180" height="30"><label for="valgtkon" class="labelLeft">Vælg konkurrencetype</label></td>
<td><select id="select" name="select" onchange="run()">
<?php
$virksomhedsID = $_SESSION['virkID'];
$sql = "SELECT * FROM konkurrenceType ORDER BY konkurrenceType.kontypeID";
$result = mysql_query($sql) or die(mysql_error());
echo '<option value="Vælg type">Vælg type</option>';
while($rowSelect = mysql_fetch_assoc($result))
{
$kontypeID = $rowSelect['kontypeID'];
$kontypeTitel = $rowSelect['kontypeTitel'];
echo '<option value="' . $kontypeID . '">' . $kontypeTitel . '</option>';
}?>
</select></td>
<script>
function run() {
var select = document.getElementById("select");
document.getElementById("valgtID").value = valgtTitel.options[select.selectedIndex].innerHTML; //how to make this right?
document.getElementById("valgtTitel").value = valgtTitel.options[select.selectedIndex].innerHTML; //how to make this right?
}
</script>
<td><input type="text" name="valgtID" id="valgtID"/><input type="text" name="valgtTitel" id="valgtTitel"/></td>
</tr>

Resources