phpdumdumb Posted June 16, 2012 Share Posted June 16, 2012 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/264276-sorting-using-isset/ Share on other sites More sharing options...
trq Posted June 16, 2012 Share Posted June 16, 2012 You forgot to describe your problem. Quote Link to comment https://forums.phpfreaks.com/topic/264276-sorting-using-isset/#findComment-1354316 Share on other sites More sharing options...
phpdumdumb Posted June 16, 2012 Author Share Posted June 16, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264276-sorting-using-isset/#findComment-1354320 Share on other sites More sharing options...
xyph Posted June 16, 2012 Share Posted June 16, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/264276-sorting-using-isset/#findComment-1354366 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.