Jump to content

Archived

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

chico1st

this seems very innefficient

Recommended Posts

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

Share this post


Link to post
Share on other sites
I'd simply built up an array of WHERE clauses, then deal with building the actual select statement after, since order doesn't matter.

Share this post


Link to post
Share on other sites

×

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.