Check the first value of radio button (Codeigniter) - php

I am populating data from database to radio button and want to check the first value as default but I have no idea on how to do it. Here is my coding that I've done so far.
View
<?php if($designThemes != null){; ?>
<?php foreach ($designThemes as $designTheme) { ?>
<?php
$designThemesValue = (set_value('theme_name') ? set_value('theme_name') : $designTheme['slug_url']);
$designThemesAttr = array(
'name' => 'designThemes',
'value' => $designThemesValue,
);
?>
<div class="col-md-4 col-sm-4">
<div class="radio custom-radio">
<label>
<?php echo form_radio($designThemesAttr); ?>
<span class="fa fa-circle"></span> <?php echo $designTheme['theme_name'] ?>
</label>
</div>
</div>
<?php }}; ?>
If I add 'checked' => TRUE in $designThemesAttr = array() the system will check the last value of radio button as the default, but I wanted to check the first value.

solved your issue you can set it by conditional statement as replace your code by this
<?php if($designThemes != null){; ?>
<?php $i=1;foreach ($designThemes as $designTheme) { ?>
<?php
$designThemesValue = (set_value('theme_name') ? set_value('theme_name') : $designTheme['slug_url']);
$designThemesAttr = array(
'name' => 'designThemes',
'value' => $designThemesValue,
);
if($i==1){
$designThemesAttr['checked'] = TRUE;
}
?>
<div class="col-md-4 col-sm-4">
<div class="radio custom-radio">
<label>
<?php echo form_radio($designThemesAttr); ?>
<span class="fa fa-circle"></span> <?php echo $designTheme['theme_name'] ?>
</label>
</div>
</div>
<?php $i++; }}; ?>

'checked' => TRUE in $designThemesAttr = array() the system will check the last value of radio button as the default. This is right, because you doing it inside a for loop, and only one radio remains selected inside a group. So the last one is showing checked.
So maintain a variable to count the iteration like:
$count = 1;
if( $count = 1; )
{
// use 'checked' => TRUE here
$count++; // The value is incremented so that the if condition can't run again. This will add 'checked' => TRUE to first radio only.
}

Related

Display none to element in foreach if == 0

I have a foreach loop, and in that I have a more information button. I want it so that when x == 0 then the display to that more information button is 'none'
This is the variable that is different in each foreach item.
$issuesFixedCount
This is the class that needs to be display none if the variable == 0.
<a href="<?='version/?version=' . $item->name; ?>" class="moreInfo">Meer
My code:
<div class="pb-2 mb-3">
<h1 class="h2">Recente Jira releases</h1>
<div class="release-items" id="release-items">
<?php
foreach ($items as $item):
$proj = new ProjectService();
$versionService = new VersionService();
$version = $proj->getVersion('', $item->name);
$res = $versionService->getRelatedIssues($version);
$uns = $versionService->getUnresolvedIssues($version);
$issuesFixedCount = $res->issuesFixedCount;
$issuesUnresolvedCount = $uns->issuesUnresolvedCount;
$roundedIssue = $issuesFixedCount - $issuesUnresolvedCount;
if ($res->issueFixedCount == 0){
echo "
<script>
const moreInfoVar = document.querySelector('.moreInfo');
const.style.display = 'none';
</script>";
}
?>
<div class="item" id="item">
<h4>Versie: <?= $item->name; ?></h4>
<div class="details">
<p>Issues in versie: <?= $res->issuesFixedCount; ?> </p>
<p>Afgeronde issues: <?= $roundedIssue;?> </p>
<p>Nog te verwerken issues: <?= $uns->issuesUnresolvedCount;?> </p>
Meer info<br>
</div>
<small><?= $item->releaseDate; ?></small>
<a class="toggle">Toon meer</a>
</div>
<?php
endforeach;?>
<?php
$startAtValueNext = $_GET['startAt'];
$startAtNext = $startAtValueNext += 5;
$startAtValuePrevious = $_GET['startAt'];
$startAtPrevious = $startAtValuePrevious -= 5;
if ($startAtPrevious <= 0){
$startAtPrevious = 0;
}
?>
</div>
<div class="Next-Previous">
<- Vorige
Volgende ->
</div>
</div>
There's no need to inject JavaScript in this way. Instead, simply modify what is sent to the browser in the first place:
<p>Nog te verwerken issues: <?= $uns->issuesUnresolvedCount;?> </p>
<a href="<?='version/?version=' . $item->name; ?>" class="moreInfo" <?= ($res->issueFixedCount == 0 ? 'style="display:none;"' : ''); ?>>Meer info</a><br>
</div>
P.S. document.querySelector('.moreInfo') will only ever select the first element it finds which has that class. It doesn't target the specific element you're outputting on the current row, so that's one reason why that approach doesn't work.

