i have a questions about managament rows in grid. For example we have food/cost table like:
Fruit | cost
------------
Apple | 10$
Bannan| 5$
We can create form like:
<form>
<input type='text' name='Fruits[0][fruit] value='Apple' /> <input type='text' name='Fruits[0][cost] value='10$' /> <br>
<input type='text' name='Fruits[1][fruit] value='Bannan' /> <input type='text' name='Fruits[1][cost] value='5$' />
</form>
And save all it like:
if ($is_new)
if(isset($_POST['Fruits'])) {
foreach ($_POST['Fruits'] as $fruit) {
$model = new Fruit();
$model->attributes = $fruit;
$model->save();
}
}
} else { //here code for update, $model->load()... }
All fine, we can update both rows... But that if i want add new? All fine method above make all good, but what if i delete 1 row? In my base i will have 2 rows, but i need only 1.
Here 2 ways i see:
1. Delete all fruits from databse on every update when POST count != databse count
2. We can load database rows, write loop, check every row... but its so hard, so many code...
How you manage new and deleted rows?
Use Yii data handlers to print results, like 'CGridView' :
http://www.yiiframework.com/doc/api/1.1/CGridView/
Related
I've multiple Select tags in HTML form i.e 'Gender', 'District' etc where the default selection is All. The eloquent query is dependent on these tags. I can retrieve data from DB if user select an option like Male or Female but don't know how to use `All'.
here is html code of Gender Select tag.
<div class="col-md-2">
<label for="gender">Gender</label>
<select wire:model="byGender" class="custom-select form-control" name="gender" id="gender">
<option selected>All</option>
<option value="male">Male</option>
<option value="female">Female</option>
<option value="combine">Combine</option>
</select>
</div>
and here is Laravel Code
public function mount()
{
$this->rows = DutySheet::where('gender', $this->byGender)->get()->toArray();
}
Note: this is Livewire function and byGender will be updated as the user select an option.
My question is what to use as value of All option in Select tag to retrieve all data from DB, "" is not working.
Your All options are represented by empty values (empty strings). You can leverage this by using the when() method on the query-builder, which effectively is an if-statement - the closure is only applied to the querybuilder if the first parameter is true.
$this->rows = DutySheet::when($this->byGender !== '', function($query) {
return $query->where('gender', $this->byGender)
})
->get()
->toArray();
You can chain these and add as many when() as you want to your query, to compute a more complex query with conditional conditionals.
You can add some logic to only apply the where condition when the select menu is set to any value other than "All".
If the user selects male, female or combine then the where condition will be run against that value, otherwise the query will return every record.
public function mount()
{
$dutySheets = DutySheet::query();
if ($this->byGender !== 'All') {
$dutySheets->where('gender', $this->byGender);
}
$this->rows = $dutySheets->get()->toArray();
}
I am trying to get default value selected (from database) in my select box using ng-options.
My view
<select class="form-control samlength modalinput"
ng-options="p.procid as p.procname for p in processes track by p.procid"
ng-model="p.procid">
<option value="">-- choose an option --</option>
</select>
where p.procid is a value received from the database.
My data
procid procname time
1 MyProcess 2018-05-30 13:34:54.097 3003162
3 Testing 2018-05-31 18:31:32.467 3003162
If selected procid is 3, how can I get it to be selected by default?
FYI - I have tried multiple answers given in other threads. I have also tried ng-init but nothing helped.
You can keep your HTML as:
<select class="form-control samlength modalinput"
ng-options="p.procid as p.procname for p in processes track by p.procid"
ng-model="selectedProcess">
<option value="">-- choose an option --</option>
</select>
Now if we have a requirement to select a particular object in the array. We can do that by iterating through the array and comparing value at the given key:
function functiontofindIndexByKeyValue(arraytosearch, key, valuetosearch) {
for (var i = 0; i < arraytosearch.length; i++) {
if (arraytosearch[i][key] == valuetosearch) {
return i;
}
}
return null;
}
Call this function as:
var index = functiontofindIndexByKeyValue($scope.processes, "procid", procid);
$scope.selectedProcess = $scope.processes[index];
alert(index);
Hope it works!
Update your html code to this:
<select ng-model="processSelected"
ng-options="p.procname for p in processes track by p.procid">
</select>
Then in controller initialise your model value as:
$scope.processSelected = $scope.processes[1];
Where $scope.processes is an array of process objects.
Plunker Example
I'm having trouble with checkboxes on my site.
Here's the code I have so far:
#{
Layout = "~/_SiteLayout.cshtml";
WebSecurity.RequireAuthenticatedUser();
var db = Database.Open("StayInFlorida");
var rPropertyId = Request.QueryString["PropertyID"];
var Propertyinfo = "SELECT * FROM PropertyInfo WHERE PropertyID=#0";
var qPropertyinfo = db.QuerySingle(Propertyinfo, rPropertyId);
if(IsPost){
var sql = "INSERT INTO PropertyInfo (FePool, FeJac, FeGames) VALUES (#0, #1, #2) WHERE PropertyID=#3";
db.Execute(sql, Request["check1"], Request["check2"], Request["check3"], rPropertyId);
}
}
<form method="post">
<div>
<input type="checkbox" name="check1" value="true" />
Value 1
<br/>
<input type="checkbox" name="check2" value="check2" />
Value 2
<br/>
<input type="checkbox" name="check3" value="check3" />
Value 3
<br/>
<div>
<input type="submit" name="buttonSubmit" value="Submit" />
</div>
</form>
What I need to be able to do is check 3 columns in my database, and see if they are set to true (the columns are set as "bit"). If they are, then I want the checkbox to be shown as checked. I also want to be able to toggle these checkboxes and post "true" or "false" using a form, to post into the database.
You have a WHERE clause in your INSERT statement which makes no sense. If you are inserting, you create a new row. I suspect you intend to UPDATE an existing row in which case your SQL should be:
"UPDATE PropertyInfo SET FePool = #0, FeJac= #1, FeGames = #2 WHERE PropertyID=#3"
If a checkbox is not checked, nothing will be included for that checkbox in the Request.Form collection. If it is checked, the value passed is "on" by default, or whatever you have specified in the "value" attribute. For check1, that will be "true". For check2, that will be "check2". You need to establish whether the checkbox was actually included in the Request.Form collection and then set the values you want to commit to the database accordingly. If you want to pass true or false depending on whether the checkbox was checked, you can do this:
var c1 = !Request["check1"].IsEmpty();
var c2 = !Request["check2"].IsEmpty();
var c3 = !Request["check3"].IsEmpty();
var sql = "UPDATE PropertyInfo SET FePool = #0, FeJac= #1, FeGames = #2 WHERE PropertyID=#3";
db.Execute(sql, c1,c2,c3,rPropertyInfo);
Now that you have a series of booleans, you can use those to determine whether the checkbox is checked or not by using Conditional Attributes:
<input type="checkbox" name="check1" checked="#c1" /> Value 1<br/>
<input type="checkbox" name="check2" checked="#c2" /> Value 2<br/>
<input type="checkbox" name="check3" checked="#c3" /> Value 3<br/>
If you want to preset the values of the checkboxes from a query, you do that prior to the IsPost block:
var rPropertyId = Request.QueryString["PropertyID"];
var Propertyinfo = "SELECT * FROM PropertyInfo WHERE PropertyID=#0";
var qPropertyinfo = db.QuerySingle(Propertyinfo, rPropertyId);
bool c1 = qPropertyinfo.FePool;
bool c2 = qPropertyinfo.FeJac;
bool c3 = qPropertyinfo.FeGames;
Obviously you don't need var in front of the c1 - c3 variables in the IsPost block now.
How would I use this in a query with multiple selections it would have to be something like
SELECT * FROM venue_event where venue = $check or $check ??
Form
<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check;
}
}
?>
I don't know php well enough to post that part, but your SQL query should end up looking like:
SELECT *
FROM venue_event
WHERE venue IN ('venue1','venue2');
Or you can use OR:
SELECT *
FROM venue_event
WHERE venue = 'venue1' OR
venue = 'venue2';
I'm creating a form elements template file in PHPTAL. I would like to be able to OPTIONALLY pass in an id attribute for a field...
So far the code looks like this:
<xml>
<tal:block metal:define-macro="text">
<label tal:condition="php: !isset(hideLabel) || isset(hideLabel) && !hideLabel">${field/label}</label>
<input name="${name}" type="text" value="${field/value}" />
<p tal:condition="exists:field/error">${field/error}</p>
</tal:block>
</xml>
This works as advertised. What I'd like to add is something, like
<input name="${name}" tal:attributes="id exists: id $id | $name" value="${field/value}" />
to allow me to optionally pass in an id from the METAL call...
Should I be doing it differently? I've tried using PHP: isset(id) ? $id : NULL and variations thereof, but just end up with an id="0" in the resultant HTML.
Any ideas?
In case anyone else needs it, one working answer is:
<xml>
<tal:block metal:define-macro="text">
<label tal:condition="not: exists:hideLabel">${field/label}</label>
<input name="${name}" tal:attributes="id id | nothing" type="text" value="${field/value}" />
<p tal:condition="exists:field/error">${field/error}</p>
</tal:block>
</xml>
Where passed in variables are id, name, an array named field, and hideLabel .
Note, that I've also managed to simplify the label test to something which I believe is more idiomatically TAL.
Set VAR at a DIV containing the soon to be used element:
div class="" tal:define="VAR context.property"
div class="" tal:attributes="class python:'grid_8 omega' if VAR else 'grid_8 alpha'"
in PHP:
<div id="contentCenter" tal:attributes="id
php:isset(variable)&&isset(variable.property)?'IDVALUE':NULL">