[英]Display Results from query in a select tag in wordpress page?
I have a WordPress page that connects to an external database from WordPress using the following code:我有一个 WordPress 页面,它使用以下代码从 WordPress 连接到外部数据库:
$my_wpdb = new wpdb('me', 'password', 'database', 'localhost');
$myrows = $my_wpdb->get_results( "SELECT Name FROM testing" );
...then i use print_r($myrows);
...然后我使用print_r($myrows);
and get the following:并获得以下信息:
Array
(
[0] => stdClass Object (
[Name] => Jesus
)
[1] => stdClass Object (
[Name] => James
)
[2] => stdClass Object (
[Name] => Matt
)
)
Now I need to output the names inside those objects in a select tag using php.现在我需要使用 php 在 select 标签中输出这些对象内的名称。
Any help would be appreciated.任何帮助,将不胜感激。
There you go:你去吧:
<select>
<?php foreach ($myrows as $myrow) : ?>
<option value=""><?php echo $myrow->Name; ?></option>
<?php endforeach; ?>
</select>
Also, by the looks of your table it might be easier to just use WordPress get_col()
function, which works pretty much the same way as get_results
but should directly return "Name".此外,从您的表的外观来看,使用 WordPress get_col()
函数可能更容易,它的工作方式与get_results
几乎相同,但应该直接返回“名称”。
EDIT - Maybe the [insertphp] plugin likes this version better:编辑- 也许 [insertphp] 插件更喜欢这个版本:
<select>
[insertphp]
foreach ($myrows as $myrow) {
echo '<option>' . $myrow->Name . '</option>';
}
[/insertphp]
</select>
Also, please provide any error info that might help troubleshoot the problem otherwise.此外,请提供任何可能有助于解决问题的错误信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.