简体   繁体   English

在PHP中使用HTML Select Tag中的数据库输出

[英]Using database output in HTML Select Tag with PHP

I have the following working perfectly on a wordpress site. 我的以下内容在wordpress网站上运行良好。

<?php 

global $wpdb;

$results = $wpdb->get_results( "SELECT post_title FROM wp_posts WHERE post_type = 'show' AND post_status = 'publish' ORDER BY post_title ASC" );
$options = array();
foreach ($results as $result) {
$options[$result->post_title] =  $result->post_title;
}

echo '<select class="field" name="djshow">';
foreach ($options as $key => $value) {
echo "<option value='$key'" . ( $_GET['show'] == $value ? " selected='selected'" : "" ) . ">$value</option>";
}
'</select>';

?>

It searches the database for every instance of post_title from wp_posts where the post_type is show and the post_status is public . 它从wp_posts中搜索post_title的每个实例的数据库,其中post_typeshowpost_statuspublic It then sorts it and outputs it to the array before populating a Select tag with a list of every post_title that matches. 然后对它进行排序,并将其输出到数组,然后用一个匹配的每个post_title列表填充Select标记。

At the moment it outputs the post_title to both the option value and name fields.... 目前,它会将post_title输出到选项值和名称字段。

What I would like to do is select another field ID in the original query to populate the option value while the post_title populates the name. 我想做的是在原始查询中选择另一个字段ID ,以填充选项值,而post_title填充名称。

So the select would read something like this when rendered in html : 因此,在html中呈现时,select会读取如下内容:

<option value="15">Post Title 1</option>
<option value="18">Post Title 2</option>
<option value="23">Post Title 3</option>
etc...

I think the correct query I need to use is this (but correct me if i'm wrong) 我认为我需要使用的正确查询是这样(但如果我错了,请更正我)

$results = $wpdb->get_results( "SELECT post_title,ID FROM wp_posts WHERE post_type = 'show' AND post_status = 'publish' ORDER BY post_title ASC" );

But that's where my limited knowledge let's me down... 但这就是我有限的知识让我失望的地方...

How would I assign the output of ID to the option value and post_title to the name of the select tag? 如何将ID的输出分配给选项值,并将post_title分配给select标记的名称?

When you query for several values you can use a comma to separate them. 当查询多个值时,可以使用逗号分隔它们。 In your code 在你的代码中

post_title AND ID

should be 应该

post_title, ID

Your return value are in $results, your foreach could be 您的返回值以$ results为单位,您的foreach可能是

foreach ($results as $result) {
echo "<option value='$result->ID'" . ( $_GET['show'] == $result->ID ? " selected='selected'" : "" ) . ">$result->post_title</option>";
}

not sure if your $_GET holds the post_title or the ID 不知道您的$ _GET是否持有post_title或ID

After changing the SQL to include both post_title and ID you really only need to fix this part: 更改SQL以同时包含post_titleID您实际上只需要修复此部分:

foreach ($results as $result) {
  $options[$result->post_title] =  $result->post_title;
}

Which should now be: 现在应该是:

foreach ($results as $result) {
  $options[$result->ID] =  $result->post_title;
}

Because in the next foreach what you have in $key is coming from what you put in the brackets there, and $value from the right side of the assignment. 因为在下一个foreach中, $key内容来自于放在方括号中的内容,而$value来自赋值右侧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM