This topic is locked

Add record to detail table from master table list view

8/4/2012 10:41:02 PM
PHPRunner General questions
D
dkasing author

I have a master table that consists of ideas - IdeasTable and a detail table that consists of votes for each idea - VotesTable. They are linked on the IdeaID field. I want voters to be able to enter their votes for each idea in IdeasTable in the IdeasTable list view. I have tried using the Master/Detail approach, but it requires several steps to enter a vote - first have to add a new record in line, then save it, then close the detail view. I would like to have one column in the IdeasTable list view that contains a drop down list from which voters can just choose a vote and which would then add a record to the VotesTable containing the IdeaID, VoterID and VoteScore. I think I can do this by adding a button to each row which would then trigger an update in the VotesTable. I can get the IdeaID from the field in the current table, and I can get the VoterID from a global variable set when the user logs in. But is there some way I can get the VoteScore from a dropdown list based on its value at the time the button is pressed? Right now I have the drop down list updating a field in the IdeasTable that stores its current value, but I think this may create some concurrency problems later. Ideally I would be able to include a drop-down list not linked to a data field and just capture its current value when the voting button is pressed. Any ideas on how to do this? Or is there a better way to do this?
Thank you.
Douglas

C
cgphp 8/6/2012
  1. Create an alias field for one of the fields of the table and set it as custom
  2. In the custom field enter the code to build a dropdown
  3. Add a button for each row in the list page: http://xlinesoft.com/phprunner/docs/inserting_button.htm#grid
  4. Add the client and server code to the button events in order to update the vote values in the database

D
dkasing author 8/7/2012

Christan,

Thanks - this seems to be exactly what I would like to do. I have created an alias field and set it as custom and have added the code to the custom field. The drop down list box is correctly created, but instead of appearing in each row of the table, the proper number of drop-down lists appear one after another at the top of the page. I suspect this has to do with the window.location in the code below, but I don't know how to set the location to the current table row and can't seem to find an example of this in the forum. Any suggestions?
Douglas
echo "Vote";

//create dropdown box

$str = "";

$str.= "<select onchange=\"window.location.href=this.options[this.selectedIndex].value;\">

<option value=\"\">Please select</option>";
//select values from database

//create dropdown box

$str = "";

$str.= "<select onchange=\"window.location.href=this.options[this.selectedIndex].value;\"><option value=\"\">Please select</option>";
//select values from database

global $conn;

$strSQL = "select Score from ie_scoring";

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

while ($data = db_fetch_array($rs))

$str.="<option value=\"v_vote_innovations_list.php?a=search&value=1&SearchFor=".$data["Score"]."&SearchOption=Contains&SearchField=Score\">".$data["Score"]."</option>";
$str.="</select>";

echo $str;

C
cgphp 8/7/2012

The Custom field doesn't accept echo statement. To echo the dropdown, assign the $str var to $value. Please, check the following code:



$str = "";

$str.= "<select onchange=\"window.location.href=this.options[this.selectedIndex].value;\"><option value=\"\">Please select</option>";
//select values from database

global $conn;

$strSQL = "select Score from ie_scoring";

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

while ($data = db_fetch_array($rs))

$str.="<option value=\"v_vote_innovations_list.php?a=search&value=1&SearchFor=".$data["Score"]."&SearchOption=Contains&SearchField=Score\">".$data["Score"]."</option>";

$str.="</select>";
$value = $str;
D
dkasing author 8/7/2012

Cristian:

Thanks for the fast reply. This worked perfectly - the drop down lists are in each row now. I will work on the rest of the script and hopefully I can complete it without too many more questions for you.
Thanks again.
Douglas



The Custom field doesn't accept echo statement. To echo the dropdown, assign the $str var to $value. Please, check the following code:



$str = "";

$str.= "<select onchange=\"window.location.href=this.options[this.selectedIndex].value;\"><option value=\"\">Please select</option>";
//select values from database

global $conn;

$strSQL = "select Score from ie_scoring";

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

while ($data = db_fetch_array($rs))

$str.="<option value=\"v_vote_innovations_list.php?a=search&value=1&SearchFor=".$data["Score"]."&SearchOption=Contains&SearchField=Score\">".$data["Score"]."</option>";

