首先感谢你阅读我的问题。所以我手中有一个文本区域,它提交到表中的单个列。
假设我只在一行中发送了这个值:
<first_name>Sophia</first_name><last_name>Aba</last_name>
<first_name>Ana</first_name><last_name>Bush</last_name>
提交后,它将转到名为贡献者的列。
现在,我想要从数据库中获取该值,并像这样将它们分开:
<?php
$get = $connect->query("SELECT * FROM articles WHERE id= '1' ");
$fetch = $get->fetch_array(MYSQLI_ASSOC);
$contributors = $fetch['contributors'];
但是现在我如何分离该条目,使php获得标记内的名称,并检测其中有多少,这样我就可以将thoose放入单独的变量中,如:
<?php
$count = "HERE COUNT FOR HOW MUCH FIRST_NAME THERE IS "
for($i = 0; i >= $count; $i++){
$array = ("a","b","c");
$first_name_$array[$i] = "HERE THE VARIABLE THAT GETS THE FIRST_NAME TAG OF THE FIRST"; // I know this is incorrect way of incrementing a variable name that's why im also asking for help
}
发布于 2018-07-21 07:01:09
以不同的方式实现
首先,我建议首先以不同的方式存储数据。保存XML,然后在从数据库中读出数据后再次从中提取数据是一种非常复杂的方法(您可以从您的问题中看到如何做到这一点)。有关这一点,请参阅into Database normalization。
如果你一定要这样做的话
为此,我建议使用PHPs SimpleXML,如下所示:
<?php
$contributors =
"<contributors>
<contributor>
<first_name>Sophia</first_name>
<last_name>Aba</last_name>
</contributor>
<contributor>
<first_name>Ana</first_name>
<last_name>Bush</last_name>
</contributor>
</contributors>";
$xmlContributors = new SimpleXMLElement($contributors);
foreach ($xmlContributors->children() as $contributor) {
echo 'This is one contributor: ' . $contributor->first_name . ' ' . $contributor->last_name . PHP_EOL;
}
但是请注意,您必须更改XML结构才能获得一些合理的XML构建(如上所示)。
https://stackoverflow.com/questions/51450309
复制相似问题