0

I have a website where you can select a row of a table and that would display another table without refreshing the page. The new table is based on a string that is converted to an array and separated based on commas.

The table is generated on the server side, not within html with php elements. I cannot seem to get the table to show all the elements of the original string, now as an array, and add them dynamically as rows.

Here is the server side code:

if (isset($_GET['query'])) {
    $id = $_GET['query'];
    $query = "SELECT * FROM samples_database WHERE sample_id=$id;";


    $result = mysqli_query($connect, $query);

    $input = mysqli_fetch_array($result);

    $input1 = $input['micro_analysis'];

    $rows = var_dump(explode(',', $input1));

    if (count($rows) > 0) {
        $output .= 
        '<thead>
        <tr>
        <th>Tests</th>                      
        </tr>                                   
        </thead>
        <tbody>';
        foreach ($rows as $row): array_map('htmlentities', $row);
            '<tr>
            <td>'; echo implode('</td><td>', $row); 
            '</td>                
            </tr>';
        endforeach;  
        '</tbody>';
    }
    echo $output;
}

In the case for better understanding the variable $input1 will look something like this:

$input1 = "1,2,44,67";

When using var_dump(explode(',', $input1)); it produces an array like this:

array(4) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(2) "44" [3]=> string(2) "67" }

So now I need the table to create under the column tests a row with "1", then a new row with "2" and so on...

Can anybody please help?

3
  • In your foreach, you don't echo the html, just have it in limbo without echo, you only echo the implode of the $row Commented Jul 6, 2018 at 9:27
  • I keep getting this error: Invalid argument supplied for foreach() in.... Commented Jul 6, 2018 at 9:29
  • Because you're assigning a var_dump to $rows, var_dump dosn't produce a usable variable, only output, foreach expects an array, $rows is null / empty Commented Jul 6, 2018 at 9:29

2 Answers 2

2

If $input1 is a simple string separated by commas then something like this should be enough:

$input1 = "1,2,44,67";

$rows = explode(',', $input1);
$output = "";
if (count($rows) > 0) {
    $output .= '<thead><tr><th>Tests</th></tr></thead><tbody>';
    foreach ($rows as $row){
        $output .= '<tr><td>' . $row . '</td></tr>';
    }
    $output .= '</tbody>';
}
echo $output;

No need to use array_map and explode again, just loop through each result inside $rows and use string concatenation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this was much simpler that I imagined!
0

First of all remove var_dump from $rows = var_dump(explode(',', $input1));. Also need to append every html string to $output and echo it at the end.

Code should be like:

$rows = explode(',', $input1);
$output = "";
if (count($rows) > 0) {
    $output .= '<thead><tr><th>Tests</th></tr></thead><tbody>';
    foreach ($rows as $row): array_map('htmlentities', $row);
        $output .= '<tr><td>' . implode('</td><td>', $row) . '</td></tr>';
    endforeach;
    $output .= '</tbody>';
}
echo $output;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.