Search code examples
phpqr-code

Return new line in QR code scanner in PHP


So after search the questions available in here, I still cannot find the solution for breaking new line when users scan the code.

I already try \n, PHP_EOL but still can't managed to solve it.

<?php
$host = 'localhost';
$user = 'root';
$pass = 'db_pass';
$name = 'db_name';

$i = 0;
$q = $r = $s ="";
$data = $data2 = array();
$con = new mysqli($host,$user,$pass,$name);
    if($con->connect_error)
        die ("Error: ".$con->connect_error);
    $pid = ($_POST['pid']);
        $result = mysqli_query($con,"SELECT * FROM purchase_detail WHERE purchase_id ='$pid' ");

 $b=mysqli_num_rows($result);
    while($row = mysqli_fetch_array($result))
    {
        
        $data[$i] = $row['type_id'];
        $data2[$i] = $row ['quantity'];
        $i++;
    }
    
        
    
    for ($x = 0; $x < $b; $x++) 
    {   
    $q .= $data[$x];
    $r .= $data2[$x];
    $s .='Type: '.$q[$x] .' Quantity: '.$r[$x];
    echo ''.PHP_EOL.'';
    } 
 
echo "<img src='qr_img.php?d=$s'";


?>

<html>
    <form action="<?php echo htmlspecialchars ($_SERVER['PHP_SELF']);?>" method="post">
    <input type="text" name="pid">
    </form>
</html>

The result for current is:

Type: 1 Quantity: 1Type: 2 Quantity: 1Type: 3 Quantity: 1Type: 4 Quantity: 1

But, I would like it appear like this:

Type: 1 Quantity: 1
Type: 2 Quantity: 1
Type: 3 Quantity: 1
Type: 4 Quantity: 1

Example: QR Scanner

I use CodeTwo Desktop QR reader for scanning the QR code.

EDITED: If I used '\n':

for ($x = 0; $x < $b; $x++) 
{   
 $q .= $data[$x];
 $r .= $data2[$x];
 $s .='Type: '.$q[$x] .' Quantity: '.$r[$x];
 echo '\n';
} 

Result of '\n'


Solution

  • After google-ing for days, I found the answer below:

    for ($x = 0; $x < $b; $x++) 
    {   
    $q .= $data[$x];
    $r .= $data2[$x];
    $s .='Type: '.$q[$x] .' Quantity: '.$r[$x].'%0A';
    } 
    

    Simply %0A