Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Junior Member
    Join Date
    Apr 2006
    Posts
    3
    Member #
    12986
    Hi everyone,

    I've been searching around the net for over a week now on how to actually get this working, so maybe some of you folks can help me out. This is what I'm trying to do:

    I am creating a survey results page that displays all the results that have been submitted via a form I've created. I would like to create a page that displays the results in a table, and be able to sort the data by each of the fields that are in the table. I also want to display a checkbox next to each entry so I can delete useless garbage that people might have submitted (including my own test submissions to make sure the form works). I also want to be able to paginate the results and display 20 results per page.

    Here's what I've come up with so far. I googled around and found a script that will display the results in a table and give the option to sort each field. I've also found another tutorial on how to implement the checkboxes, but here's the problem - I don't know where in each of the scripts to combine them for them to work properly. My knowledge of PHP is intermediate, so I'm not a total n00b, but with this project you might as well call me a n00b.

    http://codewalkers.com/tutorials/21/2.html is the URL I found that will display the db results and give you the option to organize them via each field.

    http://www.phpeasystep.com/mysqlview.php?id=8 is the URL I found that will implement the checkboxes.

    I have found a URL to paginate, but I believe I can do that on my own.

    Now, my question is, what do I need from the latter URL to throw into the first URL in order for it to work, and where exactly do I place it? Is it even possible to do with the two examples of code I gave? If not, could someone point me in the direction of a tutorial that will give me the results I want?

    Thanks to everyone in advance. If there is any more information I can provide to help, please let me know.

    Thanks again!

  2.  

  3. #2
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    If you just want a sortable table with delete-checkboxes, based on the two example codes you provided, then you can do it as shown below.
    I have used the original code with a minimum of alterations, so you should be able to recognize it.

    Notice that the array "$allowed_order" is hardcoded (as in the original example), so you can only use this code on a table that contains the columns "name", "lastname", "email". (based on the provided example table shown below, that came with the "delete" code)
    If you want a code that's capable of displaying any given table, with the sort functionality available on all columns, you will have to query the table first, and then build the array dynamically, based on the table column names.
    Code:
    CREATE TABLE `test_mysql` (
    `id` int(4) NOT NULL auto_increment,
    `name` varchar(65) NOT NULL default '',
    `lastname` varchar(65) NOT NULL default '',
    `email` varchar(65) NOT NULL default '',
    PRIMARY KEY (`id`)
    ) TYPE=MyISAM AUTO_INCREMENT=7 ;
    
    -- 
    -- Dumping data for table `test_mysql`
    -- 
    
    INSERT INTO `test_mysql` VALUES (1, 'Billly', 'Blueton', 'bb5@phpeasystep.com');
    INSERT INTO `test_mysql` VALUES (2, 'Jame', 'Campbell', 'jame@somewhere.com');
    INSERT INTO `test_mysql` VALUES (3, 'Mark', 'Jackson', 'mark@phpeasystep.com');
    INSERT INTO `test_mysql` VALUES (4, 'Linda', 'Travor', 'lin65@phpeasystep.com');
    INSERT INTO `test_mysql` VALUES (5, 'Joey', 'Ford', 'fordloi@somewhere.com');
    INSERT INTO `test_mysql` VALUES (6, 'Sidney', 'Gibson', 'gibson@phpeasystep.com');
    You said you wanted to figure out the pagination code yourself, so no pagination code is included.

    A few additional notes:
    I have error_reporting set to include unset variables. That means I have to supress errors resulting from unset variables with the @ sign (@$some_variable). You can leave them in, they won't affect the code.
    Secondly, I have register_globals set to OFF (in the php.ini file) as PHP recommends (security reasons).
    That means variables sent through the URL or POST, will be invisible to the script until you make them available.
    A variable or array named $var, will have to be collected like this $var = $_POST['var'];
    If you have register_globals set to ON, you can still use the code as is.


    multiple_delete.php:
    PHP Code:
    <?php
    $host
    ="localhost"// Host name 
    $username=""// Mysql username 
    $password=""// Mysql password 
    $db_name=""// Database name 
    $tbl_name="test_mysql"// Table name 

    /* set the allowed order by columns */
    $default_sort 'lastname';
    $allowed_order = array ('name''lastname','email');

    /* if order is not set, or it is not in the allowed
     * list, then set it to a default value. Otherwise, 
     * set it to what was passed in. */
    if (!isset ($_GET['order']) || 
        !
    in_array ($_GET['order'], $allowed_order)) {
        
    $order $default_sort;
    } else {
        
    $order $_GET['order'];
    }

    /* connect to db */
    mysql_connect ($host$username$password);
    mysql_select_db ($db_name);

    /* construct and run our query */
    $query "SELECT * FROM $tbl_name ORDER BY $order";
    $result mysql_query ($query) or die(mysql_error());
    $count=mysql_num_rows($result);

    /* make sure data was retrieved */
    $numrows mysql_num_rows($result);
    if (
    $numrows == 0) {
        echo 
    "No data to display!<p>";
        exit;
    }

    /* now grab the first row and start the table */
    $row mysql_fetch_assoc ($result);
    echo 
    "<TABLE border=1>\n";
    ?>
    <tr>
    <td><form name="form1" method="post" action="<?php echo @$PHP_SELF ?>">
    <input type="hidden" name="count" value="<?php echo @$count ?>"/></td></tr>
    <?php
    echo "<TR>\n<td>#</td>";
    foreach (
    $row as $heading=>$column) {
        
    /* check if the heading is in our allowed_order
         * array. If it is, hyperlink it so that we can
         * order by this column */

        
    echo "<TD><b>";
        if (
    in_array ($heading$allowed_order)) {
            echo 
    "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>";
        } else {
            echo 
    $heading;
        }                
        echo 
    "</b></TD>\n";
    }
    echo 
    "</TR>\n";

    /* reset the $result set back to the first row and 
     * display the data */
    mysql_data_seek ($result0);
    while (
    $row mysql_fetch_assoc ($result)) {
        echo 
    "<TR>\n";
    ?>
    <td align="center"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id']; ?>"></td>
    <?php
        
    foreach ($row as $column) {
            echo 
    "<TD>$column</TD>\n";
        }
        echo 
    "</TR>\n";
    }
    echo 
    "<tr><td colspan=\"5\" align=\"center\">\n<input name=\"delete\" type=\"submit\" id=\"delete\" value=\"Delete\">\n</td></tr>\n";
    echo 
    "</TABLE>\n";

    // Check if delete button active, start this 
    if(@$_POST['delete'])
    {
        
    $checkbox $_POST['checkbox']; 
        
    $count count($checkbox);
        for(
    $i=0;$i<$count;$i++)
        {        
            
    $del_id $checkbox[$i];
            
    $sql "DELETE FROM test_mysql WHERE id=$del_id";
            
    $result mysql_query($sql)or die("delete feil: ".mysql_error());
        }

        
    // if successful redirect to delete_multiple.php 
        
    if($result)
        {
            echo 
    "<meta http-equiv='refresh' content='0;URL=delete_multiple.php'>";
        } 
    }
    mysql_close();
    ?>
    </form>
    S. Rosland

  4. #3
    Junior Member
    Join Date
    Apr 2006
    Posts
    3
    Member #
    12986
    Hi rosland,

    Thanks for the response! I will implement this code right now and let you know how it goes. Once again, thank you!

  5. #4
    Junior Member
    Join Date
    Apr 2006
    Posts
    3
    Member #
    12986
    Awesome! It works wonders! Thank you so much rosland!

    Working on the pagination now. Thanks again for everything!


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 12:04 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com