I have recently started to write code in PHP for a specific project. I am using the PHPQRCode library to generate QR codes from a sample database created in MYSQL. I am running all of this on a XAMPP server hosted on my machine.
The sample entries from my db are given below:
Also I will post the snippet of the code I have written so far . This code fetches the data from the db and convert it into a QRCode. Comments have been added at required lines.
<?php
include "phpqrcode/qrlib.php";
// This library helps in generating QR Code
// db_name is quickcodedatabase
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "quickcodedatabase");
$connect = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
//actual sql query
$sql = "SELECT * FROM quickcodetable";
$result = $connect->query($sql);
$count = 1;
// count is used here to generate sequential names for output image files.
if ($result->num_rows > 0)
{
// output data of each row
while ($row = $result->fetch_assoc())
{
$rowdata = "Name: " . $row["s_name"] . " Address = " . $row["s_address"] . " Amount = " . $row["amount"];
echo nl2br($rowdata . "\n");
$file_name = "test_mid" . $count . ".png";
$count++;
//count is used to generate sequential file names
QRcode::png($rowdata, $file_name);
echo nl2br("<img src='../$file_name'>" . "\n");
}
}
else
{
echo "No results";
}
mysqli_close($connect);
?>
The output I get after running the above code is:
Now what I want to achieve is to get(or export) each QR and the data associated with it on a separate HTML file . So, in my case I will have three newly created HTML files after I run the code.
As I am pretty new to PHP I am not able to figure out how to do this. I looked at some of the related questions posted here but could most of them involved use of some COM objects which I am totally unaware about. I dont care about the formatting on the output pages as long as I get the details related to each database entry exported on a separate HTML page along with the corresponding QR code.
Any help in this regard will be appreciated.
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$n = $_POST['n'];
$p = $_POST['p'];
$con = mysqli_connect("localhost","mysql username","mysql password","database name");
if($con->query("insert into users(name,password)values('$n','$p')")){
header("location:index.php");
}
}
?>
<form method="post" action="file.php">
<input type="text" name="n" placeholder="Name"/>
<input type="password" name="p" placeholder="Password"/>
<input type="submit" value="add"/>
</form>
Slightly change this piece of code as follows. It will save the html text with qr code image to an html file on each iteration with the name "test_mid" . $count . ".html"
while ($row = $result->fetch_assoc())
{
$rowdata = "Name: " . $row["s_name"] . " Address = " . $row["s_address"] . " Amount = " . $row["amount"];
$rowdata=nl2br($rowdata . "\n");
$file_name = "test_mid" . $count . ".png";
$count++;
//count is used to generate sequential file names
QRcode::png($rowdata, $file_name);
$rowdata.=nl2br("<img src='../$file_name'>" . "\n");
file_put_contents("test_mid" . $count . ".html",$rowdata);
}
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.