using require statments with if/elseif statements [duplicate]

喜夏-厌秋 提交于 2019-12-12 03:39:29

问题


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';
} 

回答1:


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.




回答2:


Your if statement is not using comparative equal signs or quotes. It should be as follows:

if ($result_array[0] == 'Politics')



回答3:


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']);
}


来源:https://stackoverflow.com/questions/4697751/using-require-statments-with-if-elseif-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!