This topic is locked

How can I pick the only option in a lookup field automatically

9/18/2016 9:03:41 PM
PHPRunner General questions
S
Stucco author

If I have a lookup table field on my add/edit page, and there is only one option available, I would like to it to be selected by default when the user visits the page. I expect this will have to be JavaScript OnPageLoad event.
I can get the control with Runner.getControl, but am not sure how to check how many options there are.
Thanks!

romaldus 9/19/2016

for example if you lookup from other table:
table1 is current add/edit page.

Fields (id, lookup, etc.....)
table2 is lookup table

Fields (id, lookup, etc.....)
on table1,

  1. setup your "lookup" field to lookup from table 2

    [size="2"]2. on add/edit page [/size]**



//modify the sql query to suit your needs

global $conn;

$sql = "SELECT lookup as my_lookup FROM table2";

$result = mysqli_query($conn, $sql);
//check if query returns only one row for lookup

if (mysqli_num_rows($result) == 1)

{

while($row = mysqli_fetch_assoc($result))

{

//save row count in session variable

$_SESSION["row_count"] = mysqli_num_rows($result);

//save query result in session variable

$_SESSION["lookup"] = $row["my_lookup"];
$pageObject->setProxyValue("row_count", $_SESSION["row_count"]);

$pageObject->setProxyValue("my_lookup", $_SESSION["lookup"]);

$pageObject->setProxyValue("values", $pageObject->getCurrentRecord());

}

}


on table1, add/edit page [color="#ff0000"][b]javascript onload** event:


if (proxy.row_count='1')

{

var ctrl = Runner.getControl(pageid, 'lookup');

ctrl.setValue(proxy.my_lookup);

}