Jump to content

Sorting Issue


dfowler

Recommended Posts

Hey guys, I have a major issue here.  I have two columns that I need to allow for sorting.  Here is my code:

<?php
include 'admin/system.php';
$page = 'FrontEnd';
require('includes/header.php');

$query1 = "SELECT * FROM categories";
$result1 = mysql_query($query1);
$cats = array();
while (($row = mysql_fetch_assoc($result1)) !== false) {
  $cats[] = $row;
}

if (!$_GET['id']) { ?>
<td valign="top" width="400"><span class="subheadbla">Please select a category to view pdf's from...</span><br>

	<form name="form1">
		<select name="categories" ONCHANGE="location=this.options[this.selectedIndex].value;">
		<option selected>---- Choose a Category to Browse ----</option>
				<?php foreach($cats as $c) { ?>
				<option value="http://www.site.com/index.php?id=<?php echo $c['id']; ?>"><?php echo $c['name']; ?></option>
				<?php } ?>
			</select>
	</form>
</td>
<?php
require('includes/footer.php');
} else {
if(!$_GET['sort']) {
$cat_id = $_GET['id'];

$query = "SELECT * FROM cat_pdfs WHERE cat_id='$cat_id'";
$result = mysql_query($query);
$catid = array();
while (($row = mysql_fetch_assoc($result)) !== false) {
	$catid[] = $row;
}

$pdfs=array();
foreach($catid as $c) {
	$query2 = "SELECT * FROM pdfs WHERE id='".$c['pdf_id']."'";
	$result2 = mysql_query($query2);
	while (($row2 = mysql_fetch_assoc($result2)) !== false) {
		$pdfs[] = $row2;
	}
}
} else {
$sort = $_GET['sort'];
$count = $_GET['count'];
$cat_id = $_GET['id'];

$query = "SELECT * FROM cat_pdfs WHERE cat_id='$cat_id'";
$result = mysql_query($query);
$catid = array();
while (($row = mysql_fetch_assoc($result)) !== false) {
	$catid[] = $row;
}

$pdfs=array();
foreach($catid as $c) {
$query2 = "SELECT * FROM pdfs WHERE id='".$c['pdf_id']."'";
$result2 = mysql_query($query2);
	while (($row2 = mysql_fetch_assoc($result2)) !== false) {
		$pdfs[] = $row2;
	}
}
$count++;
}
if (!$_GET['sort']){
$count=1;
}
?>
<td valign="top" width="400"><span class="subheadbla">Please select a category to view pdf's from...</span><br>
  		
	<form name="form1">
		<select name="categories" ONCHANGE="location=this.options[this.selectedIndex].value;">
		<option selected>---- Choose a Category to Browse ----</option>
				<?php foreach($cats as $c) { ?>
				<option value="http://www.site.com/index.php?id=<?php echo $c['id']; ?>"><?php echo $c['name']; ?></option>
				<?php } ?>
			</select>
	</form>

	<div id="pdfs">
		<table width="400" class="txtbl2" >
			<tr>
				<td width="60" bgcolor="#EBEBEB"><a href="http://www.site.com/index.php?id=<?php echo $id; ?>&sort=date&count=<?php echo $count; ?>">Date</a></td>
				<td bgcolor="#EBEBEB"><a href="http://www.site.com/index.php?id=<?php echo $id; ?>&sort=name&count=<?php echo $count; ?>">Title</a></td>
			</tr>
		<?php 
		$style_count=1;
			if($sort == name){
				if($count&1){
					array_multisort($pdf, SORT_ASC);
				} else {
					array_multisort($pdf, SORT_DESC);
				} 
			}
			if($sort == date){
				if($count&1){
					array_multisort($pdf, SORT_ASC);
				} else {
					array_multisort($pdf, SORT_DESC);
				} 
			}
		foreach($pdfs as $p) {
			if($style_count&1) { ?>
				<tr>
					<td><?php echo $date = date("n/d/y", strtotime($p['date'])); ?></td>
					<td><a href="<?php echo $p['location']; ?>" target="_blank"><?php echo $p['name']; ?></a></td>					
				<tr>
			<?php } else { ?>
				<tr>
					<td bgcolor="#DDF3FF"><?php echo $date = date("n/d/y", strtotime($p['date'])); ?></td>
					<td bgcolor="#DDF3FF"><a href="<?php echo $p['location']; ?>" target="_blank"><?php echo $p['name']; ?></a></td>					
				<tr>
			<?php }
		$style_count++;
		} ?>
		</table>
	</div>
</td>
<?php
require('includes/footer.php');
}
?>

 

You can see where I've tried to get the sorting to work, but it isn't at all.  Any help would be GREATLY appreciated as I am running into a deadline to get this finished.  Thanks!

Link to comment
https://forums.phpfreaks.com/topic/100338-sorting-issue/
Share on other sites

I managed to fix the issue myself, here is what I did:

if($sort == name){
foreach($pdf as $r){
	$column[] = $r['name'];
}
if($count&1){
	array_multisort($column, SORT_ASC, $pdf);
} else {
	array_multisort($column, SORT_DESC, $pdf);
} 
}
if($sort == date){
foreach($pdf as $r){
	$column[] = $r['date'];
}
if($count&1){
	array_multisort($column, SORT_ASC, $pdf);
} else {
	array_multisort($column, SORT_DESC, $pdf);
}  
}

Link to comment
https://forums.phpfreaks.com/topic/100338-sorting-issue/#findComment-513072
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.