This topic is locked

Howto add a single child record for many parent records, from parent t

7/11/2008 6:29:26 AM
PHPRunner Tips and Tricks
T
thesofa author

OK, I have to show a page of kids in a tutor group, be able to select all, none, many or some and add a common record to the child table for all of the selected records.

I have a master page built showing all of the pupil records, in addition I have 4 slave/child paGES BUILT, EACH WITH ADD PAGE AND LIST PAGE FOR THE FOLLOWING ACTIONS:-

  1. Detention
  2. Behaviours
  3. Efforts
  4. Service Awards


Breaking down the steps of the project I get:-

  1. Show the filtered records
  2. select the pupils who need the child record
  3. Place a link to the add record page on the new list page
  4. select the reason for the award
  5. add the award record for each of the selected children
  6. review the newly added records



Dealing with one thing at a time:-
Showing the filtered list, each teacher has a record field called group, this holds the name of the tutor group, ie 8P for year 8 kids in tutor group P.

Each kid has a record field in the Pupil table called reg with their tutor group in.

In the security settings for this page, on the advanced tab, select "users can see and edit their own data only"

set the two fields so that the owner field is `group` and the main table field is `reg`

When you show the list page, only those records in the tutor group will show.
When choosing which pages to build, make sure you select Delete. This enables the column of tick boxes and a select all tick box to select individual or all of the records for deletion, it also places a Delete Selected button on the page.
Adding a link to the Add new record for the list page for the multi add page.

Do this for the events specified

list page

BeforeProcessList event

//********** Custom code ************

// put your custom code here
$_SESSION["deleted_value"] = "";


List Page

BeforeDelete event

// Parameters:

// $deleted_values - Array object.

// Each field on the List page is represented as a 'Field name'-'Field value' pair

// $where - string with WHERE clause pointing to record to be deleted.
//********** Custom code ************

// put your custom code here

//delete records

// when you click on the delete button, the following happens

if(@$_POST["a"]=="delete")

return true;

//when you click on the other link, this happens

if (@$_POST["a"]=="update1")

{

//id of selected records are stored in the $_SESSION["deleted_value"] variable

$_SESSION["deleted_value"].=$deleted_values["id_pup"].",";

//this adds all the id records of the records marked from the list page into a session variable

//id_pup is the unique identifier for the pupil table, this must be included in the query to build the page and it must be on the pages to build

return false;

}
// return true if you like to proceed with deleting record

// return false otherwise


List Page

AfterMassDelete event

// Parameters:

// $records_deleted - number of deleted records
//********** Custom code ************

// put your custom code here

header("Location: nd_service_add.php?a=return");

exit();


List page, add a code snippet in the place you want the clickable link on the List page for this set, insert the code

// Put your code here.

echo "<A onclick=\"frmAdmin.a.value='update1'; frmAdmin.submit(); return false;\" href=\"#\">Multiple Service Awards</A>";


The Multiple Service Awards appears on the page, drag the event to where you want it to show on the list page.

You can very carefully delete the Delete Selected button by placing the cursor to its right and backspacing to remove the button, stop as soon as it has gone.

Then drag the new code snippet event into place where the button was. It looks neater.
OK, that is it in the new pages, now to modify the existing add service awards pages to deal with the new string of data.

On the normal add service page, called nd_service_add.php in my setup,

Add page

Before record added event

// Parameters:

// $values - Array object.

// Each field on the Add form is represented as a 'Field name'-'Field value' pair
//********** Custom code ************

// put your custom code here

global $conn;

if($_SESSION["deleted_value"])

{

$newrecords = explode(",",$_SESSION["deleted_value"]);

$tutor=$values["staff_id"];

$reason=$values["reason"];

$comment=$values["comment"];

$i=0;//sets counter to zero

While($i<(count($newrecords))-1)

{

$str = "insert into `nd_service` (`id_service`,`pup_id`,`staff_id`,`reason`,`comment`) values (NULL,'".$newrecords[$i]."','".$tutor."','".$reason."','".$comment."')";

db_exec($str,$conn);

$i++;

}

return false;

}

// return true if you like to proceed with adding new record

// return false otherwise
return true;



this sets $conn as a global variable

the if clause looks at the $_SESSION["deleted_value"] and if it is empty, it does the normal add a single record, if there is content, the string of comma seperated numbers we made earlier is loaded into the $newrecords array by using explode see php manual here

Using the $values array we set the fixed fields for this batch of recordsin the next 3 lines, for $tutor, $comment and $reason.

$i is a counter for the loop, we set it to zero next.

the while loop checks the condition before the loop is entered, so WHILE the counter is less than the number of records in the array, less one, we do the loop.

The line $str build up an Insert sqlstring.

db_exec runs the query

$i++ adds one to the counter.

Notice the return false; line this stops the page from saving the record sent to it by normal means, it prevents an error coming up.

once we are outside the curly braces, we have a return true; to allow normal use of the add page
Add Page

AfterAdd event (After Record Added)

// Parameters:

// $values - Array object.

// Each field on the Add form is represented as a 'Field name'-'Field value' pair

// $keys - Array object with added record key column values
//********** Redirect to another page ************

header("Location: nd_pupils_list.php?a=return");

exit();


I think that is all I have had to do, if this does not work, keep posting here and I will try to fix anything I have missed or messed.

Cheers