Jump to content

If Statement Problems


EternalSorrow

Recommended Posts

Currently I'm setting up an article database which has the option of two categories for each article.  The two database fields for the different categories are 'category' and 'sub_category.'

 

The page for the articles shows the articles with their categories below.  A visitor is able to click on the category title, which then leads them to a page showing all articles in that category.

 

However, I'm unable to figure out how to point the different category field names to the same page using 'if' and 'if else' statements to retrieve the correct articles.  For an example, when the article has two categories and one of the categories is clicked, it will retrieve articles which correspond to both categories, when it is only supposed to retrieve articles for that single clicked category.  I've tried using several variations of the 'if', 'else if', and 'else' commands but nothing has worked.

 

I'd be grateful for any point in the right direction.

 

Here's the code for the article page:

<?php
mysql_connect(localhost,username,password);
@mysql_select_db(database) or die( "Unable to select database");

$query = "SELECT * from history ORDER BY id desc LIMIT 10";
$result = mysql_query($query) or die(mysql_error());

while($row=mysql_fetch_array($result))
{
extract($row);

$sub = '';

if (strlen($sub_category) >= 1) { 
$sub = ", <a href=\"categories.php?sub_category=$sub_category\">$sub_category</A>"; 
}

echo '<p><blockquote><div class="title">'.$title.'</div>
'.$info.'
<div class="cat">Categories: <a href="categories.php?category='.$category.'">'.$category.'</A>'.$sub.'</div>
</blockquote>';

}

?>

 

And here's the code for the categories.php page the article page points to:

<?php

if (!is_numeric($_GET["category"]) && !empty($_GET["category"]) && $_GET["category"]!="")
{
$category = $_GET["category"];
}

else if (!is_numeric($_GET["sub_category"]) && !empty($_GET["sub_category"]) && $_GET["sub_category"]!="")
{
$sub_category = $_GET["sub_category"];
}

mysql_connect(localhost,username,password);
@mysql_select_db(database) or die( "Unable to select database");

$query="SELECT * FROM `history` WHERE `category` = '$category' OR `sub_category` = '$sub_category' ORDER BY title ";
$result = mysql_query( $query ) or die(mysql_error());

while($row=mysql_fetch_array($result))
{
extract ($row);

echo '<li><a href="article.php?id='.$id.'">'.$title.'</A>';

}

?>

Link to comment
https://forums.phpfreaks.com/topic/139157-if-statement-problems/
Share on other sites


$query="SELECT * FROM `history` WHERE `category` = '$category' OR `sub_category` = '$sub_category' ORDER BY title ";
$result = mysql_query( $query ) or die(mysql_error());

 

Have you tried a AND instead of a OR mate trie.

 

I have tried the AND instead of OR, but then the $sub_category options, when clicked, show an empty page and the $category options show the $sub_category options.

I've also realized that when the same name which has been assigned to the $sub_category is placed in the $category field, the name will not call the article which has been assigned the $sub_category.

 

For example, the category 'Film' is in both the $category and $sub_category fields, and when the link is pressed when it is in either field, the other field will not be searched.

Archived

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

×
×
  • 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.