Data Grid PHP Class 1.0 BETA

Wednesday, August 16, 2006
,

This PHP script is used to display data resulted from a MySQL query or an array, in an accessible way for the end-user.

39 comments:

  1. jeff said...

    Hi Stephen, this is great work. Extremely well documented - thanks.
    It's exactly what I was looking for to help out a client key in some data.
    The only thing that I wanted to ask is if there's a way to update the displayed row, and if the table contains no data, if there's a flag that I've overlooked to present a row's worth of empty text fields?
    Thanks again!

  2. Stefan Gabos said...

    hi jeff,
    i don't really understand what you mean by "update the displayed row" can you be more explicit?

    as for when there's no data in the query result - you'll have it in the new version which will be available at the start of the next week.

  3. M.Musa Ulker said...

    Hi Stefan. I'm trying to use your class in a project. But there is a problem. How can get contact with you? Waiting answer as soon as possible..
    email: musaulker[at]gmail[dot]com

  4. Stefan Gabos said...

    just make a comment here about what you want to achieve and i'll do my best to help you and maybe we can help some other people also

  5. ChrisV said...

    Stephen,
    First I want to say you have done an incredible job with this datagrid class, as well as with the others listed on your blog. Excellent job! I tested your datagrid class and examples 1-3 work just fine. When I run example 4, I get a javascript error for line 640, which refers to the "addJacaScript()." Just thought you should know.

    I would be happy to test any and all classes you develop. I can also give you alot of feedback on how I am using them.

    I think the next thing this class needs is to allow a user to click on a record (row) with a hyperlink and be taken to an html page that gives the complete returned query in a formatted (css or template) page. Im' trying to come up with such a solution right now. I will be happy to share with you anything I come up with.

    Keep up the good work!


    chris

  6. Stefan Gabos said...

    thanks chris. i am most excited about you testing the classes i develop. it would really help improve their quality and usability.

    please do let me know what can you achieve with the data grid class. i myself have done some nice tricks with it. :)

    well, this one is just a BETA for the moment so it will have some improvements until it's final release.

    thank again

  7. mchtower said...

    Hello Stefan,

    I am trying to use your class and have gotten example4 to display data. However, the display does not look like the screenshot you provided (no colors, text formatting, etc.)and the following error message is displayed at the top of the page : Notice: Undefined property: javaScriptCode in /home3/dofadmin/disciplesoffaith-www/newgrid/class.datagrid.php on line 640.

    Thanks

  8. Richard said...

    Hi Stephen,
    This is a great datagrid, which is exactly what I was looking for.

    However I noticed a small bug when you try to put two report on the same page.

    when you call: require_once $this->absolutePath."/languages/".$this->languageFile;

    on a second report, the language file do not get included. I fixed it easily by replacing it by a require.

    I'm also looking at adding a custom row at the end of the datagrid. This row could be used to do a sum of a column or other agregators. What do you think ?

  9. Stefan Gabos said...

    hi richard.

    yes, i've never thought about putting two datagrids on the same page :)

    i also thought about adding a custom row - it will be available in the final release sometime next week.

    thanks for your comments

  10. Stefan Gabos said...

    hi mchtower,

    there must be something wrong with how the template.xtpl file includes the stylesheet file (style.css) meaning that it can find it. that's why the data grid doesn't look like it's supposed to. when you're viewing the data grid in your browser, select "view source" and you should have a line that says "link rel="stylesheet" href="path-to-template-folder/style.css" type="text/css" where "path-to-template-folder" is probably wrong. you should edit the class.datagrid.php file, look for the line "$xtpl->assign("templatePath", $this->relativePath."/templates/".$this->template."/");" and modify it so that it will point correctly. i don't know why is not pointing correctly...

  11. Raph Awoseyin said...

    I just saw and tried this yesterday. Good work. Two things:
    1) Can you make the grid independent of the database driver? That is, why must it be mySQL?
    2) I would like some control over the rows background colour scheme, and generally on the display fonts. Maybe these are already there but I've not seen them in the documentation.

  12. Stefan Gabos said...

    hi Raph Awoseyin,

    well, indeed i made it to work only with mySQL...but i am sure you can make the necessary changes to fit whatever database - i mean there are only a few call.

    as for changing fonts and colors you must edit the style.css file which is located in /templates/default/

  13. psilokan said...

    Great product, saves me a lot of work. I also get an error on line 640 with example 4 :(

    Also, how would I get this working with Smarty? I've been scratching my head trying to figure that one out :P

    In any case, thanks for all the effort you've put into this.

    Brad
    FettesPS

  14. Stefan Gabos said...

    download the file again. i've put the latest version in it with which example4 works for me. i hope it's working now

  15. mchtower said...

    Hi Stefan,

    You were correct, the template path was wrong, I changed it and now all is well.

    This is great work. I'm using the datagrid for a Homeowners Association website that is about to go live and have encountered no functional problems with it, I'll let you know if I find anything.

    Also, I tweaked the template a bit to add a print icon which when clicked uses FPDF code to create a PDF file with the contents of the datagrid. When I'm done I can send you the modifications if you think they would be helpful.

    Again, great work and thank you.

  16. Joeri said...

    Dear Stefan,

    I use your datagrid but if I put 2 datagrids on the same page its interrupt each other.
    I don’t understand it If you make 2 objects then it need too be independent from the other object but that isn’t . What don’t work is the custom functions for the second dategrid and sorting If I put different sorting on both grids its get corrupted.
    1 datagrid on a page works fine.

    Greets Joeri

  17. Stefan Gabos said...

    you are right, indeed two instances won't work on the same page. i will fix it. thanks for reporting

  18. Norbert Halasz said...

    Hello everybody,

    I'm new in programming php and sql (just started to learn in the last two weeks), but this class seems very good.
    I've tried to understand how to use this class through the examples, but there are lot of things which is not clear for me.
    Can anybody send me other examples, small programms made with this class?
    It would be very helpfull for me.

    Thanks,

    Norbert

    hnorby@citromail.hu

  19. Seraphim said...

    Hi Stefan,

    You made a nice class which is very usefull. I had to made one improvement because some of my columnnames in mysql had spaces. So i changed:
    ORDER BY `".$this->sortField.(strtolower($this->sortDirection) == "desc" ? "` DESC" : "`"); and now i cane sort evry column. I also noticed that sorting one grid will also sort the second grid on the same columnnumber. It would be nice if you could fix this.

    Thanks

  20. John said...

    hi stephan,

    great class!!!

    I'm very new to javascript...

    I'm trying to modify example 4 so that when i clic on a line i am redirected to another page but nothing happens...

    It looks like this:

    function action($value_of_clicked_field, $array_with_the_values_of_all_fields_in_clicked_row)
    {
    return "[]";
    }

    can someone help me??

  21. Justin Kelly said...

    Hi Stephan,

    Great work on the datagrid, just a couple of questions

    1. have you any plans to add filtering or search?

    2. not really sure about the CC license, but can i redistribute this code in an open source app im working on - i wont be modifying your stuff just using it to create datagrids?

    Thanks again

    Cheers

    Justin
    justin @ kelly.org.au

  22. Stefan Gabos said...

    Hi Justin,
    I plan to add A LOT of features but I just don't find the time - I have a huge text file where I write down bugs to fix and features to add and is just getting bigger all the time. Hopefully I will find the time to work on them.

    Yes, you can use the class in your application as long as:
    1. you tell me where I can download the application
    2. you don't modify the code
    3. you keep somewhere a refference to http://stefangabos.blogspot.com

  23. Anonymous said...

    when I try to import the example.sql with phpmyadmin it gives an error on line 7, how else do I import the information?

  24. Andy Hawkins said...

    To fix the error:
    Notice: Undefined property: javaScriptCode on line 640

    Place This:
    $this->javaScriptCode = "";
    Under:
    $this->error = "";

    In:
    class.datagrid.php

  25. Andy Hawkins said...

    I have implemented the class functionality to search.

    It needs about one more hour of work, but the search works.

  26. Anonymous said...

    Hi Stephen,

    Nice work but i cant get it to work for some reason. Im using your Database class in conjunction with the Datagrid. Here is my code:


    require_once "../inc/class.database.php";
    require_once "../inc/inc_functions.php";

    This page lists all the videos in the database. You can change the featured video and show and hide any video you have on the Youtube account.

    $db = new database();
    $db->debug = true;
    $db->connect();
    $result = $db->query("SELECT * FROM video");

    /**
    * The simplest way to list a query's result
    * You can already
    * move between pages,
    * change the number of records displayed in a page
    * and sort on every column
    */

    // require the class
    require "../inc/class.datagrid.php";

    // instantiate the class and pass the query string to it
    $grid = new dataGrid($result);

    // show all the columns
    $grid->showColumn("VideoId");
    $grid->showColumn("Description");
    $grid->showColumn("Enabled");
    $grid->showColumn("ActiveDate");

    // sort the data by the "title" column
    $grid->setDefaultSortColumn("VideoId");

    // witness magic!
    $grid->render();


    When running the script all i get is:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #6 ORDER BY VideoId LIMIT 0, 4' at line 1

    Resource id #6 ORDER BY VideoId LIMIT 0, 4

  27. Stefan Gabos said...

    That's because you're not using it right! On "$grid = new dataGrid($result);" you're not supposed to pass $result as parameter but the SQL string! It should be something like:

    $sql = "SELECT * FROM video";
    $grid = new dataGrid($sql, &$db);

  28. Jeroen Benats said...

    Great class! I just had to change one line in the datagrid class because I was getting several STRING and L_NUMBER errors while navigating through the grid. After some research I found out it was because of some values of the columns I was showing contained single quotes (f.e. "The 80' - 90' party"). All I had to do was changing line 1029 from:

    eval("\$action = ".$this->columnsToShow[$field]["action"]."('".$row[$field]."', \$row);");

    to:

    eval("\$action = ".$this->columnsToShow[$field]["action"]."('".addslashes($row[$field])."', \$row);");

    Thanks!

  29. Stefan Gabos said...

    no, man - thank YOU!

  30. Michael said...

    Hello Stefan,

    Great class. I almost have it working the way I need it to. I added a selector column and a submit button to the grid but can't figure out how to get the submit button to process the selected rows. Any help you can provide will be greatly appreciated.

  31. Michael said...

    Hello again Stefan,

    I figured out some of what I needed to do to process selected records. In the Render function I call another function that checks to see if the submit button has been pressed and if so it will call a function that processes the selected records.

    The problem I am having now is that the values for the selector column field appear to not be accessible. I added this code which outputs all available form variables :
    if(isset($_POST['submit'])) {
    echo '' . "\n";
    print_r($_REQUEST);
    echo '' . "\n";

    and this is the result :

    Array
    (
    [dataGrid_page] =>
    [dataGrid_sortfield] => lastname
    [dataGrid_sortdirection] =>
    [dataGrid_recordsOnPage] => 15
    [submit] => Register
    )

    When I view the source for the page that outputs the grid I see that the show selector column has a name associated with it but I can't figure out why the values for it can not be accessed.

  32. Michael said...

    Stefan,

    I figured out the previous problem. I'm not sure if this is by design but if the user makes no selections the array that holds selections is not created and therefore cannot be examined. I got around this by checking to see if the key for the selection array exists in $_REQUEST like this :
    if (array_key_exists('check', $_REQUEST).

    If there a more efficient way to do this?

  33. Peck Ling said...

    function action($value_of_clicked_field, $array_with_the_values_of_all_fields_in_clicked_row)
    {

    return "javascript:window.location.href('Delete_Feedback.php?FId=". $array_with_the_values_of_all_fields_in_clicked_row["FId"]."')";
    }

    Could anyone help regarding this???
    It works in IE but when i test in Mozilla,it can't works.Thanks!

  34. Michael said...

    Peck,

    I'm trying to figure out why your function does not work in Mozilla, so far I'm stumped. I have noticed that javascript sometimes cause errors in the grid. For example, I'm also trying to figure out why adding a button with a javascript function is causing the grid paging functions to produce javascript errors. I will let you know if/when I find a solution to your problem.

  35. Peck Ling said...

    Thanks Michael!

  36. Peck Ling said...

    Can somebody help me regarding this

    function action($value_of_clicked_field, $array_with_the_values_of_all_fields_in_clicked_row)
    {

    return "javascript:window.location.href('Delete_Feedback.php?FId=". $array_with_the_values_of_all_fields_in_clicked_row["FId"]."')";
    }

    if works in IE,but can't work in Mozilla when i try to click on the row.

  37. Sylus said...

    Hi Stephan!!

    Thanks for this great class.

    I run into some problems .
    I am using chcekboxes .I get options check all etc.

    This works fine.But in case i out my grid inside a form, on clicking check all..etc.. it goes to another page.

    say for example my current page is http://10.31.45.46/xampp/abc.php" , on clicking it goes to http://10.31.45.46/xampp

    How can i get rid of this problem?

  38. Stefan Gabos said...

    the problem is that the grid itself is a form...and therefore you have nested forms which is what causing your problems. you could add your controls to the grid's form by using the setHeaderHTML() method.

  39. Sylus said...

    Thanks stephan!!

    I am aware of the setHeaderHTML() method,but I have already worked around the problem.

    I have another issue, with each row (from my databse), i want to display an aditional field that is not in the database . This is a gif image and will vary from row to row.

    I tried using showCustomColumn and setColumnHTMLProperties to display a gif image in all the rows.But i need different gif image in different rows.

    How to do that?

Post a Comment

GlossyBlue Blogger by Black Quanta. Theme & Icons by N.Design Studio
Entries RSS Comments RSS