homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Gold Sponsor 2015!
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

sort row then tick all checkboxes and keep same row sort

Msg#: 4252869 posted 3:26 pm on Jan 13, 2011 (gmt 0)


I've created a page where you can select a row by single checkbox or by ticking all the rows checkboxes, pretty normal and works ok. I then created a sort by poid and sort by asc /desc function which again is ok.

The problem is I don't know how to get them working together. For instance the page displays first time with an ORDER BY itemno ASC clause. I then apply the sort by asc / desc on the poid col which resorts the display.

Heres my problem, if I then click the checkall checkboxes url the page is sent to itself and picks up the initial ORDER BY itemno ASC clause hence resorting my poid sort and causing my problem.

I don't think I'm trying to do anything impossible and a good example is phpMyAdmin. You view the table, apply a column name sort and click Check All and it doesn't resort the display.

Heres my code which does the above:

//connect to db

if ($_GET['action']=="checked") {
if ($_GET['action']=="uncheck") {

// Initial itemno sort
$reqquery = "SELECT * FROM `porders` WHERE `username` = 'abc' ORDER BY itemno ASC ";
$userreqs = mysql_query($reqquery) or die(mysql_error());

//Check if gosort and pick up the poid and asc
if ($_POST['gosort']) {
$colsort_array = isset($_POST['colsort']) && is_array($_POST['colsort'])? $_POST['colsort']: array();
foreach($colsort_array as $key => $colchoice) {
if ($colchoice == 'poid') {

$adsort_array = isset($_POST['ascdescsort']) && is_array($_POST['ascdescsort'])? $_POST['ascdescsort']: array();
foreach($adsort_array as $key => $ascdescsort) {
if ($ascdescsort == 'asc') {
$reqsort = "SELECT * FROM `porders` WHERE `username` = 'abc' ORDER BY poid ASC ";
$userreqs = mysql_query($reqsort) or die(mysql_error());

<form method="POST" action="checkboxwithsort.php">
Sort by
<select bgcolor="#ff6" class="input" name="colsort[<?php echo $po_id;?>]">
<option value="poid">POID</option>

<select bgcolor="#ff6" class="input" name="ascdescsort[<?php echo $po_id;?>]">
<option value="asc">Asc</option>
<option value="desc">Desc</option>

<input type="submit" class="submit" name="gosort" id="gosort" value="Sort">
<a href="checkboxwithsort.php?action=checked">Check all</a>
<a href="checkboxwithsort.php?action=unchecked">Uncheck all</a>


while ($row = mysql_fetch_row($userreqs)) {
$po_id = $row[0];

<td>PO ID</td>
<td><?php echo $po_id;?></td>
<td><input type="checkbox" name="chkbox[<?php echo $po_id; ?>]" value="" <?php echo $tick; ?> /></td>


As you can see the bold and italics are separate.

Any help much appreciated. Cheers.



WebmasterWorld Senior Member vincevincevince us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 4252869 posted 12:02 pm on Jan 21, 2011 (gmt 0)

The results will be in the $_POST array as $_POST[chkbox][][]

So, in your output of the <input box, use:

<td><input type="checkbox" name="chkbox[<?php echo $po_id; ?>]" value="1" <?php echo (($_POST['chkbox'][$po_id])?('checked'):('')); ?> /></td>

To retain your 'tick all' functionality, just use || (or) logic in the inline if statement ((if)?(then):(else))

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved