Pardon me if this question has already been answered on this site. I haven't been able to find it through my research thus far.
Question:
As I step through each row of formatted table , I'm trying to determine if a column (CompletionDate) is missing it's value
Below is an example table:
DueDate, StartDate, CompletionDate
2017-06-10T22:00:29.08, 2017-05-30T20:38:37.913, 2017-05-30T20:44:05.517
2017-06-09T16:00:21.38, 2017-06-07T15:55:14.627,
Below is some of my code thus far:
foreach ($row in $tableData) {
if ($row.CompletionDate -eq ''){
write-host 'no value'
}
else {
Write-Host 'has value'
}
}
Thanks in advance for your help. If this question has been answered before, please just let me know where it is, and I'll take this question down.
}
You can use the IsNullOrEmpty method:
foreach ($row in $tableData) {
if ([string]::IsNullOrEmpty($row.CompletionDate)){
write-host 'no value'
}
else {
Write-Host 'has value'
}
}
try this
foreach ($row in $tableData)
{
if ($row.CompletionDate)
{
'has value'
}
else
{
'no value'
}
}
Related
I am fetching data from the database and the result is multiple rows but it is only showing 1st row in echo. please let me know where is the problem so that I can get all matched results.
public function review_email()
{
$date= date("Y-m-d");
$this->load->model("site_model");
$query = $this->db->get_where('review_email', array("date"=>$date));
$row = $query->row();
if (isset($row))
{
echo $name=$row->name;
}
}
Try This --
public function review_email()
{
$date= date("Y-m-d");
$this->load->model("site_model");
$query = $this->db->get_where('review_email', array("date"=>$date));
//$row = $query->row();
$query->result();
$row = $query->row_array();
foreach ($row as $c)
{
echo $c->name;
}
}
Your code $query->row(); means one row as an object, you also have $query->result(); that will give you all the results as an object too, then $query->row_array(); will give you one as an array, finally you have $query->result_array(); which will give you all the results as an array.
I have a model, for example SomeForm. There is a field. This field contains an array of selected options. Is it possible to write a rule for this field to check how many items were selected? I need to write a condition, that user has to check minimum 2 options and maximum 5.
I tried something like this, but it doesn't work (the form can be submitted if at least one option is selected)
public function rules()
{
return [
[['ingredients'], 'required'],
['ingredients', 'checkIsArray']
];
}
public function checkIsArray($attribute, $params)
{
if (empty($this->ingredients)) {
$this->addError($attribute, "config cannot be empty");
}
elseif (count($this->ingredients)>5) {
$this->addError($attribute, "Choose more");
}
elseif (count($params)<2) {
$this->addError($attribute, "Choose less");
}
}
Yes you can, but you have a wrong assignment of variable i.e $params in the last condition elseif (count($params)<2) you are counting $params instead of the $this->ingredients array. And you do not need to have the first check, adding the attribute to the required rule is enough to check if it is empty when submitted.
change the validation function to
public function checkIsArray( $attribute , $params ) {
if ( count ( $this->ingredients ) > 5 ) {
$this->addError ( $attribute , "Choose No more than 5" );
} elseif ( count ( $this->ingredients ) < 2 ) {
$this->addError ( $attribute , "Choose No less than 2" );
}
}
I just tested it before posting with the kartik\Select2 multiselect and it works just fine, but if it still reacts the same you need to add the controller/action code hope it helps you out.
I am trying to parse values from an XML file that will add items to a collection using a foreach loop, then add the items from that collection to another collection using another foreach loop with an addition value. This is what I am doing so far:
[xml]$testResults = Get-Content -Path $testResultsPath
$resultsByName = #{}
$resultsByPhone = #{}
$loop = 0
foreach($testCase in $testResults.'test-results'.'test-suite')
{
foreach($testCase in $testResults.'test-results'.'test-suite'[$loop].'results'.'test-suite'.'results'.'test-suite'.'results'.
'test-suite'.'results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite'.'results'.'test-case')
{
$NameWithPone = $testCase.name.ToUpper().Substring($testCase.name.LastIndexOf('.')+1);
$Name =$NameWithPone.Substring(0, $NameWithPone.IndexOf('_'));
$PhoneVersion = $testCase.name.Substring($testCase.name.IndexOf('_')+1);
$resultsByName.Add($PhoneVersion, $Name)
Foreach($resultCase in $resultsByName)
{
$resultsByPhone.Add($resultsByName, $testCase.result)
}
}
$loop++
}
But this will only add it first result, then give the error "Item has already been added. Key in dictionary:
'System.Collections.Hashtable' Key being added: 'System.Collections.Hashtable'" I think this is because I am adding the same item each time, how can I correct this?
The first collection will look like this:
google_pixel_xl-7_1_1 TESTTHATAREGISTEREDUSERCANLOGINTOTHECUSTOMERAPPSUCCESSFULLY
htc_10-6_0_1 TESTTHATAREGISTEREDUSERCANLOGINTOTHECUSTOMERAPPSUCCESSFULLY
oneplus_one-4_4_4 TESTTHATAREGISTEREDUSERCANLOGINTOTHECUSTOMERAPPSUCCESSFULLY
But I want to add both values together to another collection which would look like:
google_pixel_xl-7_1_1 TESTTHATAREGISTEREDUSERCANLOGINTOTHECUSTOMERAPPSUCCESSFULLY Error
I got this done by doing this:
[xml]$testResults = Get-Content -Path $testResultsPath
foreach($testCase in $testResults.'test-results'.'test-suite')
{
function Get-TestCases($myResults)
{
$testCases = #()
foreach($child in $myResults.ChildNodes)
{
if($child.'test-case' -eq $null)
{
foreach($testCase in Get-TestCases $child)
{
$testCases += $testCase
}
}
else
{
$testCases += $child.'test-case'
}
}
return $testCases
}
$tests = Get-TestCases $testResults.'test-results'.'test-suite'[$loop]
foreach($test in $tests)
{
$PhoneVersion = $test.name.Substring($test.name.IndexOf('_')+1);
$resultsByPhone.Add($PhoneVersion, #{})
$NameWithPhone = $test.name.ToUpper().Substring($test.name.LastIndexOf('.')+1);
$Name =$NameWithPhone.Substring(0, $NameWithPhone.IndexOf('_'));
$resultsByPhone[$phoneVersion].Add($Name, $test.result)
}
$resultsByPhone[$phoneVersion]
$resultsByPhone
$loop++
}
In the official documentation, the following columns are mentioned :
parent_id
lft
rgt
depth
I haven't found any explanation of their types in the documentation. Could someone help me and tell me what they are ?
I also want to know if they are all mandatory if I only want to reorder a list of items (I don't need any nesting).
Edit: As this question is quite popular, I've updated the documentation with the correct info.
The reordering id columns should be integer or INT(10) if you're not using a migration.
Unfortunately they're all mandatory, yes. But if you're on a very strict DB schema, you could eliminate all of them except the "lft" column by adding this method to your EntityCrudController (basically overwriting the one in Backpack\CRUD\app\Http\Controllers\CrudFeatures\Reorder):
public function saveReorder()
{
$this->crud->hasAccessOrFail('reorder');
$all_entries = \Request::input('tree');
if (count($all_entries)) {
$count = 0;
foreach ($all_entries as $key => $entry) {
if ($entry['item_id'] != '' && $entry['item_id'] != null) {
$item = $this->crud->model->find($entry['item_id']);
$item->lft = empty($entry['left']) ? null : $entry['left'];
$item->save();
$count++;
}
}
} else {
return false;
}
return 'success for '.$count.' items';
}
I have a quick powershell syntax question. I have following loop in powershell. I would like to know which value exists (which is easy). I can use either "Read" or "Visitor" to assign permission. I can do 2 if statements but if there is a smarter way i would rather use that.
if(($listRA.Member.Name) -ne $authUsers)
{
foreach($spRoleDefinition in $rc)
{
if(($spRoleDefinition.Name -eq "Read") -OR ($spRoleDefinition.Name -eq "SP Visitor"))
{
Need to determine which one exists
Adding a user code....
write-host $spRoleDefinition.Name
}
}
}
Use a switch statement:
if(($listRA.Member.Name) -ne $authUsers)
{
foreach($spRoleDefinition in $rc)
{
switch($spRoleDefinition.Name)
{
'Read' {
# do Read stuff
break;
}
'SP Visitor' {
# do SP Visitor stuff
break;
}
}
}
}