This topic is locked

Custom Button based on a value

1/20/2020 1:42:08 PM
PHPRunner General questions
M
mhollibush author

I am going beyond my scope on this ( nothing new here )
I want a custom button on the view page that redirects to another page only if a value from another table is set to 1
I currently have it working as a snippet with a link, but would like this to be a custom button.



global $pageObject;

$data = $pageObject->getCurrentRecord();

$sql ="SELECT * from studs where stud =". $data["id"] ."";

$rsstud = CustomQuery($sql);

while($data1=db_fetch_array($rsstud))

if ($data1["active"] == 1) {

echo "<font color ='yellow'><b>OPEN FOR STUD</b></font>
<a href='studs_view.php?editid1=". $data1["studid"] ."'>View Details</a>";

}

else {
}


any pointers on how I can accomplish this would be greatly appreciated....

A
acpan 1/20/2020

Check the online manual that will do that for you:
Online Manual Reference: Custom Button and Redirect
Here's what you could do :
Insert the custom button to your view page first.
In button's Server Event:



$result["active"] = 0;

$url = "";
$data = $button->getCurrentRecord();

$id = $data["id"];

$result["debug_var"] = "id=$id | ";
$sql = "SELECT * from studs where stud = $id";

$result["debug_var"] .= "sql=$sql | ";

$rs = DB::Query($sql);

$data1=$rs->fetchAssoc();

if($data1)

{

if ($data1["active"] == 1)

{

$url = "studs_view.php?editid1=".$data1["studid"];

$result["active"] = 1;

}

}
$result["debug_var"] .= " active=".$data1["active"]." | url=$url";

$result["url"] = $url;


Client After Event:



// ######## Debug Start ##########

var debug_var = result["debug_var"];

// Check console debug var

// console.log(debug_var);

// popup debug var

alert(debug_var);

// Display message with ajax

// ajax.setMessage(debug_var);

// OR if ajax does not work for you

// ctrl.setMessage(debug_var);

// ########## Debug End ###########



if (result["active"] == 1)

{

location.href=result["url"];

}


ACP

A
acpan 1/20/2020

Maybe it is better to hide the button when value is not 1.
In this case, do the SQL query at the before processed event, and use:
$pageObject->hideItem("your_custom_button_id", $recordId);
where your_custom_button_id is the item id at the designer page for your custom button.
ACP

M
mhollibush author 1/21/2020



Maybe it is better to hide the button when value is not 1.
In this case, do the SQL query at the before processed event, and use:
$pageObject->hideItem("your_custom_button_id", $recordId);
where your_custom_button_id is the item id at the designer page for your custom button.
ACP


I went through the online manual - was able to create a button for something else.

I am a little ( A Lot ) confused on this one...

I got the general idea - but not sure where to put everything.

You said put the SQL query "before process" does that mean do not do the query in Server Event?

Like I said.. way beyond my scope.... but want to learn

Sergey Kornilov admin 1/21/2020

I believe you guys are talking about two different things here.
@mhollibush is talking about doing something in button Server event while @acpan is talking hiding this button when necessary. The SQL query will be the same in both cases, which can be confusing.

M
mhollibush author 1/21/2020



I believe you guys are talking about two different things here.
@mhollibush is talking about doing something in button Server event while @acpan is talking hiding this button when necessary. The SQL query will be the same in both cases, which can be confusing.


ACPAN is kind of on the right trail...

I would need the button hidden if the value of the other table field was '0'
Which is another confusing thing for me to try and grasp <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=90013&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />

A
acpan 1/21/2020

Okay, let me try to make it a little clearer and easier. Since we are hidding the button based on condition, we move the SQL query to step 2 below to hide the button, as what @Sergey interpreted correctly.
Below is the complete start to end steps for your question:
Step 1: Insert custom button on View Page
Go to Page Designer and insert the custom button. Next, enter code below for the custom button:
In button's Server Event:



$data = $button->getCurrentRecord();

// get the student id

$id = $data["id"];

$url = "studs_view.php?editid1=$id";

$result["debug_var"] = "id=$id | url=$url";

$result["url"] = $url;


In button's Client After Event:



var debug_var = result["debug_var"];

alert(debug_var);

location.href=result["url"];


