[英]Use an if statement with mysql_query?
I have a news table and I want to echo out a different statement depending on what type of news it is. 我有一个新闻表,我想根据它是哪种新闻来回显不同的声明。 The type is stored in the field named "type" in the table. 该类型存储在表中名为“ type”的字段中。
Here's the basic code I'm using to return the data to an array 这是我用来将数据返回到数组的基本代码
$query = mysql_query("SELECT * FROM news");
while ($row = mysql_fetch_assoc($result)) {
}
So at the moment I'm returning all the information from the table 'news' but I want to echo out different statements depending on the type within the row. 因此,目前我将从表“ news”返回所有信息,但我想根据行中的类型回显不同的语句。
The purpose of the table is to display user submitted news with different pre-defined text based on the type field. 该表的目的是根据类型字段以不同的预定义文本显示用户提交的新闻。
To give an example of what I mean: if the row contains the type "release_date" echo out ''name' will be released on". If the row contains the type of new_video echo "'name' has a new video". It also then needs to be ordered by the submission date which is also contained in the table. 举例说明:如果该行包含类型“ release_date”,则回显“名称”,如果该行包含new_video回显“名称”,则显示“新视频”。然后还需要在表格中包含的提交日期之前进行排序。
I don't want to do this with seperate queries because I need to arrange all of the news by submission date. 我不想使用单独的查询来执行此操作,因为我需要按提交日期排列所有新闻。 Would the best way of doing this be with an if statement? 最好的方法是使用if语句吗? I'm a bit lost for ideas at the moment, any advice will be really appreciated. 目前,我对创意有些迷失,任何建议将不胜感激。 Hope this makes sense 希望这有意义
To order the results by date you could extend your query like this 要按日期排序结果,您可以像这样扩展查询
SELECT * FROM news ORDER BY `submission_date` DESC //or "ASC" for opposite order
by using your column name. 通过使用您的列名。 To handle different results 处理不同的结果
while ($row = mysql_fetch_assoc($result)) { //for each returned row
switch($row['type']){ //your column name for field
case 'release_date':
echo $row['name']." will be released on";
break;
case 'new_video':
echo $row['name']." has a new video";
break;
//other cases
default: //if there should be a process for "anything else"-cases
//code
break;
}
}
edit: 编辑:
"echo" at this point won't make you happy; 此时的“回声”不会使您感到高兴; I guess you want to output the results in html; 我想你想用html输出结果; then you have to define a variable before the loop starts and fill it inside the loop. 那么您必须在循环开始之前定义一个变量,并将其填充到循环中。 Like this: 像这样:
$html = ''; //set variable
while ($row = mysql_fetch_assoc($result)) { //for each returned row
switch($row['type']){ //your column name for field
case 'release_date':
$html .= "<p>".$row['name']." will be released on</p>";
break;
case 'new_video':
$html .= "<p>".$row['name']." has a new video</p>";
break;
//other cases
default: //if there should be a process for "anything else"-cases
//code
break;
}
}
echo $html;
/*$html is now filled with some paragraphs.
You should echo it inside a complete html-structure
with doctype and inside the body-tag.
The position of the echo inside you script depends on the rest of your code. */
Because you're inserting userdefined content make sure that the inputs are 由于您要插入用户定义的内容,因此请确保输入的内容是
And please notice eggyal's comment, I've just taken your code. 并且请注意eggyal的评论,我刚刚接受了您的代码。 Do not use mysql_query. 不要使用mysql_query。
You could do this in your PHP code. 您可以在PHP代码中执行此操作。 If you want to do this in the SQL query, then you can use CASE
for this: 如果要在SQL查询中执行此操作,则可以为此使用CASE
:
SELECT CONCAT('\'', name, '\' ',
CASE type
WHEN 'release_date' THEN 'will be released on'
WHEN 'new_video' THEN 'has a new video'
ELSE 'did something unexpected'
END)
FROM news
ORDER BY submission_date DESC
Note that the use of CONCAT
is just an example. 请注意,使用CONCAT
只是一个示例。 You could still combine the text fragments in PHP. 您仍然可以在PHP中组合文本片段。 Or you could have a CASE
on the outside, and different CONCAT
expressions in each of the THEN
clauses. 或者,您可以在外部使用CASE
,并在每个THEN
子句中使用不同的CONCAT
表达式。 Depends on what exactly you need, whether you always want a quoted name up front for example. 取决于您究竟需要什么,例如是否始终要始终使用带引号的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.