I need to create to dependant dropDownLists with yii. Thus, I create a view and an action in controller like this :
The view
<?php
echo "<div class='row'>";
echo $form->labelEx($model, 'id_structure');
echo $form->dropDownList($model, 'id_structure', GxHtml::listDataEx(StructureInformation::model()->findAllAttributes()),
array('ajax' => array('type' => 'POST',
'url' => CController::createUrl('InfoComplementAAjouter/fillTypeList'),
'update' => '#typeDonnee',
'data' => array('id_structure' => 'js:this.value'))
));
echo $form->error($model, 'id_structure');
echo '</div>';
echo "<div class='row'>";
echo $form->labelEx($model, 'type');
echo $form->dropDownList($model, 'type', array(), array('prompt' => 'Sélectionner', 'id' => 'typeDonnee'));
echo $form->error($model, 'type');
echo '<div>';
?>
Now the action in the Controller InfoComplementAAjouterController :
public function actionFillTypeList(){
$id_struct = $_POST['InfoComplementAAjouter']['id_structure']; //I get the selected value
$taille = StructureInformation::model()->find('id = ' . (int)$id_struct)->taille; //and then pass it to the StructureInformation model to obtain the attribute taille
//I create two arrays which content will be the options of my dropDownList.
$un = array('text'=> 'Texte', 'radio' => 'Bouton radio', 'dropDownList' => 'Liste déroulante');
$plusieurs = array( 'checkboxlist' => 'Choix multiple',);
//If $taille == 1, I display the contents of $un; if $taille > 1, the content of $plusieurs will be displayed.
if($taille == 1){
foreach ($un AS $value => $name){
$opt = array();
$opt['value'] = $value;
echo CHtml::tag('option', $opt, CHtml::encode($name), true);
}
}
if($taille > 1){
foreach ($plusieurs AS $value => $name){
$opt = array();
$opt['value'] = $value;
echo CHtml::tag('option', $opt, CHtml::encode($name), true);
}
}
die;
}
I remark that the action is not executed. I can not understand why.
Can somebody help me to fix the problem ?
I just saw a typo:
'upedate' => '#typeDonnee',
should be 'update'
try with fixing it first
Edit:
you have more small errors/typos
echo $form->dropDownList($model, 'id_structure', GxHtml::listDataEx(StructureInformation::model()->findAllAttributes(),
array('ajax' => array('type' => 'POST',
'url' => CController::createUrl('InfoComplementAAjouter/fillTypeList'),
'update' => '#typeDonnee',
'data' => array('id_structure' => 'js:this.value'))
));
here you need to close parentheses
=====================================================
StructureInformation::model()->findAllAttributes()
there is no findAllAttributes() method in CActiveRecord
maybe you wanted to use findAllByAttributes(), but then you would want to pass some attributes. If this is your custom method then you should explain how it works, or paste a code
Edit 2:
$_POST['InfoComplementAAjouter']['id_structure']
Try to access post like this:
$_POST['id_structure']
I think you send post in that structure
It's not that easy for me to duplicate your situation, so I'm kinda shooting in the dark
Related
I have some code that takes an rss feed url, expands the rss link and gets the individual links inside that feed xml. After checking if a link exists, i insert it to a table if it does not exist and do nothing if it does. However my code is becoming more unreadable and one more check that requires another foreach it shall be even more unreadable.
This is the code
public function links_cron_job(){
//Get Rss Links
$this->db->select("the_link_itself");
$query = $this->db->get_where("search_engine_links", array("link_type" => "rss"));
foreach ($query->result() as $row){
$url = $row->the_link_itself;
$rss = Feed::loadRss($url);
foreach ($rss->item as $item) {
$this->db->where('the_link_itself',$item->link);
$query3 = $this->db->get('search_engine_links');
if ($query3->num_rows() > 0){
echo 'duplicates are there';
}else{
$data = array(
'link_country' => 'usa',
'the_link_itself' => $item->link,
'link_category' => 'news_website',
'link_added_by' => 'admin',
'link_type' => 'ordinary_link',
'link_local_type' => 'news',
'link_region' => 'countrywide',
'link_city' => 'washington',
'date_added' => $item->timestamp,
'last_updated' => time()
);
$this->db->insert('search_engine_links', $data);
echo 'no duplicates are there';
}
}
}
}
What would be another approach in doing what i am doing?
Normally I would say, just enter a return. But in this case you still might have
work to do in the extra iterations. So in this case at least we know that we are
done with this iteration, so at least we can add a continue statement:
<?php
foreach ($rss->item as $item) {
$this->db->where('the_link_itself',$item->link);
$query3 = $this->db->get('search_engine_links');
if ($query3->num_rows() > 0) {
echo 'duplicates are there';
continue;
}
$data = array(
'link_country' => 'usa',
'the_link_itself' => $item->link,
'link_category' => 'news_website',
'link_added_by' => 'admin',
'link_type' => 'ordinary_link',
'link_local_type' => 'news',
'link_region' => 'countrywide',
'link_city' => 'washington',
'date_added' => $item->timestamp,
'last_updated' => time()
);
$this->db->insert('search_engine_links', $data);
echo 'no duplicates are there';
}
I am trying to save a custom post type. I can't figure out why the save isn't working for the input fields listed below.
I included all the relevant code, as I can't see where the problem might be. It seems like everything is working except for the save function (listed last).
I am only trying to save inputs where 'type' => 'singleinput'. I haven't tried to save the 'fulltext' ones.
Can anyone point out where I am going wrong?
//input boxes for custom post
$specialpage->inputs = array(
'item1' => array(
'type' => 'singleinput',
'headline' => 'Page headline',
'bullet1' => 'Bullet Point 1:',
'bullet2' => 'Bullet Point 2:',
'bullet3' => 'Bullet Point 3:'
),
'item2' => array(
'type' => 'fulltext',
'promo1' => 'Promo text 1:',
'promo2' => 'Promo text 2:',
'promo3' => 'Promo text 3:'
),
'item3' => array(
'type' => 'singleinput',
'quote' => 'The quote'
)
);
This function is called to display the meta box.
public function display_meta_box_($post_object){
foreach($this->inputs as $item){
foreach($item as $name => $title){
if ($title == 'singleinput'){
$activate = 'singleinput';
continue;
}
if ($title == 'fulltext'){
$activate = 'fulltext';
continue;
}
if ($activate == 'singleinput'){
$this->singleinput[$name] = $title;
${$this->name . $name} = get_post_meta($post_object->ID, $this->name . $name, true);
echo '<span class="meta_titles">' . $title . '</span>';
?>
<input type='text' class='input_single' name='<?php echo $this->name . $name;?>_name' value='<?php echo ${$this->name . $name}; ?>' >
<?php
}
}
This is the save function
public function save_profile( $post_id, $post_object ) {
if( $this->name == $post_object->post_type){
//check if singleinput are defined for this custom post type
if ($this->singleinput){
//save single line text input boxes
foreach ($this->singleinput as $name => $title){
update_post_meta($post_id, $this->name . $name, $_POST[$this->name . $name . '_name']);
}
}
}
Edit: Added Save Function
$simplejoiner->saveNow();
public function saveNow(){
add_action( 'save_post', array($this, 'save_profile'), 10, 2 );
}
I want to use onsubmit="return travel_form(this);
in active form
<?php echo form_open_multipart('employee/travel/store_travel' ,['id'=>'abc']); ?>
How to use it. Please guide me.
You can do with this :
<?php array('onsubmit' => "return travel_form(this)"); ?>
This is another way.
<?php $data = array('id' => 'abc',
'onsubmit' => 'return travel_form(this);');
echo form_open_multipart('employee/travel/store_travel' ,$data);
?>
Simply put it on your 2nd parameter which is the attribute
<?php
$attribute = array(
'id' => 'abc',
'onsubmit' => 'return travel_form(this)'
);
echo form_open_multipart('employee/travel/store_travel' , $attribute);
?>
I am trying to do a website (don't ask what it is) and I need help.
I have created this page called meallist.php :
<?php
/* Ingredients List */
$meatlist = array(tavuketi => "Tavuk Eti",
baliketi => "Balık Eti");
$vegelist = array(domates => "Domates",
patates => "Patates",
salatalik => "Salatalık");
/* The Meals - dun dun duuuunnnnnn */
$yemekaralist = array(mangaldatavuk => array(
name => "Mangalda Tavuk",
ing1 => tavuketi),
mangaldapatateslitavuk => array(
name => "Mangalda Patatesli Tavuk",
ing1 => tavuketi,
ing2 => patates),
mangaldabalik => array(
name => "Mangalda Balık",
ing1 => baliketi),
firindapatateslibalik => array(
name => "Fırında Patatesli Balık",
ing1 => baliketi,
ing2 => patates));
?>
You get the idea. I made a foreach loop on index.php that creates a select:
<div id="etler">
<select multiple="multiple" name="selectMeal[]" size="4" data-max-length="30">
<?php
foreach($meatlist as $id => $name)
{
print "<option value=\"" . $id . "\">" . $name . "</option>";
}
?>
</select>
</div>
<div id="sebze">
<select multiple="multiple" name="selectMeal[]" size="4" data-max-length="30">
<?php
foreach($vegelist as $id1 => $name1)
{
print "<option value=\"" . $id1 . "\">" . $name1 . "</option>";
}
?>
</select>
</div>
And it posts the selectMeal[] and gets it into $search in searchmeal.php. The thing I want is, I want it to search the $yemekaralist in meallist.php and check if ing1 (and ing2 if available) if it matches with the selectMeal[] array, and print name => "something" if available. How can I do that?
Best regards,
mission712
Can you change the structure of $yemekaralist?
It would simplify your algorithm if you change the structure for something like this:
$yemekaralist = array(
item1 => array(
'name' => 'item1_name',
'ingredients' => array('tavuketi', 'patates'),
),
item2 => array(
'name' => 'item2_name',
'ingredients' => array('tavuketi', 'patates'),
),
And then go like this:
foreach ($yemekaralist as $item) {
foreach ($item['ingredients'] as $ingredient) {
foreach ($selectMeal as $selectedMeal) {
if ($selectedMeal == $ingredient) {
echo $item['name'];
}
}
}
}
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: helpers/form_helper.php
Line Number: 522
Here is my view that contains form
<?php
// open the form using CI form helper
echo form_open('loginController', array('id' => 'login'));
// first input field
echo "<div>";
echo form_label('Username', array('for' => 'login_username'));
echo form_input(array(
'type' => 'text',
'name' => 'username',
'id' => 'login_username',
'value' => set_value('username')
));
echo "</div>";
// second input field
echo "<div>";
echo form_label('Password', array('for' => 'login_password'));
echo form_password(array('type' => 'password', 'name' => 'password', 'id' => 'login_password'));
echo "</div>";
// submit button
echo "<div class='submit'>";
echo form_button(array('type' => 'submit', 'content' => 'Log in'));
echo "</div>";
echo "<div class='errors'>";
// display if incorrect username/password
if ($this->session->flashdata('login_error'))
echo 'Incorrect username/password';
// display if incorrect input data
echo validation_errors();
echo "</div>";
// close the form
echo form_close();
?>
Here is form_helper.php (line 513 to 537) that is mentioned in PHP Error
if ( ! function_exists('form_label'))
{
function form_label($label_text = '', $id = '', $attributes = array())
{
$label = '<label';
if ($id != '')
{
$label .= " for=\"$id\"";
}
if (is_array($attributes) AND count($attributes) > 0)
{
foreach ($attributes as $key => $val)
{
$label .= ' '.$key.'="'.$val.'"';
}
}
$label .= ">$label_text</label>";
return $label;
}
}
Here
echo form_label('Username', array('for' => 'login_username'));
and here
echo form_label('Password', array('for' => 'login_password'));
You are passing an array for the id parameter of the function that should be a string try
echo form_label('Username', "lblUsername", array('for' => 'login_username'));
and
echo form_label('Username', "lblPassword", array('for' => 'login_username'));
Or simply pass null in as the value of the id.
You are calling function in wrong way. The 3rd parameter is array but you are passing 2nd parameter as array.
Use below kind of function call for all form_label.
echo form_label('Username', 'Username',array('for' => 'login_username'));
^^^^^^^^^^^^
Function definition
function form_label($label_text = '', $id = '', $attributes = array())