how to get checkbox value as checked from database in php? - checkbox

in database values are stored as "A,B,C,D,E,F" etc. now i have to fetch those data in the form for update. i have to check the checkbox if it matches the value with database value.
$rs=sql("select sectionName from sections where sectionName != ' ' order by sectionName");
$cat_options="";
foreach($rs as $d){
$option=$d['sectionName'];
$cat_options.= "<label><input type='checkbox' name='category[]' value='$option'>$option</label><br/>";
}
It retrieve list of category from one table.
$a = explode(",", $category);
$a is array of name of category that should be checked in list of category.
I already knew that there are lots off topics on net related to my topics. I tried them but no luck.

$query="SELECT sectionName from sections";
$result = #mysql_query ($query);
while($drop=#mysql_fetch_array($result))
{
echo "<input type='checkbox' name='category[]' value=$drop[sectionName]>$drop[sectionName]";
}

Related

Wordpress database query results not displaying

So, I'm creating a Wordpress plugin which, when activated, creates a new table in the database and then stores entries from a form into that table.
The form submission is working perfectly and entries are successfully storing in the table.
Now I'm trying to create a shortcode which will retrieve a list of all the entries from the table and display them in a dropdown menu so that the use can select which entry to view.
The query to retrieve the data from the table appears to be working, but the "foreach" loop is not populating the dropdown list.
Here is a screenshot of the table contents:
And here is the code for the shortcode:
function cpe_history_select(){
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
global $wpdb;
$history_tbl = $wpdb->prefix . "cpe_history";
$history_query = "SELECT id,submitDate FROM $history_tbl WHERE userID=$user_id ORDER BY submitDate DESC";
$cpe_history = $wpdb->get_results ( $history_query );
if(!empty($cpe_history)){
$output = '<form action="" method="post">';
$output .= '<select id="cpe_history" name="cpe_history">';
$output .= '<option value="">Select</option>';
foreach($cpe_history as $cpe_entry){
$entry_id = $cpe_entry->id;
$entry_date = $cpe_entry->submitDate;
$ouput .= '<option value="' . $entry_id . '">' . $entry_date . '</option>';
}
$output .= '</select>';
$output .= '</form>';
}
return $output;
}
add_shortcode('cpe-history','cpe_history_select');
So, as you can see, I am trying to get just the "id" and "submitDate" fields from the table and display the list of Submit Dates as options in the dropdown list, with the entry's "ID" as the value of the option.
I am logged in as the user with user_id = '1', so there is definitely entries to display, but I'm only getting the initial "Select" option and nothing else.
http://sandbox.graphicdetail.co.nz/cpe-test/
Where have I gone wrong?
PS - Weird thing is, I've created plugins before making similar queries and looping through the results and have not had this sort of issue before. :-/
OMG! I found the problem and I can't believe it was something so simple and obvious!
It was a simple typo!
On the line:
$ouput .= '<option value="' . $entry_id . '">' . $entry_date . '</option>';
I had simply left the first "t" out of "$output"! DUH!

Cakephp Update multiple records or create new one if id doesn't exist in database

hello I have an edit form which displays the input fields.I am displaying the data from database in those fields so user can edit them. And also there is a button which ask to add new field. I am able to edit multiple fields but the problem is how can I create a new field in database if there is no existing id of the record.
Note: For the new field I am sending 0 id so that I can check in cakephp that it has a new field.
foreach ($exp as $k => $v) {
$dat[$k]["prp_id"] = $prpid;
$dat[$k]["exp_company"] = $v['company'];
$dat[$k]["position"] = $v['position'];
$dat[$k]["exp_id"] = $v['exp_id'];
}
$this->Experience->saveAll($dat, array('conditions' => array('exp_id' => $v['exp_id'])));
This is updating all the fields. Btw I am also not sure why its working correctly as I am here $v['exp_id'] sending only one value because its out of the loop but it is working perfectly meaning I can be able to multiple fields which I am not sure how. So In the end I have two problems. One is to create a new record if exp_id is 0 and second is why my this code works good for updating records.
Try this:
foreach ($exp as $k => $v) {
$dat[$k]["prp_id"] = $prpid;
$dat[$k]["exp_company"] = $v['company'];
$dat[$k]["position"] = $v['position'];
if ($v['exp_id'] != 0) {
$dat[$k]["exp_id"] = $v['exp_id'];
}
}
$this->Experience->saveAll($dat);

ng-select: get selected object, preselect by Id

I have a list of users (array of objects)
I need the selected user in $scope.owner for use elsewhere.
I need to pre-select a user by Id and have $scope.owner update with correct object values (e.g. user.Email, user.Address).
$scope.item.UserId needs to change based on the selected user.
This is what I have so far:
<select
ng-model="owner"
ng-change="item.UserId=owner.Id"
ng-options="user as user.Company + ' - ' + user.FirstName + ' ' + user.LastName for user in users track by user.Id">
</select>
Currently 1. and 3. work, but when I pre select using:
$scope.owner = { Id: 1 };
Then I obviously don't all the values from users such as user.Email.
Anyone got ideas or advice?
Thanks, Lex
If you use $scope.owner = {Id:1} even it has same Id it is different instance then one created when iterating in ng-options.
When you get your list of users why not just set
$scope.owner = $scope.users[0]
// or if you need to get by id just use
$scope.owner = $scope.users.filter(...)[0]
Here is sample plunker.
http://plnkr.co/edit/5VZbdmBmtaDpkeLYQwft?p=preview

Fetching values of a particular field from an array to a variable in cakephp

In cakephp, I executed this query to find the id field value of the corresponding username
$count = $this->User->find('all',array('conditions'=>array('User.username' => $n)))
How I will get take the value of a field from this array result to a variable?
I am new to cakephp and any help will be very useful.
Well, after calling find, you will notice that $count will be populated if something was brought from the database. I would change something, though, I would use "first" instead of "all" because you are finding only one record.
You can either use this
//in your controller
$count = $this->User->find('first',
array('conditions'=>array('User.username' => $n)));
//and set the variable to be used in the view in this way
$this->set('yourId', $count['User']['id']);
Then in your view
echo $yourId;
Or, you can also do this
$yourId = $this->User->field('id', array('User.username' => $n));
$this->set(compact('yourId'));
and then in your view
echo $yourId

how can I filter data cumulatively in CakePHP

I'm relatively new to CakePHP and object oriented programming. I have built apps in CakePHP before, but never to this extent, hence the reason I'm asking for help.
I have a large product management database, the user needs to be able to set filter criteria to find the closest product matches.
The products table contains category_id, manufacturer_id, type_id, status_id related to the respective tables.
I have a sidebar with dropdown boxes for each filter, and want all the filters to work together, for instance, if a manufacturer and product type is selected, display those filtered results, and change the status and category dropdowns to only contain options from the already filtered results.
In the past I have accomplished this by using a dynamic query setting conditions based on user input, appending where clauses each time a filter was applied.
so far I've only been able to get individually filtered results using
$this->Product->recursive = 0;
$mid = (isset($this->params['named']['mid'])) ? $this->params['named']['mid'] : '';
if(!empty($mid)){
$this->paginate = array(
'conditions' => array('Product.manufacturer_id' => $mid),
'limit' => 10
);
}
$products = $this->paginate('Product');
$this->set(compact('products'));
where 'mid' is the manufacturer id, and this is manually entering 'mid' in the url
How do I get cumulatively filter results if the user sets category_id, manufacturer_id, type_id, status_id or any combination? And what queries would I use to repopulate the dropdowns?
(Eventually I will use ajax, but for now assume I have 4 dropdown boxes with a submit button)
The long way would be:
var $conditions = array();
if (isset($this->params['named']['mid']) && !empty($this->params['named']['mid'])){
$conditions['Product.manufacturer_id'] = $this->params['named']['mid']);
}
if (isset($this->params['named']['catid']) && !empty($this->params['named']['catid'])){
$conditions['Product.category_id'] = $this->params['named']['catid']);
}
if (isset($this->params['named']['statid']) && !empty($this->params['named']['statid'])){
$conditions['Product.status_id'] = $this->params['named']['statid']);
}
$this->paginate['conditions'] = $conditions;
$this->paginate['limit'] = '10';
$this->set('products', $this->paginate('Product'));
Once you get the hang of that, you could put all your checks into a loop.

Resources