This topic is locked

How to update selected records with value from dropdown box

11/19/2013 6:45:49 PM
PHPRunner Tips and Tricks
Sergey Kornilov admin

This is a little more advanced version of "Update selected records" tutorial PHPRunner manual.
Lets say we have a list of orders and want to be able to update several orders at once selecting status from dropdown list on the same page. Statuses are stored in another table.


  1. To display a dropdown box with list of statuses proceed to the List page in Visual Editor and insert the Code Snippet where you need this dropdpown box to appear.

$str= "<select id='mycontrol'><option value=''>Select status</option>";

//select values from database

global $conn;

$strSQL = "select status from statuses";

$rs = db_query($strSQL,$conn);

while ($data = db_fetch_array($rs))

$str.="<option value='".$data["status"]."'>". $data["status"]."</option>";

$str.="</select>";

echo $str;



Make sure to amend the SQL query (select status from statuses) and field name (status).
2. Add a button next to this code snippet. Name it 'Update selected'.
Add the following to ClientBefore event:

// make sure user selected some value from dropdown box

if ($("#mycontrol").val()=="") {

alert('Choose something already');

return false;

}

// send the selected value to OnServer event

params['status']=$("#mycontrol").val();


Add the following code to OnServer part:

global $dal;

while ( $data = $button->getNextSelectedRecord() ) {

$sql = "Update Orders set Status='".$params['status']."' where ID=".$data["ID"];

CustomQuery($sql);

}


Amend OnServer code changing table name (Orders) and field names (Status and ID). Leave parameter name ($params['status']) as is.
To refresh the page after updating selected records add the following code to ClientAfter event:

location.reload();


Enjoy!

J
jackheitzer@gmail.com 3/25/2014

Hi,
I've tried this trick and it works great, however...
I use this trick to insert a name in a table. In a few occassions the name to insert is a double name; let's say "Billy Bob".In the table only the first portion of the name is inserted (Billy), the second part (Bob) is omitted.
Is there a way to insert the full double name,
Thanks,
All the best,
Jack



This is a little more advanced version of "Update selected records" tutorial PHPRunner manual.
Lets say we have a list of orders and want to be able to update several orders at once selecting status from dropdown list on the same page. Statuses are stored in another table.

  1. To display a dropdown box with list of statuses proceed to the List page in Visual Editor and insert the Code Snippet where you need this dropdpown box to appear.

$str= "<select id='mycontrol'><option value=''>Select status</option>";

//select values from database

global $conn;

$strSQL = "select status from statuses";

$rs = db_query($strSQL,$conn);

while ($data = db_fetch_array($rs))

$str.="<option value='".$data["status"]."'>". $data["status"]."</option>";

$str.="</select>";

echo $str;



Make sure to amend the SQL query (select status from statuses) and field name (status).
2. Add a button next to this code snippet. Name it 'Update selected'.
Add the following to ClientBefore event:

// make sure user selected some value from dropdown box

if ($("#mycontrol").val()=="") {

alert('Choose something already');

return false;

}

// send the selected value to OnServer event

params['status']=$("#mycontrol").val();


Add the following code to OnServer part:

global $dal;

while ( $data = $button->getNextSelectedRecord() ) {

$sql = "Update Orders set Status='".$params['status']."' where ID=".$data["ID"];

CustomQuery($sql);

}


Amend OnServer code changing table name (Orders) and field names (Status and ID). Leave parameter name ($params['status']) as is.
To refresh the page after updating selected records add the following code to ClientAfter event:

location.reload();


Enjoy!