开发者

Gridview like control in php

Im new to php..I had used GridView control in asp.net for Listing records.

Now I want to use a control in php to list the records with checkbox options to select them for deletion and also page 开发者_如何学JAVAenabled. Can anyone guide a beginner on how to do this?

Thanks in advance.


Your idea consists of two things; a table and a php-file that processes the input from the table.

File: table.php

<form method="post" action="process.php">
   <table>
      <thead> 
         <tr>
           <th>Title</th>
           <th>Activate</th>
           <th>Delete</th>
         </tr>
      </thead>
      <?php foreach( $records as $record ): ?>
         <tr>
            <td><?php echo $record['title']; ?></td>
            <td><input type="checkbox" name="activate_ids[]" value="<?php echo $record['id']; ?>" /></td>
            <td><input type="checkbox" name="delete_ids[]" value="<?php echo $record['id']; ?>" /></td>
         </tr>
      <?php endforeach; ?>
    </table>
 </form>

File: process.php

<?php

    //Gather the $_POST
    $activate_ids = $_POST['activate_ids'];
    $delete_ids = $_POST['delete_ids'];

    //Let's make sure we only get ints
    for($i = 0; $i < count($activate_ids); $i++)$activate_ids[$i] = intval($activated_ids[$i]);        
    for($i = 0; $i < count($delete_ids); $i++)$delete_ids[$i] = intval($delete_ids[$i]);

    //Get the ids
    $activate_ids_string = implode(',', $activate_ids);
    $delete_ids_string = implode(',', $delete_ids);

    //Make sure we don't have '' as a parameter for SQL
    if( $activate_ids_string == '' ) $activate_ids_string = 0
    if( $delete_ids_string == '' ) $delete_ids_string = 0;

    //UPDATE the table
    $sql_activate = 'UPDATE Records SET active=1 WHERE id IN (' . $activate_ids_string . ')';
    $sql_delete = 'DELETE FROM Records WHERE id IN (' . $delete_ids_string . ')';

    //Execute the queries and get the # of affected rows
    $aff_rows_activate = mysql_num_rows(mysql_query($sql_activate));
    $aff_rows_delete = mysql_num_rows(mysql_query($sql_delete));

    //Last but not least, output how it went for both queries
    if( $aff_rows_activate === 1 ) echo 'Activated one record.';
    else echo 'Activated ' , $aff_rows_activate, ' records.';
    if( $aff_rows_delete === 1 ) echo 'Deleted one record.';
    else echo 'Deleted ' , $aff_rows_activate, ' records.';

?>

I haven't had a chance to try out the code above, so there might still be a couple of syntax errors in it - however, you should be able to get an idea of what you need to do to achieve the results that you want.


php is different form asp. asp has many controls whcih u can use but php has core function for core funtionlity. to achive ur goal u have to use table put checkbox in the table with ur data in it and use ajax to delete rows from the table or just post the page on checkbox click. but there is no control for this u have to code it urself, u can say php is like c++. and for paging u can use some class which are find here enter link description here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