i'm trying to update a custom table for my wordpress plugin, first im getting the roles from my table then display it using a form and this works fine, now the problem is that when i check the boxs it update in database, but when i uncheck it, it dont update at all and if i uncheck it all i get this error message
Warning: Invalid argument supplied for foreach() in C:\wamp\www\wp_test\wp-content\plugins\Data\settings.php on line 52
<?php
$roles=get_editable_roles();
global $wpdb;
$table_name = $wpdb->prefix. "Author_detailed_repport";
?>
<h3>Settings Page</h3>
<h4>Add/Remove a role from filter list</h4>
<p>This setting allow you to add/remove roles from the filter<br />
list, here down a list of all the roles existing in your website, all<br />
you have to do is to check/uncheck wich you wanna add/rmove from filter list</p>
<form action="<?php $_REQUEST['PHP_SELF'] ?>" method="post">
<?php
require_once('../wp-config.php');
$i=0;
foreach($roles as $role)
{
$rom=$role['name'];
$results = $wpdb->get_results( "SELECT * FROM ".$table_name." WHERE role= '".$rom."'" );
if ($results==NULL)
{$wpdb->insert( $table_name, array(
'role' => $role['name'],
'statut' => '',
'post_number' => '',
'activate' => ''
));
}?>
<input type="checkbox" name="cat[]" value="<?php echo $i+1 ;?>" <?php checked($results[0]->statut, $i+1); ?> />
<input type="hidden" name="ww" value="0">
<?php
?>
<label>
<?php echo $results[0]->role;?></label><br />
<?php $i++; } ?>
<input type="submit" value="save" name="saveme" />
</form>
<?php
if(isset($_POST['saveme']))
{
$cats=$_POST['cat'];
foreach($cats as $cam)
{
if(isset($cam))
{
$wpdb->update( $table_name, array(
'statut' => $cam
),array('ADR_id' => $cam),array('%d'));}
else
{
$wpdb->update( $table_name, array(
'statut' => '0'
),array('ADR_id' => $cam),array('%d'));
}
}
}
?>
Because $roles is not set yet and there you need to do a another check
If(isset($roles)) {
//foreach loop goes in here
}
Related
got a question about cakephp3 search function, is it possible to search among two tables/Controllers in a search form?
this is the codes, am trying to search from categories name and also products name, their relationship is products has category id
this is the code from the Controller
$c = $this->request->getQuery('c');
if(!empty($c)) {
$this->loadModel('Categories');
$category = TableRegistry::get('Categories');
$query = $this->Products->find()->contain(['Categories'=>['Products']])
->where(['Categories.name LIKE' => '%' . $c. '%']);
}
$this->set(compact('c','query'));
$key = $this->request->getQuery('key');
if(!empty($key)) {
$products->where(['Products.name LIKE ' => '%'.$key.'%']);
}
$this->set(compact('key'));
this is from the HTML side, where i name="c", it only search for c and not key,
<?php echo $this->Form->create(null, ['type' => 'get', 'valueSources' =>
'query'); ?>
<?php echo $this->Form->control('search', ['type' => 'hidden']); ?>
<input type="text" class="form-control" name="c" placeholder="Search...">
<span class="input-group-append">
<button type="submit">
<?php echo $this->Form->end(); ?>
is there a method to combine both search in a form?
thank you for your helps
I'm trying to create a system of posts and custom fields. Ex: I create the post type "Product" and then I associate it some fields: "Name", "Image", "Price"...
When I create one of this fields I save into my db an input field, for example for Name I will insert something like this:
$data = array(
'name' => $field_key,
'id' => $field_key,
'class' => 'form-control '.$type->type_key,
'type' => $type->type_key,
'data-input-type' => $type->type_key
);
return form_input($data);
Then when I go to create my first post "Product" I want to populate a form with my custom fields.
<form method="post" action="<?php echo site_url('admin/posts/manage').'/'.$post_id; ?>">
<?php
foreach ($post_fields as $field) {
?>
<div class="form-group">
<label>
<?php
echo $field->name;
?>
</label>
<?php
// Here the field input
echo $field->meta_value;
?>
</div>
<?php
}
?>
<?php if(isset($post)){ echo $post->name;}else{echo set_value('name');} ?>
<div class="form-group">
<input type="submit" value="Save" name="save">
</div>
</form>
There's no problem since I have to create a new one. But when I have to edit my post I don't know how to load field's value for that single post, because in my form_input $data I can't put something like
$value = (isset($post)) ? $post->name : set_value($field_key);
and in the $data array
'value' => $value
Somebody have an idea on what I can do?
Thank you and sorry for my elementary english.
SOLVED. I create a model which load the correct input type. I pass post values by the controller to this model to populate input values correctly, if the action required is "edit".
I created a drop-down to select the category for search. When I search the product for example I search Shoes for MEN, when the view page loaded the item resets to default
I want the category to remain what I selected
<form action="<?php echo Yii::app()->baseUrl; ?>/search" method="GET" class="form-inline form-section-2 row fadeInDown animated">
<div class="col-sm-5 form-group">
<input type="text" name="loc" class="form-control" id="loc" value="<?php echo $locationdet ; ?>" placeholder="Enter Your Keyword">
</div>
<div class="col-sm-4 form-group" >
<select name="cat" class="form-control selectpicker">
<option>Select Category</option>
<option value = '0'>Men</option>
<option value = '1'>Women</option>
</select>
</div>
<div class="col-sm-3 form-group">
<button type="submit" class="btn btn-default btn-new">Search Products</button>
</div>
</form>
Try this:
<option value = '0' <?php if(isset($_GET['cat']) && $_GET['cat'] == '0') echo "selected" ?>>Men</option>
<option value = '1' <?php if(isset($_GET['cat']) && $_GET['cat'] == '1') echo "selected" ?>>Women</option>
use
echo CHtml::dropDownList('cat',isset( $_REQUEST['cat'] ) ? $_REQUEST['cat'] : NULL, array('0'=>'Men', '1'=>'Women'),
array('empty'=>'Select Category', 'class' => 'form-control selectpicker'));
to achieve yii style,
cheers
You will have to pass the selected option via the controller back to the view.
In the controller you will need something like this:
$this->render('viewName', array('name' => 'valueOfTheList'))
Then in the view you can use
<option value = '0' <?php if($name == '0') echo "selected" ?>>Men</option>
<option value = '1' <?php if($name == '1') echo "selected" ?>>Women</option>
However. Since you are using Yii. I would advise you to look at CHTML::dropDownList(). Then you could do something like
<?php echo CHtml::dropDownList('name', $select,
array('M' => 'Male', 'F' => 'Female'));
Which is really a more Yii way to approach these kind of things.
Yii way to implement this functionality.
You can keep the form state by setting the user input value to Model properties. For this, you can use CFormModel to implement, same like YII's default login page. Below is a sample example.
Create a form model for your search (SearchForm.php) and place this inside models folder.
class SearchForm extends CFormModel
{
public $search_key;
public $search_cat;
public function rules()
{
return array(
array('search_key,search_cat', 'required'),
);
}
}
Assume i am using SiteController. I want to show this search form in my index page. When i submit the form it will submitted to search action
class SiteController extends Controller
{
public function actionIndex()
{
$searchModel=new SearchForm();
$searchModel->search_key;
$searchModel->search_cat;
$this->render('index',array('searchModel'=>$searchModel));
}
public function actionSearch()
{
$searchModel=new SearchForm();
if($_POST['SearchForm'])
{
$searchModel->attributes=$_POST['SearchForm'];
}
$this->render('search',array('searchModel'=>$searchModel));
}
}
$searchModel->attributes=$_POST['SearchForm']; That is i am resetting the user inputs to model.So, in your view the form will appear with user input values.
Call this Form in views
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'search-form',
'enableClientValidation' => true,
'action'=>array('default/search'), //Submiting my form to Search action
));
?>
<?php echo $form->textField($searchModel, 'search_key'); ?>
<?php
$htmlOptions = array('size' => '1', 'prompt' => 'Select');
$list = array('0' => 'Men', '1' => 'Women'); // You can load your Categories from the Database table/Model.
echo $form->dropDownList($searchModel, 'search_cat', $list, $htmlOptions);
?>
<?php echo CHtml::submitButton('Search'); ?>
<?php $this->endWidget(); ?>
Hope, This will help you for your better practice.
I am trying to build a search query for report in my CakePHP application. In my query condition building go through a if, else statements. On initial load of report I want to load the pre-defined criteria and when user submit the search conditions will be changed on selection.
This is my initial conditions
$conditions['Report.expense_status'] = array('COMPLETED');
$conditions['DATE(Report.created) >='] = "date_sub(now(), interval 2 month)";
When user do a search conditions should update as below.
if ($this->request->is('post')) {
$search = $this->request->data('Report');
if( strlen($search['From']) > 0 ){
$conditions['DATE(Report.created) >='] = date( 'Y-m-d',strtotime($search['From']) );
}
}
But my initial condition code output the SQL expression as below and it is not getting populate the result.
DATE(`Report`.`created`) >= 'date_sub(now(), interval 2 month)'
it is adding quotes to the expression.
How should prevent adding quotes around condition?
Form:
<?php echo $this->Form->create('Report', array('action' => 'index')); ?>
<div>
<?php
echo $this->Form->input('Keyword');
echo $this->Form->input('Status',
array(
'options' => $status,
'multiple'=> 'false'
)
);
echo $this->Form->input('Child',
array(
'options' => $children,
'multiple'=> 'false'
)
);
echo $this->Form->input('From',
array(
'id' => 'report_from'
)
);
echo $this->Form->input('To',
array(
'id' => 'report_to'
)
);
?>
<div class="footer">
<div>
<input type="submit" value="Search" name="action">
<input type="button" value="Cancel" name="action">
</div>
</div>
</div>
<?php echo $this->Form->end(); ?>
I'm not entirely sure way but the default values for a custom form aren't displaying. Any ideas?
Is it something to do with locating templates?
$blav_options_address = array(
'street' => 'Street Address',
'county' => 'County Address',
'postcode' => 'Postcode',
);
function blavou_setup_address() {
global $blav_options_address;
if ( ! isset( $_REQUEST['updated'] ) )
$_REQUEST['updated'] = false;
?>
<?php if ( isset( $_GET['settings-updated'] ) ) {
echo "<div class='updated'><p>Settings Saved. <a href='?page=blav-setup-social'>Continue Setup</a>.</p></div>";
} ?>
<?php locate_template( 'admin/templates/blav-setup-address.php', TRUE, TRUE ); ?>
<?php
}
<div id="blav-wrapper">
<div class="blav-nav-wrapper">
<h5 class="standard-title">Photographers Address</h5>
<form method="post" action="options.php" class="standard-form">
<?php $settings = get_option( 'blav_options_address', $blav_options_address ); ?>
<?php settings_fields( 'blav_theme_options_address' );?>
<input type="text" name="blav_options_address[street]" value="<?php esc_attr_e($settings['street']); ?>"/>
<input type="text" name="blav_options_address[county]" value="<?php esc_attr_e($settings['county']); ?>"/>
<input type="text" name="blav_options_address[postcode]" value="<?php esc_attr_e($settings['postcode']); ?>"/>
<input type="submit" value="Save"></input>
</form>
</div><!--end blav nav wrapper-->
</div><!--end blav-wrapper-->
I suspect that get_option() is not doing what you expect. When you execute:
$settings = get_option( 'blav_options_address', $blav_options_address );
then the only time that the default ($blav_options_address) will be used is if the option blav_options_address is not found. If that option is in the database, then the returned value will be used regardless of whether it is of the correct form or not, e.g.even if it is not an array. If it is not an array with the correct keys, then of course nothing will be displayed for the input values.