$str.="</select>";
$value = $str;


D
dkasing author 8/7/2012

Cristian:

OK, I am stuck again already. The drop down list code that I copied filter the records on the page when I actually just want it to select a number so that I can use this number in steps 3 and 4 below. I have tried to remove the code that does the filtering but am not successful. Can you please advise? Sorry I have so many questions about this.
Thank you.
Douglas



Cristian:

Thanks for the fast reply. This worked perfectly - the drop down lists are in each row now. I will work on the rest of the script and hopefully I can complete it without too many more questions for you.
Thanks again.
Douglas

C
cgphp 8/7/2012

Check the following code:

$str = "";

$str.= "<select id='vote_".$data['primary_field_name']."'><option value=''>Please select</option>";
//select values from database

global $conn;

$strSQL = "select Score from ie_scoring";

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

while ($record = db_fetch_array($rs))

$str.="<option value='".$record['Score']."'>".$record['Score']."</option>";

$str.="</select>";
$value = $str;


Replace primary_field_name with the real name of the primary field of the list page.

D
dkasing author 8/8/2012

Dear Cristian:

OK, everything is working now, except that the button I have added to the row on the list page is able to add all the data to my Voting (ieappraisal) table except for the score value from the aliased drop-down list box (getscore). The current code is below. Interestingly, this code only works when the row is not selected. If it is selected there is no table update. But I can live with this if I can get the value from getscore into the ieappraisal table. Any advice on this?
Thank you.
Sincerely,

Douglas
global $dal;
$data = $button->getCurrentRecord();
if ($keys["id"])
{
$Vote = $dal->Table("ie__appraisal");
$Vote->project_id = $_SESSION["current_project"];
$Vote->innovation_id = $keys["id"];
$Vote->score=$data["getscore"];
$Vote->appraiser_id = $_SESSION["user_ID"];
$Vote->date_modified = now();
$Vote->Add();
}
$result["txt"] = "Your vote was cast";



Check the following code:

$str = "";

$str.= "<select id='vote_".$data['primary_field_name']."'><option value=''>Please select</option>";
//select values from database

global $conn;

$strSQL = "select Score from ie_scoring";

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

while ($record = db_fetch_array($rs))

$str.="<option value='".$record['Score']."'>".$record['Score']."</option>";

$str.="</select>";
$value = $str;


Replace primary_field_name with the real name of the primary field of the list page.

C
cgphp 8/8/2012
global $dal;
$data = $button->getCurrentRecord();
$Vote = $dal->Table("ie__appraisal");

$Vote->project_id = $_SESSION["current_project"];

$Vote->innovation_id = $data["id"];

$Vote->score=$data["getscore"];

$Vote->appraiser_id = $_SESSION["user_ID"];

$Vote->date_modified = now();

$Vote->Add();
$result["txt"] = "Your vote was cast";
D
dkasing author 8/9/2012

Cristian:

OK, the script now is able to include "score" to the record that is added to the ie__appraisal table by getCurrentRecord, but the value it adds is always 0. Despite that I can see a number selected in the drop_down list box within the row on the list page. Here is the button script I am using:
$str = "";

$str.= "<select id='v_voteinnovations".$data['id']."'><option value=''>Please select</option>";
//select values from database

global $conn;

$strSQL = "select Score from ie_scoring";

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

while ($record = db_fetch_array($rs))

$str.="<option value='".$record['Score']."'>".$record['Score']."</option>";

$str.="</select>";
$value = $str;
v_vote_innovations is the name of the list page

id is the primary key for the list page
What am I missing here?
Thank you.
Sincerely,

Douglas


global $dal;
$data = $button->getCurrentRecord();
$Vote = $dal->Table("ie__appraisal");

$Vote->project_id = $_SESSION["current_project"];

$Vote->innovation_id = $data["id"];

$Vote->score=$data["getScore"];

$Vote->appraiser_id = $_SESSION["user_ID"];

$Vote->date_modified = now();

$Vote->Add();
$result["txt"] = "Your vote was cast";


D
dkasing author 8/9/2012

Dear Cristian:

Yes, I have exactly as below in the server code for the button. All the fields in the new record are added properly except for the "score" field which is always 0. Is there something else I need to look at besides the server code in the button and the custom code for the dropdown list?
Thank you.
Sincerely,

