Welcome to WebmasterWorld Guest from 107.22.87.205

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

sort row then tick all checkboxes and keep same row sort

     
3:26 pm on Jan 13, 2011 (gmt 0)



Hi,

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:

<?php
//connect to db

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


// 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
<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>

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

<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>

<?php

while ($row = mysql_fetch_row($userreqs)) {
$po_id = $row[0];
?>
<hr>

Display
<table>
<tr>
<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>
</tr>
</table>
<br>
<?php
};
?>

</form>

As you can see the bold and italics are separate.

Any help much appreciated. Cheers.
12:02 pm on Jan 21, 2011 (gmt 0)

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



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))
 

Featured Threads

Hot Threads This Week

Hot Threads This Month