how to select dropdown option using data coming from database?

how to pre-select option from dropdown on the base of data coming from database or model to view.
I also want to save this data if change in dropdown option.
I try this code
<select class="form-control appointment_status" >
<option <?php if ($row['status'] == 0) { ?> selected <?php } ?> value="0">In process</option>
<option <?php if ($row['status'] == 1) { ?> selected <?php } ?> value="1">Completed</option>
</select>
This is normally what I like to do(this goes intto your controller):
// Select Categories
$categories_options = array();
$categories_options[0] = 'Select Categories';
$categories_list = $this->Terms_model->get_list();
foreach($categories_list as $cat){
$categories_options[$cat->term_id] = $cat->title;
}
$data['categories_options'] = $categories_options;
Assuming that your passing a $data variable to your view, you will be able to display it like this:
<!-- Post Categories -->
<?php
$data = array(
'class' => 'form-control js-example-basic-multiple',
'value' => set_value('categories'),
'multiple' => 'multiple',
);
?>
<div class="form-group">
<?= form_label('Categories','categories'); ?>
<?= form_dropdown('categories[]', $categories_options, 0, $data); ?>
</div>
and then to edit it(a different view):
<?php
$options = array(
'' => 'Please select an option',
'0' => 'In process',
'1' => 'Completed',
);
$data = array('class' => 'form-control');
?>
<div class="form-group">
<?= form_label('Status', 'status'); ?>
<?= form_dropdown('status', $options, $item->status, $data); ?>
</div>
Obviously you will need to change more stuff but I'm sure it will give you a better idea and approach.
Try something like this:
$status = ($row['status']) ? 'selected' : '';
echo <<<END
<option value="0" $status>In process</option>
<option value="1" $status>Completed</option>
END;
Using class... most of dropdown components uses active class to definy the first element on dropdown, so just put it on which element you want and will do the trick

how change user account inactive as default (codeigniter)?

I have code CodeIgniter
I want help with the problem ,
"I want to set the account as inactive by default " it's active when a user creates new account.
<?php // status ?>
<div class="form-group col-sm-3<?php echo form_error('status') ? ' has-error' : ''; ?>">
<?php echo form_label(lang('users input status'), '', array('class'=>'control-label')); ?>
<span class="required">*</span>
<div>
<label style="font-weight:500">
<?php echo form_radio(array('class'=>'radio', 'type'=>'radio', 'name'=>'status', 'id'=>'radio-status-1', 'value'=>'1', 'checked'=>(( ! isset($user['status']) OR (isset($user['status']) && (int)$user['status'] == 1) OR $user['id'] == 1) ? 'checked' : FALSE))); ?>
<span><?php echo lang('admin input active'); ?></span>
</label>
</div>
<?php if ( ! $user['id'] OR $user['id'] > 1) : ?>
<div>
<label style="font-weight:500">
<?php echo form_radio(array('class'=>'radio', 'type'=>'radio', 'name'=>'status', 'id'=>'radio-status-2', 'value'=>'0', 'checked'=>((isset($user['status']) && (int)$user['status'] == 0) ? 'checked' : FALSE))); ?>
<span><?php echo lang('admin input inactive'); ?></span>
</label>
</div>
<?php endif; ?>
</div>
Let me give you some explanation on this:
If I understand you correctly you want to have every visitor as inactive, and (new) registered users as active? Which will become active when $user is set?
Am I correct, by reading your code, <?php if ( ! $user['id'] OR $user['id'] > 1) : ?> this part should show a line of code if it's a user? Currently you are saying two things:
The line of code needs to be shown if:
There is NO $user['id'] known
OR if $user['id'] is higher than value 1.
So, lets say:
Your code is showing to ALL users currently (both inactive and active).
You could fix it with something like this:
<?php if ( $user['id'] > 1) : ?>
<div>
<label style="font-weight:500">
<?php echo form_radio(array('class'=>'radio', 'type'=>'radio', 'name'=>'status', 'id'=>'radio-status-2', 'value'=>'0', 'checked'=>((isset($user['status']) && (int)$user['status'] == 0) ? 'checked' : FALSE))); ?>
<span><?php echo lang('admin input inactive'); ?></span>
</label>
</div>
<?php endif; ?>
By deleting your OR statement it will now ONLY show to active users. If you wish to show a line of code to inactive users you could simply do this:
<?php if ( $user['id'] > 1) : ?>
<div>
<label style="font-weight:500">
<?php echo form_radio(array('class'=>'radio', 'type'=>'radio', 'name'=>'status', 'id'=>'radio-status-2', 'value'=>'0', 'checked'=>((isset($user['status']) && (int)$user['status'] == 0) ? 'checked' : FALSE))); ?>
<span><?php echo lang('admin input inactive'); ?></span>
</label>
</div>
<?php else :?>
// Code which is showing when there is NO $user['id'] known or lower than 2.
<?php endif; ?>
I hope this helped you.

Compare and show different data from two tables

I've got two tables. Skills, that contains all the available skills.
and user_skills, that contains which skills user got.
I need to output all the available skills, and those skills that the user have (I select it by session['username']) will be marked (checked checkbox).
$get_all_skills = mysqli_query($conn, "SELECT * FROM skills");
$web_design = array();
$web_develop = array();
$automation = array();
$security = array();
while($show_row = mysqli_fetch_array($get_all_skills)){
switch($show_row['skill_type']){
case '1':
array_push($web_design, $show_row['skill_name']);
break;
case '4':
array_push($web_develop, $show_row['skill_name']);
break;
case '3':
array_push($automation, $show_row['skill_name']);
break;
case '2':
array_push($security, $show_row['skill_name']);
break;
}
}
How can possibly I make this work?
Part of the html is:
<div class="">
<ul class="to_do">
<?php
for($i=0;$i<count($web_develop);$i++){
?>
<li>
<p>
<input type="checkbox" class="flat"> <?php echo $web_develop[$i];?> </p>
</li>
<?php } ?>
</ul>
</div>
Considering you have a user_skills array ($user_skills_array) declared and have the data, use the below html code
<div class="">
<ul class="to_do">
<?php
for($i=0;$i<count($web_develop);$i++){
// check the global skill is available in the user skill set
$checked = (in_array($web_develop[$i], $user_skills_array)) ? 'checked="checked"': '';
?>
<li>
<p>
<input type="checkbox" class="flat" <?php echo $checked;?>> <?php echo $web_develop[$i];?> </p>
</li>
<?php } ?>
</ul>
</div>
try the following, explanation below.
<?php
$get_all_skills = mysqli_query($conn, "SELECT * FROM skills");
$availableSkills = [];
$skillTypes = [
1 => 'Webdesign',
2 => 'Security',
3 => 'Automation',
4 => 'Web Development',
];
while($show_row = mysqli_fetch_array($get_all_skills)){
$skill = [
'id' => $show_row['id'];
'name' => $show_row['name'];
];
$skillType = $show_row['skill_type'];
$availableSkills[$skillType][] = $skill;
}
// assuming user_skills:
$userSkills = [
1,
2,
5,
8
];
?>
<ul class="to_do">
<?php foreach ($availableSkills as $type => $skillsForType) : ?>
<li>
<?= $skillTypes[$type] ?>
<ul>
<?php foreach ($skillsForType as $skill) : ?>
<li>
<input
type="checkbox"
class="flat"
name="skill[<?= $type ?>]"
value="<?= $skill['id'] ?>"
<?= in_array($skill['id'], $userSkills) ? 'checked="checked"' : ''
> <?= $skill['name'] ?>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
First you gather all skills, as have you already done.
Next I optimized your different skills array into one and listed all skill types.
The available skills are indexed by their type id (see $skillTypes).
Next I assumed your user-skill collection could be like shown.
In the HTML section I am iterating over every skill type and in a nested foreach echo all corresponding skills.
If the skill id of the current iteration is in_array of the user-skill-relation the "checked" attribute is set.
To clarify I used short syntax in this example.
[] --> array()
<?= --> <?php echo
foreach () : endforeach;

php code to display icon on active button

I am working on cakephp 1.3..
i have button and value displaying inside button coming from database.
I want to show arrow icon on my active button only..
now the active arrow icon is displaying in all button...inside forloop.
plz help me to do this..
below is my code
<?php
foreach($modes as &$mo)
{
$temp = "Road Vs "; $mo = strtolower($mo);
?>
<li class="active">
<input name="data[Customer][mode]" class="railbtn" type="submit" id="mode" value="<?php echo $temp.$mo; ?>">
<span class="arrow">
<?php echo $this->Html->image('red_arrow.png', array('alt' => '')); ?>
</span>
</li>
<?php } ?>
You should add a condition in your for loop
Like this
<?php
// you need to send button unique name to controller
foreach($modes as &$mo)
{
$temp = "Road Vs "; $mo = strtolower($mo); ?>
<li class="<?php echo ($mo == $selectedUniqueButtonID) ? 'active' : '' ?>">
<input name="data[Customer][mode]" class="railbtn" type="submit" id="mode" value="<?php echo $temp.$mo; ?>">
<?php if($mo == $selectedUniqueButtonID){ ?>
<span class="arrow">
<?php echo $this->Html->image('red_arrow.png', array('alt' => '')); ?>
</span>
<?php } ?>
</li>
<?php
} ?>
Controller code
$selectedUniqueButtonID = $this->data['Customer']['mode'];
$this->set('selectedUniqueButtonID',$selectedUniqueButtonID);

Categories