簡體   English   中英

從php中的數據庫中獲取分隔的數組值

[英]Fetch separated array values from database in php

我有一個表“歷史”的數據庫

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

我想將該 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>

我需要像 id=2 時的輸出:

Title : SeriesA
Subtitle: SessionA
File: Mircosoft.doc
---------------------
Title : SeriesB
Subtitle: SessionB
File: Windows.docx
---------------------
Title : SeriesC
Subtitle: SessionC
File: Server.doc

查看您的值,您只需循環$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;
    }
}   

當您分解行數據時,您有一個數組,因此您可以執行以下操作:

    foreach($name as $key => $value){
        echo 'name - '.$value;
        if(isset($subtitle[$key])){
            echo 'subtitle - '.$subtitle[$key];
        }
        if(isset($file [$key])){
            echo 'file - '.$file [$key];
        }
    }

但是,如果您不確定名稱是否等於字幕和文件的數量,則可以獲取數組元素的最高數量,然后這樣做:

for($i = 0; $i < $highestCount; $i++){
    ...
    if(isset($subtitle[$i])){
         echo 'subtitle - '.$subtitle[$i];
    }
    ....
}

你可以試試:

$id = $_GET['id'];
$get_history = mysqli_query($mysqli, "SELECT * FROM history WHERE id=".$id."");
$row = mysqli_fetch_assoc($get_history);

下面這三個將得到從原始數據中分解出來的數據。

$title = explode("*" , $row['title']);
$subtitle = explode("*" , $row['subtitle']);
$file= explode("*" , $row['file']);

顯示它們:

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]

讓我知道這個是否奏效 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM