Hi, I was wondering if anyone can explain in somewhat plain english or show an example on how to setup coding where if a user checks off multiple rows, they can commit a mass update of a specific field to certain text - like for example "change status column to NO"
Thanks
Bulk action help - changing multiple row fields
-
- Veteran Member
- Posts: 45
- Joined: 2015-04-15 06:33
- Location: Hyderabad
- Contact:
Re: Bulk action help - changing multiple row fields
Hi chark,
Can you elaborate it.or PM me
Can you elaborate it.or PM me
Re: Bulk action help - changing multiple row fields
To better explain what I need, I have a table with multiple rows of data. One of the column's contains either two forms of text: 'Playable' or 'Not-Playable'. I was wondering if by having an administrator check off certain rows, a user can then press a button that will automatically change all those selected rows to say "Playable' and an additional button that would change it to read 'Not Playable'. This would help so administrators dont have to go row by row and update each one manually. Honestly I tried reading about batch actions but I couldnt figure it out.
Re: Bulk action help - changing multiple row fields
Assume the table name is "files"
1. In the hook folder, add the following code to the batch action function
By now, you should see two additional batch actions added to the drop down list.
2. Create a new javascript file in the hook folder with the following codes, and name it in this format tablename-tv.js (i.e. files-tv.js in this example)
3. Now, we code the server side php script namely change-status.php. Note this file should reside in the project folder that contains defaultLang.php, language.php and lib.php. Do not place it in Hooks folder.
Hope this help.
Weoi
1. In the hook folder, add the following code to the batch action function
Code: Select all
function files_batch_actions(&$args){
return array(
array(
'title' => 'Playable',
'function' => 'change_to_playable',
'icon' => 'ok'
),
array(
'title' => 'Not Playable',
'function' => 'change_to_notplayable',
'icon' => 'remove'
),
);
}
2. Create a new javascript file in the hook folder with the following codes, and name it in this format tablename-tv.js (i.e. files-tv.js in this example)
Code: Select all
function change_to_playable(table_name, ids){
/*
we'll open the change status page in a new window
that page is a server-side PHP script named change-status.php
but first, let's prepare the parameters to send to that script
*/
var url = 'change-status.php?table=' + table_name + '&status=playable';
for(var i = 0; i < ids.length; i++){
url = url + '&'
+ encodeURI('ids[]') + '='
+ encodeURIComponent(ids[i]);
}
window.open(url);
}
function change_to_notplayable(table_name, ids){
var url = 'change-status.php?table=' + table_name + '&status=notplayable';
for(var i = 0; i < ids.length; i++){
url = url + '&'
+ encodeURI('ids[]') + '='
+ encodeURIComponent(ids[i]);
}
window.open(url);
}
3. Now, we code the server side php script namely change-status.php. Note this file should reside in the project folder that contains defaultLang.php, language.php and lib.php. Do not place it in Hooks folder.
Code: Select all
<?php
/*
Including the following files allows us to use many shortcut
functions provided by AppGini. Here, we'll be using the
following functions:
makeSafe()
protect against malicious SQL injection attacks
sql()
connect to the database and execute a SQL query
db_fetch_assoc()
same as PHP built-in mysqli_fetch_assoc() function
*/
$curr_dir = dirname(__FILE__);
include("{$curr_dir}/defaultLang.php");
include("{$curr_dir}/language.php");
include("{$curr_dir}/lib.php");
/* receive calling parameters */
$table = $_REQUEST['table'];
$status = $_REQUEST['status'];
$ids = $_REQUEST['ids']; /* this is an array of IDs */
/* a comma-separated list of IDs to use in the query */
$cs_ids = '';
foreach($ids as $id){
$cs_ids .= "'" . makeSafe($id) . "',";
}
$cs_ids = substr($cs_ids, 0, -1); /* remove last comma */
/* update records and display a message. Assume ID is primary key, and status is field to be updated in files table*/
sql("UPDATE files SET status ='$status' WHERE id in ({$cs_ids}) ", $eo);
?>
<b>Selected records updated to <?php echo $status; ?></b><br>
<?php
Weoi
Re: Bulk action help - changing multiple row fields
Hi Weoi,
Thank you for the help! I feel like I am closer to get this accomplished. The column that contains the data is called 'Status'. Do you know where within the coding you sent I put that in?
Thanks
-Robert
Thank you for the help! I feel like I am closer to get this accomplished. The column that contains the data is called 'Status'. Do you know where within the coding you sent I put that in?
Thanks
-Robert
Re: Bulk action help - changing multiple row fields
Here is my test page: http://ucnj2.org/fieldtest/field_view.php?#
Re: Bulk action help - changing multiple row fields
Nevermind, I figured it out. This works great! Thank you very very much
Re: Bulk action help - changing multiple row fields
I notice the hook appears whether or not someone is logged in, anyway to hide it if they are not logged in?
Re: Bulk action help - changing multiple row fields
update change-status.php with the following code:
Reference:
http://bigprof.com/appgini/help/advance ... cess-pages
Weoi
Code: Select all
<?php
/*
Including the following files allows us to use many shortcut
functions provided by AppGini. Here, we'll be using the
following functions:
makeSafe()
protect against malicious SQL injection attacks
sql()
connect to the database and execute a SQL query
db_fetch_assoc()
same as PHP built-in mysqli_fetch_assoc() function
*/
$curr_dir = dirname(__FILE__);
include("{$curr_dir}/defaultLang.php");
include("{$curr_dir}/language.php");
include("{$curr_dir}/lib.php");
include_once("$currDir/header.php");
/* grant access to all logged users */
$mi = getMemberInfo();
if(!$mi['username'] || $mi['username'] == 'guest'){
echo "Access denied";
exit;
}
/* receive calling parameters */
$table = $_REQUEST['table'];
$status = $_REQUEST['status'];
$ids = $_REQUEST['ids']; /* this is an array of IDs */
/* a comma-separated list of IDs to use in the query */
$cs_ids = '';
foreach($ids as $id){
$cs_ids .= "'" . makeSafe($id) . "',";
}
$cs_ids = substr($cs_ids, 0, -1); /* remove last comma */
/* update records and display a message. Assume ID is primary key, and status is field to be updated in files table*/
sql("UPDATE files SET status ='$status' WHERE id in ({$cs_ids}) ", $eo);
echo "Selected records updated to ". $status."!";
include_once("$currDir/footer.php");
?>
Reference:
http://bigprof.com/appgini/help/advance ... cess-pages
Weoi