Jump to content

SORTING using isset


phpdumdumb

Recommended Posts

Good evening,  I'm doing a class assignmet where we have to do various sorts getting the info a DB we created

I needed help with my last sort (sort ==6) where I have to display only the featured items I have and sort by primary key.  I pasted the code below Can someone help?

 

Thanks in advance.

 

$sort = $_GET["sort"];



	 	if ($sort == 2)

			$orderby = "ProName ASC";

		else if ($sort == 3)

			$orderby = "Cost ASC";

		else if ($sort == 4)

			$orderby = "CatId ASC";

		else if ($sort == 5)

			$orderby = "Rtng ASC";

		else if ($sort == 6)
		{
			$feature = if(isset($_GET["FeatItm"])) //if(isset($_GET[$name]) && $value==$name."=".$_GET[$name])
			$orderby = "Cost ASC";
		}
		else 
			// Order by primary key (also will be the default if no "sort" value exists in your GET)

			$orderby = "PID ASC";





			$sql = "SELECT * FROM project_products 
					LEFT JOIN project_categories 
					ON project_products.CatId = project_categories.CatId WHERE ".$feature." = 1 
					ORDER BY ".$orderby." LIMIT 0, 30 ";


			$result_products = mysql_query($sql);

			while($products = mysql_fetch_assoc($result_products))
			{

				echo "<div class='product'>";
				echo "	<div class='prod-image'><img src='".$products["ImgURL"]."' /></div>";
				echo "	<div class='prod-info'>";
				echo "		<h3>". $products["ProName"] . "</h3>";
				echo "		<p>". $products["SDesc"] . "</p>";
				echo "		<p>". sprintf("$%01.2f", $products["Cost"]) . "</p>";
				echo "	</div>";
				echo "</div>";
			}

Link to comment
Share on other sites

That would help wouldn't it!  I have a DB with a featured items (FeatItm) column that lists whether a product is featured or not (0 or 1) and I have to only display that on a webpage and sort the primary key.  In my code under sort == 6, I put what I believe would accomplish that, but I'm not sure how to piece it together.

Link to comment
Share on other sites

Your logic is odd, here's how I'd do it

 

<?php

// Define the allowed sorting columns
$allowed_sort_columns = array(
'ProName',
'Cost',
'CatId',
'Rtng'
);

// Check if the given value is valid
if( in_array($_GET['sort'], $allowed_sort_columns) ) {
$sort = $_GET['sort'];
// If it's not valid, set the default
} else {
$sort = 'PID';
}

// Check if direction is valid
if( $_GET['direction'] == 'desc' ) {
$direction = 'DESC';
} else {
$direction = 'ASC';
}

// Check if you only want featured items
if( isset($_GET['featured']) ) {
$featured = TRUE;
} else {
$featured = FALSE;
}

// We'll build the query in chunks, using concatenation (.)
$sql = 'SELECT * FROM project_products
LEFT JOIN project_categories
ON project_products.CatId = project_categories.CatId';
// Check if we need to add featured
if( $featured ) {
$sql .= ' WHERE featured = 1';
}
// Add on sorting
$sql .= ' ORDER BY '.$sort.' '.$direction;

// Display it for testing
echo $sql;

?>

 

Using ?sort=Cost&direction=desc&featured it outputs

SELECT * FROM project_products
LEFT JOIN project_categories
ON project_products.CatId = project_categories.CatId WHERE featured = 1 ORDER BY Cost DESC

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.