- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我想到的,它似乎适用于返回一条记录,但是如果数据库中有多个记录(wp_hud_display.id),它会破坏 XML,“foreach”会在while 循环?我如何将它添加到我的代码中?
<?php
// create a dom document with encoding utf8
$domtree = new DOMDocument('1.0', 'UTF-8');
//database configuration
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "root";
$config['mysql_pass'] = "";
$config['db_name'] = "tasmanhud";
//connect to host
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
//select database
@mysql_select_db($config['db_name']) or die( "Unable to select database");
//select all items in table
$sql = "SELECT
wp_hud_display.id,
wp_hud_display.name,
wp_hud_display.resolution,
wp_hud_display.user_id,
wp_hud_display.instrument_id_1,
wp_hud_display.x_loc_1,
wp_hud_display.y_loc_1,
wp_hud_display.layer_1,
wp_hud_display.instrument_id_2,
wp_hud_display.x_loc_2,
wp_hud_display.y_loc_2,
wp_hud_display.layer_2,
wp_hud_display.instrument_id_3,
wp_hud_display.x_loc_3,
wp_hud_display.y_loc_3,
wp_hud_display.layer_3,
wp_hud_display.instrument_id_4,
wp_hud_display.x_loc_4,
wp_hud_display.y_loc_4,
wp_hud_display.layer_4,
wp_hud_instrument.inst_id AS inst1_id,
wp_hud_instrument.name AS inst1_name,
wp_hud_instrument.image_file AS inst1_image,
wp_hud_instrument.font_type AS inst1_ft,
wp_hud_instrument.font_size AS inst1_fs,
wp_hud_instrument_1.inst_id AS inst2_id,
wp_hud_instrument_1.name AS inst2_name,
wp_hud_instrument_1.image_file AS inst2_image,
wp_hud_instrument_1.font_type AS inst2_ft,
wp_hud_instrument_1.font_size AS inst2_fs,
wp_hud_instrument_2.inst_id AS inst3_id,
wp_hud_instrument_2.name AS inst3_name,
wp_hud_instrument_2.image_file AS inst3_image,
wp_hud_instrument_2.font_type AS inst3_ft,
wp_hud_instrument_2.font_size AS inst3_fs,
wp_hud_instrument_3.inst_id AS inst4_id,
wp_hud_instrument_3.name AS inst4_name,
wp_hud_instrument_3.image_file AS inst4_image,
wp_hud_instrument_3.font_type AS inst4_ft,
wp_hud_instrument_3.font_size AS inst4_fs
FROM wp_hud_display
LEFT JOIN wp_hud_instrument
ON wp_hud_display.instrument_id_1 = wp_hud_instrument.inst_id
LEFT JOIN wp_hud_instrument wp_hud_instrument_1
ON wp_hud_display.instrument_id_2 = wp_hud_instrument_1.inst_id
LEFT JOIN wp_hud_instrument wp_hud_instrument_2
ON wp_hud_display.instrument_id_3 = wp_hud_instrument_2.inst_id
LEFT JOIN wp_hud_instrument wp_hud_instrument_3
ON wp_hud_display.instrument_id_4 = wp_hud_instrument_3.inst_id
WHERE wp_hud_display.user_id = 1
GROUP BY wp_hud_display.id";
/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("wp_hud_displays");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);
$hud_display_group = $domtree->createElement("hud_display_group");
$hud_display_group = $xmlRoot->appendChild($hud_display_group);
$hud_display = $domtree->createElement("hud_display");
$hud_display = $hud_display_group->appendChild($hud_display);
$instrument_id_1 = $domtree->createElement("instrument_id_1");
$instrument_id_1 = $hud_display->appendChild($instrument_id_1);
$instrument_id_2 = $domtree->createElement("instrument_id_2");
$instrument_id_2 = $hud_display->appendChild($instrument_id_2);
$instrument_id_3 = $domtree->createElement("instrument_id_3");
$instrument_id_3 = $hud_display->appendChild($instrument_id_3);
$instrument_id_4 = $domtree->createElement("instrument_id_4");
$instrument_id_4 = $hud_display->appendChild($instrument_id_4);
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if(mysql_num_rows($result)>0)
{
while($result_array = mysql_fetch_assoc($result))
{
/* you should enclose the following two lines in a cicle */
$hud_display->appendChild($domtree->createElement('id',$result_array['id']));
$hud_display->appendChild($domtree->createElement('name',$result_array['name']));
$hud_display->appendChild($domtree->createElement('resolution',$result_array['resolution']));
$hud_display->appendChild($domtree->createElement('user_id',$result_array['user_id']));
// Instrument 1
$hud_display->appendChild($domtree->createElement('instrument_id_1',$result_array['instrument_id_1']));
$instrument_id_1->appendChild($domtree->createElement('inst_id',$result_array['inst1_id']));
$instrument_id_1->appendChild($domtree->createElement('name',$result_array['inst1_name']));
$instrument_id_1->appendChild($domtree->createElement('image_name',$result_array['inst1_image']));
$instrument_id_1->appendChild($domtree->createElement('font_type',$result_array['inst1_ft']));
$instrument_id_1->appendChild($domtree->createElement('font_size',$result_array['inst1_fs']));
$hud_display->appendChild($domtree->createElement('x_loc_1',$result_array['x_loc_1']));
$hud_display->appendChild($domtree->createElement('y_loc_1',$result_array['y_loc_1']));
$hud_display->appendChild($domtree->createElement('layer_1',$result_array['layer_1']));
// Instrument 2
$hud_display->appendChild($domtree->createElement('instrument_id_2',$result_array['instrument_id_2']));
$instrument_id_2->appendChild($domtree->createElement('inst_id',$result_array['inst2_id']));
$instrument_id_2->appendChild($domtree->createElement('name',$result_array['inst2_name']));
$instrument_id_2->appendChild($domtree->createElement('image_name',$result_array['inst2_image']));
$instrument_id_2->appendChild($domtree->createElement('font_type',$result_array['inst2_ft']));
$instrument_id_2->appendChild($domtree->createElement('font_size',$result_array['inst2_fs']));
$hud_display->appendChild($domtree->createElement('x_loc_2',$result_array['x_loc_2']));
$hud_display->appendChild($domtree->createElement('y_loc_2',$result_array['y_loc_2']));
$hud_display->appendChild($domtree->createElement('layer_2',$result_array['layer_2']));
// Instrument 3
$hud_display->appendChild($domtree->createElement('instrument_id_3',$result_array['instrument_id_3']));
$instrument_id_3->appendChild($domtree->createElement('inst_id',$result_array['inst3_id'])); $instrument_id_3->appendChild($domtree->createElement('name',$result_array['inst3_name']));
$instrument_id_3->appendChild($domtree->createElement('image_name',$result_array['inst3_image']));
$instrument_id_3->appendChild($domtree->createElement('font_type',$result_array['inst3_ft'])); $instrument_id_3->appendChild($domtree->createElement('font_size',$result_array['inst3_fs']));
$hud_display->appendChild($domtree->createElement('x_loc_3',$result_array['x_loc_3']));
$hud_display->appendChild($domtree->createElement('y_loc_3',$result_array['y_loc_3']));
$hud_display->appendChild($domtree->createElement('layer_3',$result_array['layer_3']));
// Instrument 4
$hud_display->appendChild($domtree->createElement('instrument_id_4',$result_array['instrument_id_4']));
$instrument_id_4->appendChild($domtree->createElement('inst_id',$result_array['inst4_id']));
$instrument_id_4->appendChild($domtree->createElement('name',$result_array['inst4_name']));
$instrument_id_4->appendChild($domtree->createElement('image_name',$result_array['inst4_image']));
$instrument_id_4->appendChild($domtree->createElement('font_type',$result_array['inst4_ft']));
$instrument_id_4->appendChild($domtree->createElement('font_size',$result_array['inst4_fs'])); $hud_display->appendChild($domtree->createElement('x_loc_4',$result_array['x_loc_4']));
$hud_display->appendChild($domtree->createElement('y_loc_4',$result_array['y_loc_4']));
$hud_display->appendChild($domtree->createElement('layer_4',$result_array['layer_4']));
}
}
// get the xml printed
echo $domtree->saveXML();
//send the xml header to the browser
header ("Content-Type:text/xml");
//output the XML data
echo $xml;
?>
这是当只有一组值要返回时的显示方式(wp_hud_display.id)
<wp_hud_displays>
<hud_display_group>
<hud_display>
<instrument_id_1></instrument_id_1>
<instrument_id_2></instrument_id_2>
<instrument_id_3></instrument_id_3>
<instrument_id_4></instrument_id_4>
<id>1</id>
<name>test display</name>
<resolution>1080p</resolution>
<user_id>1</user_id>
<instrument_id_1>1</instrument_id_1>
<x_loc_1>89</x_loc_1>
<y_loc_1>79</y_loc_1>
<layer_1>1</layer_1>
<instrument_id_2>2</instrument_id_2>
<x_loc_2>988</x_loc_2>
<y_loc_2>98</y_loc_2>
<layer_2>1</layer_2>
<instrument_id_3>3</instrument_id_3>
<x_loc_3>89</x_loc_3>
<y_loc_3>90</y_loc_3>
<layer_3>1</layer_3>
<instrument_id_4>4</instrument_id_4>
<x_loc_4>67</x_loc_4>
<y_loc_4>76</y_loc_4>
<layer_4>1</layer_4>
</hud_display>
</hud_display_group>
</wp_hud_displays>
当有多个 wp_hud_display.id WHERE user_id=1 时如何显示
1test1_gauge001-2_zpsdbe86ca1f.jpg11PID_DISTANCE30100002500101223dfd34412sdf34534056Arail122test1_needle11PID_ENGINE_LOAD211000025200160112025000000002test1_needle11PID_ENGINE_LOAD211000025200160112025000000003new gaugedfsgsdfgfd435430PID_COOLANT_TEMP2434534541023324000000003new gaugedfsgsdfgfd435430PID_COOLANT_TEMP2434534541023324000000004Bla100.jpg2702334395910299000000004Bla100.jpg2702334395910299000000002test1_needle11PID_ENGINE_LOAD211000025200160112025000000001test display1080p118979129889813899014677612dsfdsfsd720p12321342323213224232123143423444342
我追求的是无论它返回多少组记录,它都看起来像正确的 XML 输出(如第一个示例)。
最佳答案
如果您希望 hud_display 节点重复,请在 while 内包含,每次迭代都会创建一个新的 hud_display。像这样的事情必须起作用:
// create a dom document with encoding utf8
$domtree = new DOMDocument('1.0', 'UTF-8');
//database configuration and extract your data
/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("wp_hud_displays");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);
$hud_display_group = $domtree->createElement("hud_display_group");
$hud_display_group = $xmlRoot->appendChild($hud_display_group);
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if(mysql_num_rows($result)>0)
{
while($result_array = mysql_fetch_assoc($result))
{
$hud_display = $domtree->createElement("hud_display");
$hud_display = $hud_display_group->appendChild($hud_display);
$instrument_id_1 = $domtree->createElement("instrument_id_1");
$instrument_id_1 = $hud_display->appendChild($instrument_id_1);
$instrument_id_2 = $domtree->createElement("instrument_id_2");
$instrument_id_2 = $hud_display->appendChild($instrument_id_2);
$instrument_id_3 = $domtree->createElement("instrument_id_3");
$instrument_id_3 = $hud_display->appendChild($instrument_id_3);
$instrument_id_4 = $domtree->createElement("instrument_id_4");
$instrument_id_4 = $hud_display->appendChild($instrument_id_4);
/* you should enclose the following two lines in a cicle */
$hud_display->appendChild($domtree->createElement('id',$result_array['id']));
$hud_display->appendChild($domtree->createElement('name',$result_array['name']));
$hud_display->appendChild($domtree->createElement('resolution',$result_array['resolution']));
$hud_display->appendChild($domtree->createElement('user_id',$result_array['user_id']));
// Instrument 1
$hud_display->appendChild($domtree->createElement('instrument_id_1',$result_array['instrument_id_1']));
$instrument_id_1->appendChild($domtree->createElement('inst_id',$result_array['inst1_id']));
$instrument_id_1->appendChild($domtree->createElement('name',$result_array['inst1_name']));
$instrument_id_1->appendChild($domtree->createElement('image_name',$result_array['inst1_image']));
$instrument_id_1->appendChild($domtree->createElement('font_type',$result_array['inst1_ft']));
$instrument_id_1->appendChild($domtree->createElement('font_size',$result_array['inst1_fs']));
$hud_display->appendChild($domtree->createElement('x_loc_1',$result_array['x_loc_1']));
$hud_display->appendChild($domtree->createElement('y_loc_1',$result_array['y_loc_1']));
$hud_display->appendChild($domtree->createElement('layer_1',$result_array['layer_1']));
// Instrument 2
$hud_display->appendChild($domtree->createElement('instrument_id_2',$result_array['instrument_id_2']));
$instrument_id_2->appendChild($domtree->createElement('inst_id',$result_array['inst2_id']));
$instrument_id_2->appendChild($domtree->createElement('name',$result_array['inst2_name']));
$instrument_id_2->appendChild($domtree->createElement('image_name',$result_array['inst2_image']));
$instrument_id_2->appendChild($domtree->createElement('font_type',$result_array['inst2_ft']));
$instrument_id_2->appendChild($domtree->createElement('font_size',$result_array['inst2_fs']));
$hud_display->appendChild($domtree->createElement('x_loc_2',$result_array['x_loc_2']));
$hud_display->appendChild($domtree->createElement('y_loc_2',$result_array['y_loc_2']));
$hud_display->appendChild($domtree->createElement('layer_2',$result_array['layer_2']));
// Instrument 3
$hud_display->appendChild($domtree->createElement('instrument_id_3',$result_array['instrument_id_3']));
$instrument_id_3->appendChild($domtree->createElement('inst_id',$result_array['inst3_id']));
$instrument_id_3->appendChild($domtree->createElement('name',$result_array['inst3_name']));
$instrument_id_3->appendChild($domtree->createElement('image_name',$result_array['inst3_image']));
$instrument_id_3->appendChild($domtree->createElement('font_type',$result_array['inst3_ft']));
$instrument_id_3->appendChild($domtree->createElement('font_size',$result_array['inst3_fs']));
$hud_display->appendChild($domtree->createElement('x_loc_3',$result_array['x_loc_3']));
$hud_display->appendChild($domtree->createElement('y_loc_3',$result_array['y_loc_3']));
$hud_display->appendChild($domtree->createElement('layer_3',$result_array['layer_3']));
// Instrument 4
$hud_display->appendChild($domtree->createElement('instrument_id_4',$result_array['instrument_id_4']));
$instrument_id_4->appendChild($domtree->createElement('inst_id',$result_array['inst4_id']));
$instrument_id_4->appendChild($domtree->createElement('name',$result_array['inst4_name']));
$instrument_id_4->appendChild($domtree->createElement('image_name',$result_array['inst4_image']));
$instrument_id_4->appendChild($domtree->createElement('font_type',$result_array['inst4_ft']));
$instrument_id_4->appendChild($domtree->createElement('font_size',$result_array['inst4_fs']));
$hud_display->appendChild($domtree->createElement('x_loc_4',$result_array['x_loc_4']));
$hud_display->appendChild($domtree->createElement('y_loc_4',$result_array['y_loc_4']));
$hud_display->appendChild($domtree->createElement('layer_4',$result_array['layer_4']));
}
}
// get the xml printed
echo $domtree->saveXML();
//send the xml header to the browser
header ("Content-Type:text/xml");
//output the XML data
echo $xml;
关于php - 如何将 MySQL foreach 循环添加到 DOMDocument XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32130448/
我是 PHP 新手。我一直在脚本中使用 for 循环、while 循环、foreach 循环。我想知道 哪个性能更好? 选择循环的标准是什么? 当我们在另一个循环中循环时应该使用哪个? 我一直想知道要
我在高中的编程课上,我的作业是制作一个基本的小计和顶级计算器,但我在一家餐馆工作,所以制作一个只能让你在一种食物中读到。因此,我尝试让它能够接收多种食品并将它们添加到一个价格变量中。抱歉,如果某些代码
这是我正在学习的一本教科书。 var ingredients = ["eggs", "milk", "flour", "sugar", "baking soda", "baking powder",
我正在从字符串中提取数字并将其传递给函数。我想给它加 1,然后返回字符串,同时保留前导零。我可以使用 while 循环来完成此操作,但不能使用 for 循环。 for 循环只是跳过零。 var add
编辑:我已经在程序的输出中进行了编辑。 该程序要求估计给定值 mu。用户给出一个值 mu,同时还提供了四个不等于 1 的不同数字(称为 w、x、y、z)。然后,程序尝试使用 de Jaeger 公式找
我正在编写一个算法,该算法对一个整数数组从末尾到开头执行一个大循环,其中包含一个 if 条件。第一次条件为假时,循环可以终止。 因此,对于 for 循环,如果条件为假,它会继续迭代并进行简单的变量更改
现在我已经习惯了在内存非常有限的情况下进行编程,但我没有答案的一个问题是:哪个内存效率更高;- for(;;) 或 while() ?还是它们可以平等互换?如果有的话,还要对效率问题发表评论! 最佳答
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 8 年前。 我正在尝试创建一个小程序,我可以在其中读取该程序的单词。如果单词有 6
这个问题在这里已经有了答案: python : list index out of range error while iteratively popping elements (12 个答案) 关
我正在尝试向用户请求 4 到 10 之间的整数。如果他们回答超出该范围,它将进入循环。当用户第一次正确输入数字时,它不会中断并继续执行 else 语句。如果用户在 else 语句中正确输入数字,它将正
我尝试创建一个带有嵌套 foreach 循环的列表。第一个循环是循环一些数字,第二个循环是循环日期。我想给一个日期写一个数字。所以还有另一个功能来检查它。但结果是数字多次写入日期。 Out 是这样的:
我想要做的事情是使用循环创建一个数组,然后在另一个类中调用该数组,这不会做,也可能永远不会做。解决这个问题最好的方法是什么?我已经寻找了所有解决方案,但它们无法编译。感谢您的帮助。 import ja
我尝试创建一个带有嵌套 foreach 循环的列表。第一个循环是循环一些数字,第二个循环是循环日期。我想给一个日期写一个数字。所以还有另一个功能来检查它。但结果是数字多次写入日期。 Out 是这样的:
我正在模拟一家快餐店三个多小时。这三个小时分为 18 个间隔,每个间隔 600 秒。每个间隔都会输出有关这 600 秒内发生的情况的统计信息。 我原来的结构是这样的: int i; for (i=0;
这个问题已经有答案了: IE8 for...in enumerator (3 个回答) How do I check if an object has a specific property in J
哪个对性能更好?这可能与其他编程语言不一致,所以如果它们不同,或者如果你能用你对特定语言的知识回答我的问题,请解释。 我将使用 c++ 作为示例,但我想知道它在 java、c 或任何其他主流语言中的工
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我是 C 编程和编写代码的新手,以确定 M 测试用例的质因数分解。如果我一次只扫描一次,该功能本身就可以工作,但是当我尝试执行 M 次时却惨遭失败。 我不知道为什么 scanf() 循环有问题。 in
这个问题已经有答案了: JavaScript by reference vs. by value [duplicate] (4 个回答) 已关闭 3 年前。 我在使用 TSlint 时遇到问题,并且理
我尝试在下面的代码中添加 foreach 或 for 循环,以便为 Charts.js 创建多个数据集。这将允许我在此折线图上创建多条线。 我有一个 PHP 对象,我可以对其进行编码以稍后填充变量,但
我是一名优秀的程序员,十分优秀!