skip to Main Content

After reading the data from the DB, you can use foreach to output all the data. But I want to print out the printed data as an alert at once, what should I do?

this is my code

foreach($result as $row){     
            print_r("excel_number:".$row['excel_index']." / "."fail_reason:".$row['reson_result']."<br>");  
    } 

$total_fail_log = "excel_number:".$row['excel_index']." / "."fail_reason:".$row['reson_result']."<br>";
        echo "<script>alert('$total_fail_log');</script>";

and print result

enter image description here

and alert result

enter image description here

When output as alert, only the last value is output. How do I print out the whole thing like the first picture?

2

Answers


  1. You can put the alert inside the foreach loop and it will be executed from inside. By doing this, according to the output, you should have two alerts.

    Login or Signup to reply.
  2. Concatenate all your data to a string. Then pass the sctring to an alert function.

    <?php
    
    $alert_string = "";
    
    // add each line to the $alert_string (added n for formatting)
    foreach($result as $row) {
        $alert_string .= "excel_number:".$row['excel_index']." / "."fail_reason:".$row['reson_result']."<br>n";
    }
    
    // print the result as an alert script
    echo "<script>alert(`$alert_string`);</script>";
    

    Feel free to tweak the code according to your need.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search