Douglas
global $dal;
$data = $button->getCurrentRecord();
$Vote = $dal->Table("ieappraisal");

$Vote->project_id = $_SESSION["current_project"];

$Vote->innovation_id = $data["id"];

$Vote->score=$data["getScore"];

$Vote->appraiser_id = $_SESSION["user_ID"];

$Vote->date_modified = now();

$Vote->Add();
$result["txt"] = "Your vote was cast";



Cristian:

OK, the script now is able to include "score" to the record that is added to the ie
appraisal table by getCurrentRecord, but the value it adds is always 0. Despite that I can see a number selected in the drop_down list box within the row on the list page. Here is the button script I am using:
$str = "";

$str.= "<select id='v_voteinnovations".$data['id']."'><option value=''>Please select</option>";
//select values from database

global $conn;

$strSQL = "select Score from ie_scoring";

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

while ($record = db_fetch_array($rs))

$str.="<option value='".$record['Score']."'>".$record['Score']."</option>";

$str.="</select>";
$value = $str;
v_vote_innovations is the name of the list page

id is the primary key for the list page
What am I missing here?
Thank you.
Sincerely,

Douglas

C
cgphp 8/9/2012

In the "Client before", get the value of the dropdown and pass that value, with the params array, to the Server event of the button.

D
dkasing author 8/9/2012

Dear Cristian:

Can you point me to any examples of how to do this? Also, how to get the name or id of the dropdown which I am creating in the custom field?
Thank you.
Sincerely,

Douglas



In the "Client before", get the value of the dropdown and pass that value, with the params array, to the Server event of the button.

C
cgphp 8/9/2012

In the "Client before" event:

var id = rowData.keys[0]; //get the value of the primary key

params['score'] = $("select[id^='vote_" + id + "']").val();


In the "Server" event:

global $dal;
$data = $button->getCurrentRecord();
$Vote = $dal->Table("ie__appraisal");

$Vote->project_id = $_SESSION["current_project"];

$Vote->innovation_id = $data["id"];

$Vote->score=$params["score"];

$Vote->appraiser_id = $_SESSION["user_ID"];

$Vote->date_modified = now();

$Vote->Add();
$result["txt"] = "Your vote was cast";
D
dkasing author 8/10/2012

Dear Cristian:

This wasn't working - the score field always shows no value when the new record is added, so I decided to test the params variable by passing it a fixed value in the "Client Before" event as in the code below. The first time I do this, it works, meaning that the value of the score field in the vote table will be set to 7. But if I then edit the "Client Before" event to send an "8" it, rebuild the project and try it again, it will not work. It will continue to add a 7 to the score field in the vote table.
I feel like if I can't even pass a fixed value through the params variable, then I won't be able to send the selected row from the drop down list.
Any idea what might be causing this problem?
Sincerely,

Douglas
var id = rowData.keys[0]; //get the value of the primary key

//params["sendscore"] = $("select[id^='vvote" + id + "']").val();

params["sendscore"] = "7";

//params["txt"] = "Hello";

ctrl.setMessage("Processing your vote...");

// Uncomment the following line to prevent execution of "Server" and "Client After" events.

// return;



In the "Client before" event:

var id = rowData.keys[0]; //get the value of the primary key

params['score'] = $("select[id^='vote_" + id + "']").val();


In the "Server" event:

global $dal;
$data = $button->getCurrentRecord();
$Vote = $dal->Table("ie__appraisal");

$Vote->project_id = $_SESSION["current_project"];

$Vote->innovation_id = $data["id"];

$Vote->score=$params["score"];

$Vote->appraiser_id = $_SESSION["user_ID"];

$Vote->date_modified = now();

$Vote->Add();
$result["txt"] = "Your vote was cast";


C
cgphp 8/10/2012

Post the code of the dropdown, the code of the "Client before" event and the code of the "Server" event.

D
dkasing author 8/18/2012

Just to close the loop on this one for now, I have uploaded the files to tech support and am awaiting a response from them. I will update when I hear back.
Douglas



Post the code of the dropdown, the code of the "Client before" event and the code of the "Server" event.