Jump to content


Photo

this seems very innefficient


  • Please log in to reply
1 reply to this topic

#1 chico1st

chico1st
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 21 August 2006 - 09:07 PM

this works but can i make this where command less.. gigantic?

<?php

//add a where condition of a certain author/topic/both
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 = "";
}
}


//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 " .
        "WHERE news.authorID = author.authorID AND topic.topicID = news.topicID AND news.dateID = date.dateID $where " .
"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;
  ?>

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 21 August 2006 - 11:37 PM

I'd simply built up an array of WHERE clauses, then deal with building the actual select statement after, since order doesn't matter.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users