[英]Fetch separated array values from database in php
I've database with a table 'history'我有一个表“历史”的数据库
id title subtitle file
---------------------------- ----------------------------- -------------------------------------
2 SeriesA*SeriesB*SeriesC SessionA*SessionB*SessionC Mircosoft.doc*Windows.docx*Server.doc
4 Objective*Punc*Blank Shorttype*Paragraph*Match Mircosoft.doc*Windows.docx*Server.doc
I want to fetch title, subtitle and file of that ID, to html input values.我想将该 ID 的标题、副标题和文件提取到 html 输入值。
$id = $_GET['id'];
$get_history = mysqli_query($mysqli, "SELECT * FROM history WHERE id=".$id."");
$rows = array();
while($row = mysqli_fetch_assoc($get_history))
$rows[] = $row;
foreach($rows as $row) {
$title = explode("*" , $row['title']);
$subtitle = explode("*" , $row['subtitle']);
$file = explode("*" , $row['file']);
}
<html>
<body>
Title: <input type="text" name="name[]" value="<?php echo $title; ?> "/><br>
Subtitle: <input type="text" name="title[]" value="<?php echo $subtitle; ?> "/><br>
File: <input type="file" name="file[]" value="<?php echo $file; ?> "/><br>
<input type="button" class="add" value="Add"/><br>
<input type="submit" value="submit" name="submit">
</body>
</html>
I need the output like when id=2 :我需要像 id=2 时的输出:
Title : SeriesA
Subtitle: SessionA
File: Mircosoft.doc
---------------------
Title : SeriesB
Subtitle: SessionB
File: Windows.docx
---------------------
Title : SeriesC
Subtitle: SessionC
File: Server.doc
Looking at your values, you just have to loop $name
and print them simultaneously with $subtitle
and $file
index values like below:查看您的值,您只需循环
$name
并使用$subtitle
和$file
索引值同时打印它们,如下所示:
<?php
foreach($rows as $row) {
$title = explode("*" , $row['title']);
$subtitle = explode("*" , $row['subtitle']);
$file = explode("*" , $row['file']);
foreach($title as $index => $title_value){
echo 'Title:' ,$title_value,PHP_EOL;
echo 'Subtitle:',$subtitle[ $index ],PHP_EOL;
echo 'File:',$file[ $index ],PHP_EOL;
echo "----------",PHP_EOL;
}
}
When you explode your row data you have an array, so you can do this:当您分解行数据时,您有一个数组,因此您可以执行以下操作:
foreach($name as $key => $value){
echo 'name - '.$value;
if(isset($subtitle[$key])){
echo 'subtitle - '.$subtitle[$key];
}
if(isset($file [$key])){
echo 'file - '.$file [$key];
}
}
But if you are not sure that number if names is equal to numbers of subtitle and file, you can get the highest count of array elements and do it like that:但是,如果您不确定名称是否等于字幕和文件的数量,则可以获取数组元素的最高数量,然后这样做:
for($i = 0; $i < $highestCount; $i++){
...
if(isset($subtitle[$i])){
echo 'subtitle - '.$subtitle[$i];
}
....
}
You can just try:你可以试试:
$id = $_GET['id'];
$get_history = mysqli_query($mysqli, "SELECT * FROM history WHERE id=".$id."");
$row = mysqli_fetch_assoc($get_history);
these three below will get the data that is exploded from the original data.下面这三个将得到从原始数据中分解出来的数据。
$title = explode("*" , $row['title']);
$subtitle = explode("*" , $row['subtitle']);
$file= explode("*" , $row['file']);
to display them:显示它们:
Title : $title[0]
Subtitle: $subtitle[0]
File: $file[0]
---------------------
Title : $title[1]
Subtitle: $subtitle[1]
File: $file[1]
---------------------
Title : $title[2]
Subtitle: $subtitle[2]
File: $file[2]
Let me know if this works :)让我知道这个是否奏效 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.