foreach数组保持返回第一个值?

首先,我不知道php,我当然不知道sql或wordpress如何与它交互, 但我必须使用PHP等所以我在这里。 所以请尽量清楚简单地解释一下。 </ p>

我在sql wordpress表中添加了三个值。 两个值共享相同的room_id(1),第三个值的值为(2)。 它们每个在表中都有不同的开始和结束日期。 在尝试移动到下一个房间之前,我首先尝试遍历每个room_id,检查日期。 我有更多的代码与此功能,但基本上我的问题是我似乎只是每次迭代打印出数组的第一个值。 这是代码:</ p>

 &lt;?php 
/ 插件名称:HotelMania函数 /
function checkRoom(){

全局$ wpdb;
$ query =“SELECT * FROM hm_booking ORDER BY room_id DESC”;
$ rooms = $ wpdb-&gt; get_results($ query);

foreach($ rooms as $ room){
$ test = $ wpdb-&gt; get_var(“SELECT room_id FROM hm_booking”);
echo $ test;
}
}
add_shortcode('TEST', 'checkRoom');
?&gt;
</ code> </ pre>

当我尝试使用room_id或start_date测试值时,我一直在表中获得第一个值。 但我认为我的sql是正确的,因为它在数据库中工作。 请有人帮我看看,似乎无法弄清楚我做错了什么。 </ p>

提前致谢。 </ p>
</ div>

展开原文

原文

First of all I don't know php and I certainly don't know sql or how wordpress interacts with it, but I have to use php etc so here I am. So please try to explain things clearly and simply.

I have added three values to my sql wordpress tables. Two values share the same room_id (1) and the third has a value of (2). They each have a different start and end date in the table. I am trying to loop through each room_id first, checking the dates, before moving to the next room. I have more code with this function but essentially my problem is that I only seem to be printing out the first value of the array with each iteration. Here is the code:

<?php 
/*Plugin Name: HotelMania Functions*/
function checkRoom() {
    global $wpdb;
    $query = "SELECT * FROM hm_booking ORDER BY room_id DESC";
    $rooms = $wpdb->get_results($query);    
    foreach ($rooms as $room) {
       $test= $wpdb->get_var("SELECT room_id FROM hm_booking");
       echo $test;
}    
}
add_shortcode('TEST', 'checkRoom');
?>

When I try to test the value either using room_id or start_date I keep getting hte first value in the table. But I think my sql is right because it is working in the database. Please can someone help I have read around and can't seem to figure out what I am doing wrong.

Thanks in advance.

2个回答



get_results </ code>获得 $ rooms </ code>后,您不需要 使用 get_var </ code>运行另一个查询。</ p>

例如,获取 room_id </ code>并将其放入 $ test </ code >,而不是</ p>

  $ test = $ wpdb-&gt; get_var(“SELECT room_id FROM hm_booking”); 
</ code> </ pre>

做</ p>

  $ test = $ room-&gt; room_id; 
</ code> </ pre>
</ div>

展开原文

原文

Once you got $rooms from get_results, you don't need to run another query with get_var.

For example, to get the room_id and place it into $test, instead of

$test= $wpdb->get_var("SELECT room_id FROM hm_booking");

do

$test = $room->room_id;

drc15469
drc15469 你是个传奇先生!
6 年多之前 回复
douji9184
douji9184 啊,所以通过测试来确保事情正在做他们意味着什么不是我摔倒并降落在我的脸上。
6 年多之前 回复



您正在使用的SQL语句“SELECT room_id FROM hm_booking”</ code>以及 get_var() </ code>无论如何都会返回相同的值。 codex 提到:</ p>


< p> get_var函数从数据库返回单个变量。 </ p>
</ blockquote>

假设 hm_booking </ code>表包含多行,那么您运行的SQL语句将返回多行 表中的room_id </ code>列, get_var()</ code>函数只返回 room_id </ code>列的第一行。</ p>

< p>另外,请考虑利用 get_results()</ code> 。 您的第一个SQL查询可能已经存储了您需要的所有内容。 $ rooms </ code>变量是一个对象,包含 SELECT </ code>查询中的所有行和列:</ p>

  $ query =“  SELECT * FROM hm_booking ORDER BY room_id DESC“; 
$ rooms = $ wpdb-&gt; get_results($ query);

foreach($ rooms as $ room){
echo'Room ID:'。 $间 - &GT; ROOM_ID; // $ room对象表示单行
}
</ code> </ pre>
</ div>

展开原文

原文

The SQL statement you're using "SELECT room_id FROM hm_booking" along with get_var() will return the same value no matter what. The codex mentions that:

The get_var function returns a single variable from the database.

Assuming the hm_booking table contains multiple rows, then the SQL statement you're running will return multiple rows of just the room_id column in your table, and the get_var() function will only return the first row of the room_id column.

Also, consider taking advantage of get_results(). Your first SQL query may already be storing everything you need. The $rooms variable is an object containing all the rows and columns from your SELECT query:

$query = "SELECT * FROM hm_booking ORDER BY room_id DESC";
$rooms = $wpdb->get_results($query);    
foreach ($rooms as $room) {
    echo 'Room ID:' . $room->room_id; // $room object represents a single row
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