Bulk action help - changing multiple row fields

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
chark908
Posts: 16
Joined: 2015-04-15 17:28

Bulk action help - changing multiple row fields

Post by chark908 » 2015-04-22 00:39

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

Satya Kavala
Veteran Member
Posts: 45
Joined: 2015-04-15 06:33
Location: Hyderabad
Contact:

Re: Bulk action help - changing multiple row fields

Post by Satya Kavala » 2015-04-23 04:46

Hi chark,
Can you elaborate it.or PM me
Thanks&Regards
Satya Kavala
[email protected]

chark908
Posts: 16
Joined: 2015-04-15 17:28

Re: Bulk action help - changing multiple row fields

Post by chark908 » 2015-04-23 15:59

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.

wplim
Veteran Member
Posts: 36
Joined: 2013-01-17 22:42

Re: Bulk action help - changing multiple row fields

Post by wplim » 2015-04-24 05:53

Assume the table name is "files"
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'
			),

		);
	}
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)

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
Hope this help.

Weoi

chark908
Posts: 16
Joined: 2015-04-15 17:28

Re: Bulk action help - changing multiple row fields

Post by chark908 » 2015-04-24 13:47

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


chark908
Posts: 16
Joined: 2015-04-15 17:28

Re: Bulk action help - changing multiple row fields

Post by chark908 » 2015-04-24 14:03

Nevermind, I figured it out. This works great! Thank you very very much

chark908
Posts: 16
Joined: 2015-04-15 17:28

Re: Bulk action help - changing multiple row fields

Post by chark908 » 2015-04-24 14:23

I notice the hook appears whether or not someone is logged in, anyway to hide it if they are not logged in?

wplim
Veteran Member
Posts: 36
Joined: 2013-01-17 22:42

Re: Bulk action help - changing multiple row fields

Post by wplim » 2015-04-24 23:54

update change-status.php with the following code:

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

Post Reply