Notice there is no longer a need to check SQL for status above, so it is just a button to redirect without any condition check. We do this check in Step 2 below.
Step 2: Hide the custom button conditionally on View Page
We need the student id on the View Page to check for its status. The help shows you that Process Record Values Event has a parameter called $values, while Before Process Event does not.
So, in the View Page's Process Record Values Event:



// Get the current student id from the current view page:

$id = $values["id"];
// SQL Query student's status from studs table:

$sql = "SELECT * from studs where studid = $id";

$rs = DB::Query($sql);

$data_checked =$rs->fetchAssoc();

if($data_checked)

{

if ($data_checked["active"] == 0)

{

// hide the button, replace custom_button to your own item id.

$pageObject->hideItem("custom_button");

}

}



Note: To find your custom button's item id, go to Page Designer and click on the custom button, and the item id will be shown on the right.
ACP

M
mhollibush author 1/22/2020



Okay, let me try to make it a little clearer and easier. Since we are hidding the button based on condition, we move the SQL query to step 2 below to hide the button, as what @Sergey interpreted correctly.
Below is the complete start to end steps for your question:
Step 1: Insert custom button on View Page
Go to Page Designer and insert the custom button. Next, enter code below for the custom button:
In button's Server Event:



$data = $button->getCurrentRecord();

// get the student id

$id = $data["id"];

$url = "studs_view.php?editid1=$id";

$result["debug_var"] = "id=$id | url=$url";

$result["url"] = $url;


In button's Client After Event:



var debug_var = result["debug_var"];

alert(debug_var);

location.href=result["url"];


Notice there is no longer a need to check SQL for status above, so it is just a button to redirect without any condition check. We do this check in Step 2 below.
Step 2: Hide the custom button conditionally on View Page
We need the student id on the View Page to check for its status. The help shows you that Process Record Values Event has a parameter called $values, while Before Process Event does not.
So, in the View Page's Process Record Values Event:



// Get the current student id from the current view page:

$id = $values["id"];
// SQL Query student's status from studs table:

$sql = "SELECT * from studs where studid = $id";

$rs = DB::Query($sql);

$data_checked =$rs->fetchAssoc();

if($data_checked)

{

if ($data_checked["active"] == 0)

{

// hide the button, replace custom_button to your own item id.

$pageObject->hideItem("custom_button");

}

}



Note: To find your custom button's item id, go to Page Designer and click on the custom button, and the item id will be shown on the right.
ACP


OK...

tried what you had posted... this is the same thing I was running into with trying to create my own button....

it doesn't redirect to the "studid" the debug_var isn't displaying the anything past the studs_view.php?editid1=
Table structure of "studs"

studid ( int - key - used to display the view page)

