chico1st Posted August 21, 2006 Share Posted August 21, 2006 this works but can i make this where command less.. gigantic?<?php //add a where condition of a certain author/topic/both [b]if($postAuthor = $_POST['author']){ if($postTopic = $_POST['topic']){ $where = "AND news.authorID = '$postAuthor' AND news.topicID = '$postTopic'"; }else{ $where = "AND news.authorID = '$postAuthor'"; } }else{ if($postTopic = $_POST['topic']){ $where = "AND news.topicID = '$postTopic'"; }else{ $where = ""; } }[/b] //add an ORDER BY condition of author/date/topic if($_POST['sort'] == 'author'){ $sort = "author,"; }elseif($_POST['sort'] == 'topic'){ $sort = "topic,"; } $query = "SELECT news.newsID, news.name AS newsName, news.abstract, " . "date.name AS date, topic.name AS topic, author.name AS author " . "FROM news, date, topic, author " . [b]"WHERE news.authorID = author.authorID AND topic.topicID = news.topicID AND news.dateID = date.dateID $where " .[/b] "ORDER BY $sort date"; $result = mysql_query($query) or die($query."<br />\n".mysql_error()); if(mysql_num_rows($result) == 0){ $output = "Database is empty <br>"; }else{ $output = "<table>"; while($row = mysql_fetch_assoc($result)){ $newsid = $row['newsID']; $title = $row['newsName']; $abstract = $row['abstract']; $date = $row['date']; $topic = $row['topic']; $author = $row['author']; $output .= "<tr><td>"; $output .= "<a href='news_article.php?id=$newsid'>$title</a><br>"; $output .= "<em>$abstract</em></td>"; $output .= "<td>$topic</td>"; $output .= "<td>$author</td>"; $output .= "<td>$date</td>"; $output .= "</tr>"; } $output .= "</table>"; } print $output; ?> Quote Link to comment https://forums.phpfreaks.com/topic/18239-this-seems-very-innefficient/ Share on other sites More sharing options...
fenway Posted August 21, 2006 Share Posted August 21, 2006 I'd simply built up an array of WHERE clauses, then deal with building the actual select statement after, since order doesn't matter. Quote Link to comment https://forums.phpfreaks.com/topic/18239-this-seems-very-innefficient/#findComment-78341 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.