Jump to content

php generate data from foreach loop on txt file with table formatted


gongpex

Recommended Posts

Hello everyone,

 

I had tried to generate txt file from php foreach loop,
it's work, but the result is not table formatted like this :
UtexN.png
Q : Is there php plugins or something ,so that the result shown as my expectation like this :
tl31k.png
this is my code :
 $output[] = ''."\n".'';
    $output[] = '  No  '.'                    Name Product                    '.'    Total   '."\n".'';
    foreach($query->result() as $row){
       $results = $row->id_product; 
       $product = $this->db->query("select product_name from master_product WHERE product_id='".$results."'");
       $product_name = $product->row()->product_name;
       $output[] = '  '.$no.str_repeat(" ",20).''. $product_name.str_repeat(" ",20).''.$row->quantity.'  '.$row->scale."\n".'';
       $no++;
    }
    file_put_contents(APPPATH."txt/test.txt", $output);
please someone help me
Thanks
Edited by gong
Link to comment
Share on other sites

You would have to program that yourself.

 

Look through all the data of each column to find out the longest value and make the width of the column wider than that, then when writing to the file you pad all the strings with enough spaces to match the width of the column they belong to.

Link to comment
Share on other sites

And instead of using str_repeat to print the same number of spaces regardless of how long the text is, use str_pad. There's also a query that you can send to MySQL to get information about the columns in the table so that you can determine the length dynamically if you don't want to hard-code everything.

Link to comment
Share on other sites

Calculate font pixels from product name and from total of spaces pixel width, subtract to get difference, calculate spaces required from number of pixels, then use tabs to separate equally

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $servername = "localhost";
        $username = "root";
        $password = "bullards";
        $dbname = "products";

        try {
            $pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $stmt = $pdo->prepare("SELECT * FROM master_product");
            $stmt->execute();

            //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            $output = "";
            $tabtotal = 2;
            $taboutput = "";

            function spacing($Pno, $ProductName, $Ptotal) {

                global $tabtotal;

                //produce number tabs required
                for ($i = 0; $i < $tabtotal; $i++) {
                    $taboutput.="\t";
                }

                //produce number of spaces to calculate total width
                $spaceoutput = "";
                for ($i = 0; $i < 112; $i++) {
                    $spaceoutput.=" ";
                }


                list($left,, $right) = imageftbbox(12, 0, "arial.ttf", $spaceoutput);
//total pixel width of spaces
                $totalSpaceWidth = $right - $left;

                list($left,, $right) = imageftbbox(12, 0, "arial.ttf", $ProductName);
//total pixel width of product
                $TotalStringWidth = $right - $left;

                //caculate spacing reqired by subtracting product name  pixel length from spacing pixel width
                $currentSpaceing = $totalSpaceWidth - $TotalStringWidth;

                //spacing takes up 4 px, so dived result by 4
                $currentSpaceing = $currentSpaceing / 4;

                $output = str_pad($Pno, 8, " ") . $taboutput . str_pad($ProductName, $currentSpaceing, " ") . $taboutput . str_pad($Ptotal, 8, " ") . "\n";

                return $output;
            }

            $output.= spacing("No", "Product Name", "Total");

            while ($row = $stmt->fetchObject()) {
                $no++;
                $output.= spacing($no, $row->product_name, $row->quantity . ' ' . $row->scale);
            }
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }

        $file = "test.txt";
        file_put_contents($file, $output);

        $pdo = null;
        ?>

    </body>
</html>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...