stud ( int - the actual dog ( pulled from the dog table as the "id" of the dog )
trying what I have and also what you have posted this is where I am getting lost
the sql query needs to look at the stud table and pull the "stud" and the "studid"

the dog view page button needs to redirect the stud_view page and use the "studid" based on the "stud"
I probably messed that up and have you totally confused ( such as myself ) <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=90015&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />

A
acpan 1/22/2020

Okay, Try below. Slight changes - Use session variable to pass the studid obtained in Process Record Values event to Button's Server event.
=================

Information we have:
Table: dog

Fields:

id: id of dog (primary key of dog table)
Note: View page is based on this dog table.
Table: studs

Fields:

studid: primary key of studs table

stud: id of dog (foreign key to dog table)

active: 1 or 0 (status of dog in studs table)

=================
Step 1: Insert custom button on dog table's View Page
Go to Page Designer and insert the custom button. Next, enter code below for the custom button:
In button's Server Event:



// ** Below not necessary anymore, for logging purpose only

$data = $button->getCurrentRecord();

// get the dog id

$id = $data["id"];

// ** Above not necessary anymore, for logging purpose only
// #### get the studid value from "Process Record Values Event" ###

$studid = isset($_SESSION["studid"]) ? $_SESSION["studid"] : -1 ;

$url = "studs_view.php?editid1=$studid";

// #### get the studid value from "Process Record Values Event" ###
// Pass variables to Client After event

$result["debug_var"] = "id=$id | studid=$studid | url=$url";

$result["url"] = $url;


In button's Client After Event:



var debug_var = result["debug_var"];

alert(debug_var);

location.href=result["url"];


Step 2: Hide the custom button conditionally on View Page
In the View Page's Process Record Values Event:



// Get the current dog id from the current view page:

$id = $values["id"];
// SQL Query to obtain stud status(active) and studid from studs table using the dog id (stud):

// init values first

$stud = 0; $studid = 0; $active = 0;

$sql = "SELECT * from studs where stud = $id";

$rs = DB::Query($sql);

$data_checked =$rs->fetchAssoc();

if($data_checked)

{

$stud = $data_checked["stud"];

$studid = $data_checked["studid"];

$active = $data_checked["active"];

// set session value to be used by the custom_button's Sever event code

$_SESSION["studid"] = $studid;
if ($active == 0)

{

// hide the button, replace custom_button to your own item id.

$pageObject->hideItem("custom_button");

}

}
// debug values, turn on echo if needed:

$debug_var = "id=$id | stud=$stud | studid=$studid | sql=$sql | active=$active | studid session value =".$_SESSION["studid"];

// echo $debug_var;



Note: To find your custom button's item id, go to Page Designer and click on the custom button, and the item id will be shown on the right.
ACP

M
mhollibush author 1/23/2020



Okay, Try below. Slight changes - Use session variable to pass the studid obtained in Process Record Values event to Button's Server event.
=================

Information we have:
Table: dog

Fields:

id: id of dog (primary key of dog table)
Note: View page is based on this dog table.
Table: studs

Fields:

studid: primary key of studs table

stud: id of dog (foreign key to dog table)

active: 1 or 0 (status of dog in studs table)

=================
Step 1: Insert custom button on dog table's View Page
Go to Page Designer and insert the custom button. Next, enter code below for the custom button:
In button's Server Event:



$data = $button->getCurrentRecord();

// get the dog id

$id = $data["id"];
// #### get the studid value from "Process Record Values Event" ###

$studid = isset($_SESSION["studid"]) ? $_SESSION["studid"] : -1 ;

$url = "studs_view.php?editid1=$studid";

// #### get the studid value from "Process Record Values Event" ###
// Pass variables to Client After event

$result["debug_var"] = "id=$id | studid=$studid | active=$active | url=$url";

$result["url"] = $url;


In button's Client After Event:



var debug_var = result["debug_var"];

alert(debug_var);

location.href=result["url"];


Step 2: Hide the custom button conditionally on View Page
In the View Page's Process Record Values Event:



// Get the current dog id from the current view page:

$id = $values["id"];
// SQL Query to obtain stud status(active) and studid from studs table using the dog id (stud):

// init values first

$stud = 0; $studid = 0; $active = 0;

$sql = "SELECT * from studs where stud = $id";

$rs = DB::Query($sql);

$data_checked =$rs->fetchAssoc();

if($data_checked)

{

$stud = $data_checked["stud"];

$studid = $data_checked["studid"];

$active = $data_checked["active"];

// set session value to be used by the custom_button's Sever event code

$_SESSION["studid"] = $studid;
if ($active == 0)

{

// hide the button, replace custom_button to your own item id.

$pageObject->hideItem("custom_button");

}

}
// debug values, turn on echo if needed:

$debug_var = "id=$id | stud=$stud | studid=$studid | sql=$sql | active=$active | studid session value =".$_SESSION["studid"];

// echo $debug_var;



Note: To find your custom button's item id, go to Page Designer and click on the custom button, and the item id will be shown on the right.
ACP


Sorry for the delayed response:
With limited time, as my workload has picked up...

I tried the code you posted.....

It does redirect to the page... ( perfect )

I see that the button shows up on all the view pages even if the if ($active == 0)

I clicked on the button that was shown on a view page where ($active == 0) page and it directs me to the first "active == 1" record of the studs table?
watching the debug it displays the first "active == 1" record on any view page where the button should be hidden
I am lost.....
I am sure it has to do with the hideItem....
Button ID

$pageObject->hideItem("open_stud")

I named the item id and the event name the same: open_stud

A
acpan 1/23/2020

Check that you use Process Record Values event and NOT before process event.
Don't "describe" your debug values, show your debug values here.
Uncomment the echo in the event to show the debug values and paste here.
ACP

M
mhollibush author 1/23/2020



Check that you use Process Record Values event and NOT before process event.
Don't "describe" your debug values, show your debug values here.
Uncomment the echo in the event to show the debug values and paste here.
ACP


There is going to be a delay in troubleshooting this.... had a major setback

Starting almost from scratch.... HDD crashed not able to retrieve recent changes ( didn't back up the HDD since Saturday )

I really appreciate your time and effort trying to resolve this...
I will post the debug once I get to that point again...

A
acpan 1/23/2020

Okay ... no problem. Good luck with your recovery.
Follow the last suggestion, it works on my PC.
I am off for my holiday tomorrow, look at the debug values and adjust, should be easy to fix for you.
ACP

M
mhollibush author 1/25/2020

@acpan..
I am back to this point in the project after the crash...
Everything works as described except the "hideObject"
The button shows up on all view pages
I can only assume I am missing something with the hideItem
my complete "Proccess Record Values"



foreach($values as $key => $value) {

if ($value==="0")

$values[$key]="";

}
// Get the current dog id from the current view page:

$id = $values["id"];
// SQL Query to obtain stud status(active) and studid from studs table using the dog id (stud):

// init values first

$stud = 0; $studid = 0; $active = 0;

$sql = "SELECT * from studs where stud = $id";

$rs = DB::Query($sql);

$data_checked =$rs->fetchAssoc();

if($data_checked)

{

$stud = $data_checked["stud"];

$studid = $data_checked["studid"];

$active = $data_checked["active"];

// set session value to be used by the custom_button's Sever event code

$_SESSION["studid"] = $studid;
if ($active == 0)

{

// hide the button, replace custom_button to your own item id.

$pageObject->hideItem("studad");

}

}
// debug values, turn on echo if needed:

$debug_var = "id=$id | stud=$stud | studid=$studid | sql=$sql | active=$active | studid session value =".$_SESSION["studid"];

// echo $debug_var;


The debug on the view page that isn't a stud ( female dog )



id=13 | stud=0 | studid=0 | sql=SELECT * from studs where stud = 13 | active=0 | studid session value =2


so the id of the female is 13

but there is no "13" in the studs table

is that part of the problem?

A
acpan 1/25/2020

what is the purpose of extra foreach loop at the begining for your codes?

M
mhollibush author 1/25/2020



what is the purpose of extra foreach loop at the begining for your codes?



to hide any 0 value fields in the "dog" table.

I did this due to numerous "default" values of 0 on checkboxes
I did try the it by removing the extra foreach loop and it didn't change the outcome.

Button still shows up on all dog view pages

A
acpan 1/25/2020

Just my personal choice, i will hide values at the "view as/edit as", to make it easy and clean to debug.
Anyway, from your log, it seems when there is no corresponding record in the studs table, we did not hide the button. (You did not mention that the studs table may not have a record for dog table, which is why we assume studs table has a record and test for its active status).
Modify the codes to handle this as well, and also init the session value:



// Get the current dog id from the current view page:

$id = $values["id"];
// SQL Query to obtain stud status(active) and studid from studs table using the dog id (stud):

// init values first

$stud = 0; $studid = 0; $active = 0;

// ### also init session just to make sure it won't store the previous value ###

$_SESSION["studid"] = -1;

// ### also init session just to make sure it won't store the previous value ###
$sql = "SELECT * from studs where stud = $id";

$rs = DB::Query($sql);

$data_checked =$rs->fetchAssoc();

if($data_checked)

{

$stud = $data_checked["stud"];

$studid = $data_checked["studid"];

$active = $data_checked["active"];

// set session value to be used by the custom_button's Sever event code

$_SESSION["studid"] = $studid;
if ($active == 0)

{

// hide the button, replace custom_button to your own item id.

$pageObject->hideItem("custom_button");

}

}

// ##### add this condition check if no record found #######

else

{

// hide the button, if no record found in studs table (replace custom_button to your own item id)

$pageObject->hideItem("custom_button");

}

// ##### add this condition check if no record found #######
// debug values, turn on echo if needed:

$debug_var = "id=$id | stud=$stud | studid=$studid | sql=$sql | active=$active | studid session value =".$_SESSION["studid"];

// echo $debug_var;
A
acpan 1/25/2020

Refer to above comment:

  1. hide the button too if there is no record in studs table
  2. init the session value to make sure it does not store the previous value.

M
mhollibush author 1/26/2020



Refer to above comment:

  1. hide the button too if there is no record in studs table
  2. init the session value to make sure it does not store the previous value.


THANK YOU!!!!

Everything works now....
Now onto the list page...

samething.... display the button if the dog is in the stud table and is active
going to do some reading and see if I can figure it out....

Thanks again

A
acpan 1/26/2020

Glad it helps. The logging is crucial to get this works.
For hiding and showing the button on grid, refer to this tip:
Enable and Disable "inserted button" based on field's value on Grid
ACP

jadachDevClub member 1/26/2020

If your original code is working and you simply want to show a button instead of a link, why not use built in bootstrap.

<a href="/your_page/" class="btn btn-default">View Details</a>
M
mhollibush author 1/26/2020



If your original code is working and you simply want to show a button instead of a link, why not use built in bootstrap.

<a href="/your_page/" class="btn btn-default">View Details</a>



I am learning as I am doing this...

acpan has taken me down roads I thought I would never get a grip on.

I am enjoying the learning experience and it helps me in other areas of the project.
Being able to look at the code and understanding the logic behind it ( acpan has given great details on what does what ), makes it much easier for me to come up with solutions that are in other places.

Instead of bugging the forum on every little thing as I am a novice...

A
acpan 1/26/2020

@jerry brought up a very good point too.
For benefits of novice users, let's see the difference between the two methods - html(bootstrap) button code vs PHPRunner Custom Button
Below is how we could implement @Jerry's comment on html(bootstrap) button code.
Method 2: Show and Hide html(bootstrap) button based on external field data on View Page
We will use one of the fields on the view page and modify it to display conditionally a html(bootstrap) button on view page.

it is very similar to the original question using a html link, except that it presents a button instead of a URL.
Step 1: Go to Page Designer, select the field and click [b]View AS/Edit As to edit the field to insert html button[/b]
Step 2: Paste the codes below to form the url and show the html(bootstrap) button:



// init values

$studid = 0; $button_code = ""; $sql = "";
// get the dog id on the current view page

$id = $data["id"];
// get the studs table data with current dog id

$sql ="SELECT * from studs where active = 1 AND stud = $id";

$rs = DB::Query($sql);

$data_checked =$rs->fetchAssoc();

if($data_checked)

{

$active = $data_checked["active"];

$studid = $data_checked["studid"];

$url = "studs_view.php?editid1=$studid";

// ### form the bootstrap button with the URL data from another table ###

$button_code = "<a href='$url' class='btn btn-default'>View Details</a>";

// ### form the bootstrap button with the URL data from another table ###

}
// show the button

$value = $button_code;
// debug values, turn on echo if needed:

$debug_var = "id=$id | studid=$studid | sql=$sql | button_code = $button_code";

// echo $debug_var;



That's all you need to do.
When we choose to use html button code vs PHPRunner's custom button?


  1. If you just want to insert a static html button (or URL link) and forget it, use html button code or simply use URL link. In fact, it is better to do so, as you do not need to do much debugging when there is an error.
  2. If you anticipate you may need to add business logic to your button later, use the PHPR's custom button.
  3. If you just want to display a button to redirect to another page based on field value or table data, you can use html button code (simpler to implement and debug) or custom button, as in this case.
  4. If you need to handle Client Side Behavior (like show a message before and after button clicks), redirect URL based on field values and query data from database, PHPR custom button may be the preferred way in most situations.
    For the method specified here, we insert a HTML (bootstrap) button code and do some basic SQL query and show the button conditionally. For novice user to code this on a pure HTML+PHP+SQL, it will still be very difficult to implement. But with PHPRunner, it allows you to implement html button code with SQL logic easily too, with a few lines of codes as shown above.
    And if you need more advanced logic, use the PHPR's Custom Button Tri-Part Events, as shown in the earlier thread.
    Regard to this question, both method works. Html button code is simpler to handle, while custom button gives you ability to handle more complex logic.