gongpex Posted November 30, 2016 Share Posted November 30, 2016 (edited) 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 : Q : Is there php plugins or something ,so that the result shown as my expectation like this : 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 November 30, 2016 by gong Link to comment Share on other sites More sharing options...
Ingolme Posted November 30, 2016 Share Posted November 30, 2016 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 More sharing options...
justsomeguy Posted November 30, 2016 Share Posted November 30, 2016 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 More sharing options...
dsonesuk Posted November 30, 2016 Share Posted November 30, 2016 That would only work if text was exactly same width wouldn't it? I mean when you compare number of 'i' compared to 'w' then add str_pad() of twenty spaces the distance would vastly different. Link to comment Share on other sites More sharing options...
Ingolme Posted November 30, 2016 Share Posted November 30, 2016 It's a .txt file, so I think that's a safe assumption to make. Link to comment Share on other sites More sharing options...
dsonesuk Posted December 1, 2016 Share Posted December 1, 2016 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 More sharing options...
Ingolme Posted December 1, 2016 Share Posted December 1, 2016 Txt files don't have a specified font, but they are generally represented with a monospace font in most text editors. Link to comment Share on other sites More sharing options...
dsonesuk Posted December 1, 2016 Share Posted December 1, 2016 It does not matter which font i used, i just needed a font to give me general width dimensions for every character. Link to comment Share on other sites More sharing options...
gongpex Posted December 1, 2016 Author Share Posted December 1, 2016 Hello , Thanks for reply I appreciate it Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now