This topic is locked

How to select records currently displayed on list page without selecti

9/21/2011 2:16:09 PM
PHPRunner Tips and Tricks
F
FunkDaddy author

Problem: I needed to select the individual field values of each record/row in the list page without using an checkboxes to select each one.
Why you ask?
Because I designed a mobile web app where showing those record selector checkboxes was not necessary. As a matter of fact, displaying them goes against most mobile design and UI good practices. In my particular scenario I had created a list page that emulated the iPhone's list view with chevrons to navigate to the record.
Let me emphasize one thing: I was NOT looking for a solution that would let me select all records on the page with a single click (which in turn would use javascript to select all other checkboxes on the page). I was looking for a way to refer to the records on the page eventhough NO record/row checkboxes were available on that page (intentionally so as per my client's requirements).
Let me provide the use case so it's easier to follow along:

  1. All this designed using PHPR 5.3 (prior to any mobile templates as provided in PHPR 6.0).
  2. I had a list of students names, phones, and their grade levels for a school directory displayed on list page for mobile device.
  3. I wanted to allow users to filter the records on page by selecting a dropdown control that would let them select grade level to filter and show only student records from the selected grade. That was easy to implement by simply inserting a php snippet on list page and binding JS to reload page with new query string parameters (you can find how to do that in several other posts in tips & tricks in these forums).
  4. I wanted to allow my users to click on a button at the bottom of the list page that would allow them to add all records currently displayed on page (whether they resulted form a filter call or not) to another table in my app which allows them to save "Favorite" contact information based on the selected student records.
  5. Clicking this button (which I labeled "Add All To Favorites") would automatically identify what was being displayed on the page, pick-up the unique record ID's from each row and insert them via a MySQL INSERT INTO query to populate the "favorites table" (which is where those favs were stored and shown).
  6. I contacted support and scoured the forums for a way of doing this to no avail. Contact got back to me and said it could not be done using any of the available events in PHPR, nor had any suggestions for modifying output files directly (something I like to avoid doing, but sometimes is a necessary action to take).
  7. I was able to find a solution, albeir very hacky it does get the job done... so here are the steps:
    STEPS:
  8. Insert a custom php button (Client Before, Server, Client After) near the footer of the list page you want to use.
  9. Client Before Tab:



this.setEnabled(); //Allows butotn to be clicked more than once without needing a page refresh

params["rowCount"] = $('#grid_block1 tr').size() - 1; //Row count minus header tr. Jquery method to count records on list page.
if(params["rowCount"] > 35){

alert('You can only add up to 35 records to favorites at a time.\r\nPlease reduce your selection and try again.')

return;

}

params["txt"] = "Info Was Added";

ctrl.setMessage("Adding "+params["rowCount"] +" entries to Favorites...");


3.Server Tab:



// Put your code here.

$result["txt"] = $params["txt"];//Used in client after event to display "info was added message".

$_SESSION["rowCount"] = $params["rowCount"];//Get this from "Client Before" event of this buton (jquery counting rows)

//We need to slice the array because it's constantly appending itself. So we slice based on the # of records returned on the page which we count with Jquery when the "Add All Shown To Favorties" button is clicked!

$ParentID = array_slice($_SESSION["Page_Records_Array"],$_SESSION["rowCount"]*-1,$_SESSION["rowCount"]); //We slice only the portion that we need of the array

$StudentConcatName = array_slice($_SESSION["Page_Records_Array_2"],$_SESSION["rowCount"]*-1,$_SESSION["rowCount"]);

$StudentID = array_slice($_SESSION["Page_Records_Array_3"],$_SESSION["rowCount"]*-1,$_SESSION["rowCount"]);

$FamID = array_slice($_SESSION["Page_Records_Array_4"],$_SESSION["rowCount"]*-1,$_SESSION["rowCount"]);
//We insert as many records as we have rows and iterate over our arrays in the same sequence as we insert them!

// Thus, we match the same order and pair up the right fields with the correct rows on list page!

global $conn;

for($i=0;$i < $_SESSION["rowCount"]; $i++){

$strSQLInsert ="

INSERT IGNORE INTO

favorites_tbl

(

LinkFamilyID,

Favorite_Type,

Favorite_Name,

LinkFavToUserID,

LinkStudentID

)

Values

(

".$FamID[$i].",

'1',

'".$StudentConcatName[$i]."',"

.$_SESSION["UserID"].","

.$StudentID[$i]."

)

";

db_exec($strSQLInsert,$conn);



};
unset($_SESSION["Page_Records_Array"]); //Otherwise this array never stops growing

unset($_SESSION["Page_Records_Array_2"]); //Otherwise this array never stops growing

unset($_SESSION["Page_Records_Array_3"]); //Otherwise this array never stops growing

unset($_SESSION["Page_Records_Array_4"]); //Otherwise this array never stops growing

$_SESSION["rowCount"] = 0; //Reset row count.


4. Client After Tab:



var message = result["txt"] + " !!!";

ctrl.setMessage(message);


5. Now in the same list page go to "List Page: After record processed" event and add the following:


//Feeds this to the "On Server" event of the "Add All To Favorites" button

$_SESSION["Page_Records_Array"][]=$data["ParentID"];

$_SESSION["Page_Records_Array_2"][]=$data["StudentConcatName"];

$_SESSION["Page_Records_Array_3"][]=$data["StudentID"];

$_SESSION["Page_Records_Array_4"][]=$data["LinkFamilyID"];
//Needed to prevent array from groing too large! Eliminates duplicates. Thus array won't get larger than records per school

$_SESSION["Page_Records_Array"] = array_unique($_SESSION["Page_Records_Array"]);

$_SESSION["Page_Records_Array"] = array_unique($_SESSION["Page_Records_Array_2"]);

$_SESSION["Page_Records_Array"] = array_unique($_SESSION["Page_Records_Array_3"]);

$_SESSION["Page_Records_Array"] = array_unique($_SESSION["Page_Records_Array_4"]);


6. Note that when the after record process event runs its iterating over each record/row and our lines of code are essentially looping along and appending each record's array to our session variable which were also formed as arrays.
7. We then pull out each corresponding record accordingly in the "Server Tab" script when our button is pressed. The FOR loop there is then able to iterate over the array in the exact same order and number of records/rows displayed on the list page. This allows for accurate record insertion into the favorites table.
8. Note where text based record field data is used I enclosed it in single quotes in my insert query. When using numerical type data that is not necessary since MySQL understand the data type as non-string. So, don't forget to take than into consideration when building your insert or update or delete query.
9. Notice we unset the arrays to prevent them from growing too large after we've made use of our records and inserted them into the other table
10. Also notice that I used "INSERT IGNORE INTO" syntax in MySQL query on "Server Tab". This automatically prevents duplicate records from being inserted into my favorites table. Thus, if I had 10 records listed on page and pressed the "Add All Shown To Favorites" button 3 times, I would still only have 10 records added to my favorites. You do need to have a unique index for this to work (or multiple unique indexes if you need it) to control what is considered a "duplicate" record or not a duplicate by MySQL. Note that null values in a field do not count when determining the validity of a duplicate in a unique index. I probably made that sound more complicated than it is, but trust me a quick google search on the topic and you'll see what I mean.
That's all. I hope this helps anyone else looking to perform a similar task!
Cheers,