SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question about comparing data

    Hi,
    So I have a page where is listed some data from database in a table like this:

    title | data
    ----------------
    some1 | something1
    some2 | something2
    some3 | something3
    some4 | something4

    Now the question is if I wanted to compare data between one or more entries how to do it? First comes in mind to use checkboxes in each row and then show the data from the selected rows together somehow so the user can see the differences in the data in the entries he chose to look. But the problem with this is if there is a lot of data rows and multiple pages, then the checkbox values from the previous page is lost. Any ideas how to implement this kind of comparison?

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When they hit next/prev page, have it submit the current page's checkboxes with it, so as they move from page to page it is stored in a session or hidden input. When they load a new page of data, have it check the stored items and check the box if that item exists on the page.
    Last edited by funkdaddy; Jun 26, 2009 at 08:52.

  3. #3
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure if you got right what I am trying to achieve or maybe I didnt get your answer right. Anyway I want user to be able to select eg. two rows for comparison in page 1, then move to page2 and select 3rd row for comparison, and after that if the user hits "Compare" button the application should show the data for all these 3 selected rows in one set.

    I know how to make form submit from a link with javascript, but that submit is also triggering the comparing so if I submit this form, the user will be taken to the compare page with the 2 first selections in page 1 and wont proceed to page2 where he would like to select one more row before going to compare all these three rows. If you understood what I mean could you explain how to achieve this kind of functionality with the method you earlier mentioned?

    Edit:
    Found these... and I think this is exactly what I will be doing. I think these might solve my problems. Have to read them through.
    http://www.html-form-guide.com/php-f...rder-form.html
    http://www.html-form-guide.com/php-f...variables.html

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup, I dig what you're saying: Being able to select multiple items with checkboxes across multiple pages, then at any point, hit a compare button to send a compare page the items to compare - right?

    I don't know why you need javascript, doesn't each row correspond to a row of data in a database? I assume you have a unique id for each row? So every time the person navigates to a page of items, add the newly checked items to a hidden field (<input type="hidden" name="checked_items" value="1,2,3"/>) then whenever someone hits compare, the comparison page will take the newly checked values and the checked_items and grab the data to compare.

    Does that make it any more clear?

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wouldn't use sessions. I would just pass the data as hidden fields.

    You need to have the script which processes the form be able to distinguish between two actions.

    Code:
    <input type="checkbox" name="ids[]" value="123">
    <input type="checkbox" name="ids[]" value="456">
    <input type="checkbox" name="ids[]" value="5">
    <input type="submit" value="proceed normally">
    <input type="submit" name="compare" value="compare now">
    PHP Code:
    if (isset($_POST['compare'])) {
        
    // output it an a way that lets user compare the details easily
        // probably you want to fetch details of each id from the database
    } else {
        
    // they didnt specify they want to compare, 
        // so default is we assume they want to select more
        
    foreach ($_POST['ids'] as $id) {
            
    printf('<input type="hidden" name="ids[]" value="%d">'$id);

            
    // or show them what they have, maybe they wanna deselect some
            // but we select it for them by default
            // printf('<input type="checkbox" name="ids[]" value="%d" checked="checked">', $id);


        
    ... code here to give them more options ...
        }

    The brackets for name="ids[]" is a special syntax
    http://www.php.net/manual/en/faq.htm...aq.html.arrays

  6. #6
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for everyone who replied. I get the hidden field thing, but I am not sure if this becomes a problem in my case. The thing is that there is nothing like 1page-2page-3page-FINISH kind of process. It is like that there is paging and the user should be able to navigate back and forward while the app is keeping track of the selections untill the user press compare (on any of the pages). And the paging is done via links (a href). It is still possible with the hidden field way?

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If that's what you want, you definately should use sessions then.

    PHP Code:
    if (isset($_POST['compare'])) {
        
    // they want to compare the stuff
        
    print_r($_SESSION['ids']);
    } elseif (isset(
    $_POST['ids'])) {
        
    // they want to add more
        
    $_SESSION['ids'] = array_unique(array_merge($_SESSION['ids'], array_map($_POST['ids'], 'intval')));

    This is similar to a very primitive shopping cart.

  8. #8
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, its been very helpful. The latter thing looks like it is what I want.

    Edit:
    One more thing, in order to get $_POST['ids'] to the script dont I have to submit the form and when I do that also the $_POST['compare'] will be submitted since its the name of the submit button? So practically they both get executed?

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thinking about it, you probably want to do
    PHP Code:
    if (isset($_POST['ids'])) { 
        
    // they want to add more 
        
    $_SESSION['ids'] = array_unique(array_merge($_SESSION['ids'], array_map($_POST['ids'], 'intval'))); 
    }


    if (isset(
    $_REQUEST['compare'])) { 
        
    // they want to compare the stuff 
        
    print_r($_SESSION['ids']); 
    } else {
        
    // they dont want to compare yet

    You need more than one submit button. One button is "add any new ids to the list, and compare immediately", the other button "add any ids to the list, and do not compare".

    So you should assume if ids were sent, that they should be added to the list. This should happen regardless of whether or not they want to immediately compare.

    Then, after collecting any ids sent, you just check to see if the compare button was clicked. If so, show them whats in the list. Else, do something else.

  10. #10
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Allright, I'll see how this proceeds, thanks again. Btw. some reason why use $_REQUEST instead of $_POST in the other if?

  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just because I figured there's a chance you might have a page which doesn't offer them the ability to add more stuff to the compare list, but you might still want to have the ability to goto the page which compares whatever stuff they already have in the compare list. Then you can just hyperlink to it instead of needing a form, href="foo.php?compare"

  12. #12
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah I see. Didnt think bout that.

  13. #13
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still more to ask. I just realized what I am trying to do is like your example above, but the part "add any ids to the list, and do not compare" should not be another submit button, instead it should happen when page is changed from a link. So would this go the way I add on the paiging href tag something like onclick=> submit (javascript) the ids variable and when pressing the (only one compare submit) button then we would go to compare all the selected. Or... is it even possible?'

    Btw. with two buttons it works great.

  14. #14
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Javascript would be required.

  15. #15
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have always had some problems bringing values from javascript to php, so I have to ask again this. Say I have a link like this <a href="something.php&page=3" onclick="getCheckBoxValues();">page3</a>. So when I now click this link I can get the selected checkbox values in javascipt no problem wit a function something like this:
    Code:
    function getCheckBoxValues()
    {
    	// Get selected values from current page
    	// and save them to string or array for example.	
    }
    But the problem is how do I get this string/array from javascript into php $_SESSION['x'] -variable? Other way around this would be easy (from php to javascript) because php is executed before javascript on the server side but what about this way? Must use ajax or?

  16. #16
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem solved.

    Code:
    function getCheckedIds()
    {
        var linkHref = $(this).attr('href');
        var values = new Array();
        $.each($("input[name='ids[]']:checked"), function() {
            values.push($(this).val());
        });
    
        $.post('ajax/ajax_template_history.php', { 'test[]' : values },  function(data){
            alert("Data loaded: " + data);
            document.location = linkHref;
        });
        return false;
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •