using require statments with if/elseif statements [duplicate]
Possible Duplicate:
PHP Question: How to fix these if/elseif statements
Hello is there a way to write if/ elseif statements to display multiple php pages using require statements. I am currently collecting articles from blogs using rss and displaying the links on a web page. How can I display a certain feed depending on an array value selected 开发者_开发百科from a mysql database. Sorry this is a lot, hope I explained my question well. Here is the code on the display page:
$query = "SELECT interests FROM signup WHERE username = '$username'";
$result = mysql_query($query) or die ("no query");
$result_array = array();
while($row = mysql_fetch_array($result))
{
$result_array[] = $row['interests'];
} if ($result_array[0] = Politics) {
require 'politics.php';
}
You have a single =
and Politics isn't quoted. Change you last few lines to look like this:
if ($result_array[0] == 'Politics') {
require 'politics.php';
}
Also, as a side note, you should be sure to correctly escape the $username
in your query variable to prevent SQL injection attacks.
Your if statement is not using comparative equal signs or quotes. It should be as follows:
if ($result_array[0] == 'Politics')
Smells somewhat of a bad design. You'd be better off storing users in a user table, their interests in a seperate table, and the master list of interests in a 3rd table.
user: (id, name, etc...)
user_interests (user_id, interest_id)
interests (id, name, filename)
Then you could trivially do something like:
SELECT interests.filename, interests.name
FROM interests
RIGHT JOIN user_interests ON interests.id = user_interests.interest_id
and
while($row = msyql_fetch... ) {
include($row['filename']);
}
精彩评论