Reputation: 199
I have the following chunk of code to do one of several things, the most important being to set up the main loop from which we will grab db contents (depending on conditions).
These conditions are: whether or not we are looking at a category - whether or not we are looking at an ID - whether or not we are looking at neither (the front page) - whether or not the admin password/username combination is used (for deleting/editing purposes).
$replies is grabbing (if we're looking at an ID) all entries that have a PARENT of the ID being called. $quickinfo is used for setting things like meta tags (title, keywords, etc.)
My only (current) question being: is this efficient? If not, why not.
$selection = "ID,CONTENT,IP,SUBJECT,CATEGORY,APPROVED,DATE,PARENT,
PASSWORD,USERNAME,THANKS,DISAPPROVE,IPS,BUMPS";
$id = strip_tags($id);
$category = strip_tags($category);
$threads = mysql_query("SELECT COUNT(*) FROM $board") or die();
list($threadsTotal) = mysql_fetch_row($threads);
$threadsTotal_pages = ceil($threadsTotal / $POSTSPERPAGE);
$threadsPage = intval(@$_GET["page"]);
if (0 == $threadsPage)
{
$threadsPage = 1;
}
$threadsStart = $POSTSPERPAGE * ($threadsPage - 1);
$threadsMax = $POSTSPERPAGE;
if ($category > "" && $id == ""
&& $passw != <ONE MISTAKE HERE> $adminPass
&& $username != <ANOTHER MISTAKE HERE> $adminID)
{
$threads = mysql_query("SELECT $selection FROM $board WHERE PARENT=0
AND CATEGORY='$category'
ORDER BY ID DESC LIMIT $threadsStart, $threadsMax");
}
if ($category == "" && $id == "" &&
$passw !== <NOT EQAULS DOES NOT REQUIRE TWO EQUAL SIGNS AGAIN>
$adminPass && $username != <AND AGAIN> $adminID)
{
$threads = mysql_query("SELECT $selection FROM $board WHERE PARENT=0
ORDER BY ID DESC LIMIT $threadsStart, $threadsMax");
}
// PLEASE CHECK THE NOT EQUALS FUTHER ON....
if ($id > "" && $passw != $adminPass && $username != $adminID)
{
$threads = mysql_query("SELECT $selection FROM $board WHERE PARENT=0
AND ID=$id LIMIT 1");
$quickinfo = mysql_query("SELECT COUNT(*) FROM $board") or die();
$quickinfo = mysql_query("SELECT ID,CONTENT,SUBJECT,CATEGORY,USERNAME FROM
$board WHERE PARENT=0 AND ID=$id LIMIT 1");
$replies = mysql_query("SELECT COUNT(*) FROM $board") or die();
$replies = mysql_query("SELECT $selection FROM $board WHERE PARENT=$id
ORDER BY ID ASC");
while (list( $ID, $CONTENT, $SUBJECT, $CATEGORY, $USERNAME) =
mysql_fetch_row($quickinfo))
{
$threadID = $ID;
$threadContent = $CONTENT;
$threadSubject = $SUBJECT;
$threadCategory = $CATEGORY;
if ($USERNAME > "")
{
$threadAuthor = $USERNAME;
}
elseif ($USERNAME == "")
{
$threadAuthor = "Anonymous";
}
}
} // That is the end of your if
if ($passw == $adminPass && $username == $adminID)
{
$threads = mysql_query("SELECT $selection FROM $board ORDER
BY APPROVED DESC LIMIT $threadsStart,
$threadsMax");
}
Upvotes: 0
Views: 178
Reputation: 1034
I think I see one issue. I believe you really want this
if ($category > "" && $id == ""
&& $passw != <ONE MISTAKE HERE> $adminPass
&& $username != <ANOTHER MISTAKE HERE> $adminID)
{
to be this
if ($category > "" && $id == ""
&& ($passw != <ONE MISTAKE HERE> $adminPass
|| $username != <ANOTHER MISTAKE HERE> $adminID))
{
or possibly, you might want another || in place of another &&, I am not completely certain of the logic there of the category and ID.
Otherwise, you only query the DB once on the page which seems like a pretty efficient setup.
Upvotes: 0
Reputation: 59987
Matt- Reformatted code and also spotted that you use '!==' for '!-'
Also you should consider using mysqli or POD. mysql library for PHP is deprecated.
Upvotes: 1