Jump to content

this seems very innefficient


chico1st

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