How do I use a PHP If... statement inside of a while... statement? Please try to help!?

amadeus93lee

New member
I need to first get data from a mysql database, but the problem comes when I try to retrieve the data, and filter it out. I only want articles in section1, category 1 to appear on this page, but I don't know how to filter those out from all the others. Here is my existing code so far. If some of it looks broken up, that is because of formatting I did to it so it could fit into this column...

<?php
$query="SELECT * FROM content";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$title=mysql_result
($result,$i,"title");

$date=mysql_result
($result,$i,"date");

$creator=mysql_result
($result,$i,"creator");

$intro=mysql_result
($result,$i,"intro");

$body=mysql_result
($result,$i,"body");

$published=mysql_result
($result,$i,"published");

$access=mysql_result
($result,$i,"access");


echo "<b>$title</b><br>$intro<br>$body
<br>Created by $creator on $date<hr><br>";

$i++;
}

?>
 
The best thing for you to do is use the WHERE Clause for your statement. For instance
$query="SELECT * FROM content WHERE section='section1' AND category='category1'";

But if you really want to use a if statement to exclude info you would just follow something like this.
if($section = 'section1' && $category = 'category1') {
print 'data goes here';
}
 
Theres a few ways you could do that. One way is mentioned above so I will mention another option. You could limit it from the query itself for example:

$query="SELECT * FROM content WHERE section=1 AND category=1";

If you wanted to take it further and show most recent articles first you could do:

$query="SELECT * FROM content WHERE section=1 AND category=1 ORDER BY date DESC";
 
Back
Top