简体   繁体   中英

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 = $_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 :

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:

<?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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM