I have small problem with my PHP code. I must insert to database multiple values but I dont have any idea how I can do this.
My View:
<?php foreach($myKidsGroupID as $row): ?>
<label><input type="checkbox" name="user_my_group_msg" value="<?php echo $row->id; ?>" class="my_group_msg pull-right"><?php echo $row->firstname; ?> <?php echo $row->lastname; ?></label><br>
<?php endforeach; ?>
And my Model looks like this:
...
elseif($checked_my_group == 1) {
foreach ( ?? ) {
$new_mail = array(
'to_user' => $this->input->post('user_my_group_msg'),
);
$this->db->insert('mailbox', $new_mail);
}
}
...rest code....
Inside my View I display all users as checkbox but If I select two persons I must INSERT two query to database. Anyone can help me?
simple thing is
use serialize function don't use foreach
serialize $this->input->post('user_my_group_msg')
try it once it will help you your View code will look as like
<?php foreach($myKidsGroupID as $row): ?>
<label><input type="checkbox" name="user_my_group_msg" value="<?php echo $row->id; ?>" class="my_group_msg pull-right"><?php echo $row->firstname; ?> <?php echo $row->lastname; ?></label><br>
<?php endforeach; ?>
Model will be corrected like this
<?php
elseif(sizeof($this->input->post('user_my_group_msg'))>=1) {
foreach ( $this->input->post('user_my_group_msg') as $value ) {
$new_mail = array(
'to_user' => $value,
);
$this->db->insert('mailbox', $new_mail);
}
}
.....rest code
?>
Related
Currently I have a permissions table where the user can use a checkbox to give access or revert access for a URL link to a user with a particular role. So for this I've made it so that when a checkbox is pressed, the id of that checkbox and the role of the user are inserted in my database, but I'm unable to insert it in the database for some reason. I've tried the following code:
Controller Class:
public function permission()
{
if ($this->form_validation->run() == FALSE)
{
$main['permissions']=$this->users_model->get_permission_array();
$main['roles']=$this->users_model->get_roles_array();
foreach($main['roles'] as $key => $val):
$main['access'][$val['roles_id']]=$this->users_model->get_access_array(array('roles_id'=>$val['roles_id']));
endforeach;
$main['page'] = 'crm/users/permission';
$this->load->view('crm/index', $main);
}
if($this->input->post())
{
$loginid=false;
foreach($main['roles'] as $key => $val):
if(isset($_POST['roleid'.$val['roles_id']])){
$this->users_model->clear_access(array('roles_id'=>$val['roles_id']));
foreach($_POST['roleid'.$val['roles_id']] as $id => $access):
$data=array('roles_id'=>$val['roles_id'],'permissions_id'=>$access);
$loginid=$this->users_model->permission_access($data);
endforeach;
}
endforeach;
if($loginid){
$this->session->set_flashdata('message', '<p>Permission updated Successfully.</p>');
redirect('users/permission');
} else {
$this->session->set_flashdata('message', '<p>Error!! - Permission not updated.</p>');
redirect('users/permission');
}
}
}
Model Class:
function get_permission_array()
{
$query = $this->db->get("crm_client_permissions");
return $query->result_array();
}
function get_access_array($cond)
{
$this->db->select("permissions_id");
$this->db->where($cond);
$query = $this->db->get("crm_client_role_access");
return $query->result_array();
}
function clear_access($cond)
{
return $this->db->delete("crm_clients_access",$cond);
}
function permission_access($data)
{
return $this->db->insert("crm_clients_access",$data);
}
function get_roles_array($cond='')
{
if($cond !='') $this->db->where($cond);
$query = $this->db->get("crm_client_roles");
return $query->result_array();
}
View Class:
<div <?php echo form_open_multipart('users/permission'); ?>>
<table>
<?php if($permissions) $i=0;foreach($permissions as $key => $permission): ?>
<tr>
<td class="align-center"><?php echo ++$i; ?></td>
<td><?php echo $permission['page']; ?></td>
<td><?php echo $permission['url']; ?></td>
<?php foreach($roles as $rolekey => $role):
if($role['roles_id'] == 1)$checked = 'checked';
if(in_array($permission['permissions_id'],array_map('current',$access[$role['roles_id']])))
$checked = 'checked';
else
$checked = ''; ?>
<td align="center"><div class="checkbox checkbox-success m-t-0"><input type="checkbox" class="accessbox"
id="role<?php echo $rolekey ?>-<?php echo $key ?>" name="roleid<?php echo $role['roles_id']; ?>[]"
<?php echo $checked?> <?php echo ($role['roles_id'] == 1) ? 'disabled="disabled"' : '' ?> value="<?php echo $permission['permissions_id']; ?>" />
<label for="role<?php echo $rolekey ?>-<?php echo $key ?>"></label></div></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
<div class="text-center">
<button type="submit" class="btn btn-info">Save Permission</button> Cancel
</div>
<?php echo form_close(); ?> </div>
But here I always get Error!! - Permission not updated. in my view class which means it jumps to the else part of my controller. I'm not sure where I'm going wrong here
It seems problem is here $loginid=$this->users_model->permission_access($data); and here return $this->db->insert("crm_clients_access",$data);.
My advise is to:
use XDebug to debug your code and see what insert function returns and why.
Also you may look for php log to see if there is any database errors. To use error logs you need to find php.ini config file and enable line 'error_log = /path/to/fige.log'.
Also you may to lookup the database data to determine if insert function makes new row in table crm_clients_access, if it doesn't? you need to check your connection config to database host:port, so database process must be available at theese host:port.
I've got a list of countries, sometimes the list contains just one country and sometimes more. This is my code:
<?php if($this->value): ?>
<a class="tag" href="{{env::url}}/business?land=<?php echo $this->value; ?>" title="<?php echo $this->value; ?>"><?php echo $this->value; ?></a>
<?php endif; ?>
The output right now is: "Germany,Austria,Switzerland".
I want to create a link for every country, how can I do that?
I hope you guys can help me.
I assume that you are getting comma separate list of countries inside $this->value.
We can use function like explode to split that string into array and then use foreach to loop through array and generate individual link
<?php if($this->value): ?>
<?php
$array = explode( ',', $this->value );
?>
<?php foreach($array as $value): ?>
<a class="tag" href="{{env::url}}/business?land=<?php echo $value; ?>" title="<?php echo $value; ?>"><?php echo $value; ?></a> -
<?php endforeach; ?>
<?php endif; ?>
I hope this answers your question.
I assume that $this->value is an array so you can get the countries one by one using foreach loop like below:
<?php if($this->value): ?>
<? $countries = $this->value; ?>
<? foreach ($countries as $country): ?>
<a class="tag" href="{{env::url}}/business?land=<?php echo $country; ?>" title="<?php echo $country; ?>"><?php echo $country; ?></a>
<? endforeach; ?>
<? endif; ?>
if it is string than you can use explode() function and get the countries as an array like:
<?php $countries = explode("," , $this->value); ?>
and pass this $countries array to foreach loop.
For achieving this you need to check and explode the country string by comma and than loop it in foreach.
Sample code:
<?php
if ($this->value) {
$countries = explode(",", $this->value);
if (count(countries) > 1) {
foreach ($countries as $country) {
?>
<a class="tag" href="{{env::url}}/business?land=<?php echo $country; ?>" title="<?php echo $country; ?>"><?php echo $country; ?></a>
<?php
}
} else {
?>
<a class="tag" href="{{env::url}}/business?land=<?php echo $this->value; ?>" title="<?php echo $this->value; ?>"><?php echo $this->value; ?></a>
<?php
}
}
?>
Please help with this code:
<ul class="activity-projects">
<?php foreach ($ownProjects as $userProject) : ?>
<li>
<a class="<?php echo ($userProject->id == $project_id) ? 'active' : '';?>"
href="<?php echo Yii::app()->createUrl('user/activity', array(
'user_id'=>$user->id,
'project_id'=>$userProject->id,
'date'=>$date)
); ?>"><?php echo $userProject->name; ?></a>
</li>
<?php endforeach; ?>
<?php foreach ($projects as $userProject) : ?>
<li>
<a class="<?php echo ($userProject->project_id == $project_id) ? 'active' : '';?>"
href="<?php echo Yii::app()->createUrl('user/activity', array(
'user_id'=>$user->id,
'project_id'=>$userProject->project->id,
'date'=>$date)
); ?>"><?php echo $userProject->project->name; ?></a>
</li>
<?php endforeach; ?>
</ul>
How to change it to dropdown list, using CHtml::dropDownList. Thanks for watching!
First, you need to define a key-value array like this:
$options = array();
<?php foreach ($ownProjects as $userProject)
array_push($options, array($userProject->id => $userProject->name));
?>
echo CHtml::dropDownList('seletName', '1', $options);
This will be produced an html <select> tag with "seletcName" name. And also option with value "1" will be selected option. You can use your desired value for first and second parameters.
Also you can use CActiveForm#dropDownList for this purpose.
In your form, use the form dropDownList() function.
<?php echo $form->dropDownList(
$model,
'project_id',
CHtml::listData(OwnProjects::model()->findAll(),
'id', 'name'),
array('empty' => '(Select project)','class'=>"form-control")
);
?>
From your example, it looks like OwnProjects is not a model on its own, but a subset of a model. You can customise the query
<?php echo $form->dropDownList(
$model,
'project_id',
CHtml::listData(OwnProjects::model()->findAllByAttributes(array('user_id'=> Yii:app()->user->id),
'id', 'name'),
array('empty' => '(Select project)','class'=>"form-control")
);
?>
This solution finally helps me:
<?php $options = array();
foreach ($projects as $userProject) :
$options[$userProject->id] = $userProject->project->name;
endforeach;
echo CHtml::dropDownList('selectName', '1', $options);
?>
I have to input several strings into table in db. Its tabular input yii. Cannot understand, how to do it this way, if i have such controller:
public function actionCreate()
{
$model1=new Section;
$model2=new SectionI18n;
if(isset($_POST['SectionI18n'])){
foreach ($model2 as $i => $m1) {
foreach($_POST['SectionI18n'] as $i => $m1)
{
$m1=new SectionI18n;
$m1->attributes=$_POST['SectionI18n'][$i];
$my[]=$m1->attributes;
}
}
and some layout:
<?php foreach ($model2 as $i=>$m1):?>
<?php ?>
<div class="row">
<?php echo $form->labelEx($model2,'Название'); ?>
<?php echo $form->textField($model2,'[$i]title',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model2,'title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model2,'Описание'); ?>
<?php echo $form->textArea($model2,'[$i]description',array('size'=>60,'maxlength'=>200)); ?>
<?php echo $form->error($model2,'description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model2,'Язык'); ?>
<?php echo $form->dropDownList($model2,'[$i]lang',array('ru'=>'ru','en'=>'en','ua'=>'ua')); ?>
<?php echo $form->error($model2,'lang'); ?>
</div>
<?php ?>
<?php endforeach;?>
Help please
It would be something similar to this:
foreach ( $_POST[Model Name] as $i => $y ){
$var name[$i] = new Model Name;
$var name[$i]->title = $_POST[Model Name][$i][title]
...
}
var_dump the post ($_POST), object makes it easier to see.
I have used following code to create a checkbox in WordPress pages (in admin section):
<?php while($mb->have_fields_and_multi('sidebar-block')): ?>
<?php $mb->the_group_open(); ?>
<!-- Some more code here for other fields -->
<p class="checkbox">
<input name="<?php $metabox->the_name('blue-block'); ?>" type="checkbox" value="1" <?php if ($metabox->get_the_value('blue-block')) echo ' checked="checked"'; ?>>
<label>Do not use Blue Block for content</label>
</p>
<?php $mb->the_group_close(); ?>
<?php endwhile; ?>
Above code is working fine. My question is how can i check if this checkbox is checked on the main website (frontend)?
Following is the code in my template for frontend:
<?php
$my_meta = get_post_meta($post->ID,'_sidebar_meta',TRUE);
if ($my_meta['sidebar-block']) {
foreach ($my_meta['sidebar-block'] as $sidebar)
{
?>
<div id="aside-blue">
<?php if ($sidebar['side_heading']) { ?>
<h2 class="sideheading"><?php echo $sidebar['side_heading']; ?></h2>
<?php } ?>
<?php echo apply_filters( 'the_content', $sidebar['side_content'] ); ?>
</div>
<?php } ?>
<?php } ?>
I want to add id="aside-blue" only when checkbox is checked.
i tried following code for this, but it's not working
<?php
$my_meta = get_post_meta($post->ID,'_sidebar_meta',TRUE);
if ($my_meta['sidebar-block']) {
foreach ($my_meta['sidebar-block'] as $sidebar)
{
?>
<div <?php if ($sidebar['blue-block'] == 'yes') { ?>id="aside-blue"<?php } ?>>
</div>
<?php } ?>
<?php } ?>
Check in the wp_postmeta table how the value is stored. When I used checkboxes, the values were serialized. You'll probably need to unserialize it before using the meta value.