i got this script. it looks into a file and read all the lines and put it nicely in a table.. what i need to do now is to take all the table data and split it into two tables ..
eg. if their is 100 rows.. then instead of one long list, i will get 50 data in one table and the other 50 in the other table...
enter code here<?php
if(isset ($_GET['type']))
{
$otype = $_GET['type'];
}
else
{
$otype = 'm';
}
$statusFile = "d:\\CLIENTS\\status.txt";
$file_handler = fopen($statusFile, "r");
// read the contents
$contents = fread($file_handler, filesize($statusFile));
// close the file
fclose($file_handler);
if (strcasecmp($otype, "m") == 0)
{
echo $contents;
}
else
{
$lines = explode("\n",$contents);
$frow = explode(",", $lines[0]);
if (strcmp($frow[1],"1") == 0)
{
echo "Update Status: <b>Complete</b>";
//to count total lines in txt file
$statusFile = "d:\\CLIENTS\\status.txt";
$line = count(file($statusFile));
echo "There are".$line."lines in";
}
else
{
echo "Update Status: <b>Incomplete</b>";
}
echo "<table border=\"1\">";
for ($count = 1; $count < sizeof($lines); $count++)
{
$fields = explode(",",$lines[$count]);
$sz = sizeof($fields);
if ($sz > 1)
{
$str = "OK";
echo "<tr>";
echo "<td>" . $fields[0] . "</td>";
echo "<td>" . $fields[1] . "</td>";
//echo "<td>" . $fields[2] . "</td>";
if (strpos($fields[2],'OK') !== false)
{
echo "<td><font color='green'>". $fields[2] ."</font></td>";
//echo "<td style='background-color: #00FF00;'>". $fields[2] ."</td>";
}
else
{
//echo "<td><font color='red'>". $fields[2] ."</font></td>";
echo "<td style='background-color: #FF0000;'>". $fields[2] ."</td>";
}
echo "</tr>";
}
}
echo "</table>";
}
?>
Use a foreach instead of a for and $count to calculate if it is needed to echo a table tag. $count is only incremented when a row was inserted.
<style>
div.container {
width: 100%; /* Change the width to the desired width */
padding: 0;
}
div.container table {
width: 50%; /* Change the width to half of the width of the div */
margin: 0;
float: left;
}
</style>
<?php
if(isset ($_GET['type']))
{
$otype = $_GET['type'];
}
else
{
$otype = 'm';
}
$statusFile = "d:\\CLIENTS\\status.txt";
$file_handler = fopen($statusFile, "r");
// read the contents
$contents = fread($file_handler, filesize($statusFile));
// close the file
fclose($file_handler);
if (strcasecmp($otype, "m") == 0)
{
echo $contents;
}
else
{
$lines = explode("\n",$contents);
$frow = explode(",", $lines[0]);
if (strcmp($frow[1],"1") == 0)
{
echo "Update Status: <b>Complete</b>";
//to count total lines in txt file
$statusFile = "d:\\CLIENTS\\status.txt";
$line = count(file($statusFile));
echo "There are".$line."lines in";
}
else
{
echo "Update Status: <b>Incomplete</b>";
}
echo '<div class="conatiner">';
$count = 0;
foreach ($lines as $currLine)
{
$fields = explode(",",$currLine);
$sz = sizeof($fields);
if ($sz > 1)
{
if ($count == 0) {
echo "<table border=\"1\">";
}
elseif ($count % 50 == 0) {
echo "</table>";
echo "<table border=\"1\">";
}
$str = "OK";
echo "<tr>";
echo "<td>" . $fields[0] . "</td>";
echo "<td>" . $fields[1] . "</td>";
//echo "<td>" . $fields[2] . "</td>";
if (strpos($fields[2],'OK') !== false)
{
echo "<td><font color='green'>". $fields[2] ."</font></td>";
//echo "<td style='background-color: #00FF00;'>". $fields[2] ."</td>";
}
else
{
//echo "<td><font color='red'>". $fields[2] ."</font></td>";
echo "<td style='background-color: #FF0000;'>". $fields[2] ."</td>";
}
echo "</tr>";
$count++;
}
}
echo "</table>";
echo '</div>';
}
Related
I am learning php and in doing it I had a problem.
I have to insert in a table of the payment data taken from a second table but only after checking that the id of the person corresponds to the id of the person who made the payment.
<?php
3 Query ...
while($rowSocio = mysqli_fetch_array($mostraSocio)) {
$idSocio = $rowSocio['socio_id'];
$nomeSocio = $rowSocio['socio_nome'];
$cognomeSocio = $rowSocio['socio_cognome'];
echo "<tr>";
echo "<td>{$cognomeSocio}</td>";
echo "<td>{$nomeSocio}</td>";
$i = 0;
$print = "0";
while($row = mysqli_fetch_array($mostraOperazione)) {
$id = $row['socio_id'];
$nome = $row['socio_nome'];
$cognome = $row['socio_cognome'];
$importo = $row['opfin_importo'];
$quota = $row['quota_nome'];
if($idSocio === $id) {
if($quota === "Prima Rata" && $print !== "1") {
echo "<td class='align-rx'>€ {$importo}</td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
$i++;
$print = "1";
} elseif($quota === "Seconda Rata" && $print !== "2") {
echo "<td></td>";
echo "<td class='align-rx'>€ {$importo}</td>";
echo "<td></td>";
echo "<td></td>";
$i++;
$print = "2";
} elseif($quota === "Terza Rata" && $print !== "3") {
echo "<td></td>";
echo "<td></td>";
echo "<td class='align-rx'>€ {$importo}</td>";
echo "<td></td>";
$i++;
$print = "3";
} elseif($quota === "Quota Stagionale" && $print !== "4") {
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
$i++;
$print = "4";
echo "<td class='align-rx'>€ {$importo}</td>";
} else {
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
$i++;
}
} else {
echo "</tr>";
$i++;
}
}
}
?>
so I only print the names of the people and not the payments they make
output:
enter image description here
I have a csv file with some data inside.
When User type something in the search bar,it will return the relate data but these results are too much, I want to make a pagination.I don't have any idea, google a lot but can't find a way to do. Please help.
$filename = "Database.csv";
$delimiter = ",";
if (!file_exists($filename) || !is_readable($filename))
return false;
if ($delimiter == ',') {
$csv = array_map('str_getcsv', file($filename));
} else {
$lines = file($filename);
$line_num = count($lines);
$dm = [];
$csv = array_map('str_getcsv', $lines, array_pad($dm, $line_num, $delimiter));
}
array_walk($csv, function (&$row) use ($csv) {
$row = array_combine($csv[0], $row);
});
array_shift($csv);
$total_row_of_csv = count($csv);
$total_row = 0;
for ($i = 0; $i < $total_row_of_csv; $i++) {
if (preg_match("/$value/i", $csv[$i]['Catagory'])) {
echo "<tr>";
echo "<td>" . $csv[$i]['Ref'] . "</td>";
echo "<td>" . $csv[$i]['Catagory'] . "</td>";
echo "<td>" . $csv[$i]['JobTitle'] . "</td>";
echo "<td>" . $csv[$i]['Description'] . "</td>";
echo "<td>" . $csv[$i]['Salary'] . "</td>";
echo "<td>" . $csv[$i]['Nature'] . "</td>";
echo "</tr>";
$total_row++;
}
}
GET : index.php?page=2&search=purchasing
clear the page=1 for a new search
This function is done with get method not post, you can do it as the post by doing appropriately
<?php
// you can make this as post as you want
// i have made this as a get param function
$value = !empty($_GET['search']) ? $_GET['search'] : '';
$filename = "Database.csv";
$delimiter = ",";
if (!file_exists($filename) || !is_readable($filename))
return false;
if ($delimiter == ',') {
$csv = array_map('str_getcsv', file($filename));
} else {
$lines = file($filename);
$line_num = count($lines);
$dm = [];
$csv = array_map('str_getcsv', $lines, array_pad($dm, $line_num, $delimiter));
}
array_walk($csv, function (&$row) use ($csv) {
$row = array_combine($csv[0], $row);
});
array_shift($csv);
$total_row_of_csv = count($csv);
$countForPage = 0;
for ($i = 0; $i < $total_row_of_csv; $i++) {
//var_dump($csv[$i]['Catagory']);die();
// didint get the $value so commented id
if (preg_match("/$value/i", $csv[$i]['Catagory'])) {
$countForPage ++;
}
}
$countForPage;
$numPerPage = 10;
$numCurrPage = !empty($_GET['page']) ? $_GET['page'] : 1;
$numFromCnt = $numPerPage * ($numCurrPage - 1);
$numLastCnt = $numFromCnt + $numPerPage ;
$total_row = 0;
echo "<table>";
for ($i = 0; $i < $total_row_of_csv; $i++) {
//var_dump($csv[$i]['Catagory']);die();
// didint get the $value so commented id
if (preg_match("/$value/i", $csv[$i]['Catagory'])) {
//echo $total_row .">=". $numFromCnt ."&&". $total_row ."<". $numLastCnt; //die();
if($total_row >= $numFromCnt && $total_row < $numLastCnt) {
echo "<tr>";
echo "<td>" . $csv[$i]['Ref'] . "</td>";
echo "<td>" . $csv[$i]['Catagory'] . "</td>";
echo "<td>" . $csv[$i]['JobTitle'] . "</td>";
echo "<td>" . $csv[$i]['Description'] . "</td>";
echo "<td>" . $csv[$i]['Salary'] . "</td>";
echo "<td>" . $csv[$i]['Nature'] . "</td>";
echo "</tr>";
}
$total_row++;
}
}
//echo $total_row;die();
echo "</table>";
$links = generateLinks($countForPage, $numCurrPage, $numPerPage, $value);
echo $links;
function generateLinks($total_row_of_csv, $numCurrPage, $numPerPage, $search){
$pagLink = '';
if($numCurrPage > 1) {
$pagLink .= "<li class='active'><a href='csv_pagination.php?page="
.($numCurrPage-1)."&search=".$search."'>Prev</a></li>";
}
// for ($i=1; $i<=$total_row_of_csv; $i++) {
// if ($i==$numCurrPage) {
// $pagLink .= "<li class='active'><a href='csv_pagination.php?page="
// .$i."'>".$i."</a></li>";
// } else {
// $pagLink .= "<li><a href='csv_pagination.php?page=".$i."'>
// ".$i."</a></li>";
// }
// }
if($numCurrPage < ($total_row_of_csv/$numPerPage)) {
$pagLink .= "<li class='active'><a href='csv_pagination.php?page="
.($numCurrPage+1)."&search=".$search."'>Next</a></li>";
}
return $pagLink;
}
?>
I created a code that converts characters to binary and make table cells black/white corresponding to the ones and zeros. This is my code:
$str_splt = str_split($text);
echo "<table>";
for ($a=0;$a < count($str_splt);$a++) {
$bits = array(128,64,32,16,8,4,2,1);
$store = array(0,0,0,0,0,0,0,0);
$inp = ord($str_splt[$a]);
for ($x=0;$x < count($bits);$x++) {
if ($bits[$x] <= $inp) {
$inp = $inp - $bits[$x];
$store[$x] = 1;
} else {
$store[$x] = 0;
}
};
$store_rvs = array_reverse($store);
echo "<tr>";
for ($b=0;$b < count($store_rvs);$b++) {
if ($store_rvs[$b] == '1') {
echo "<td id=\"blk\"></td>";
}
else {
echo "<td></td>";
}
}
echo "</tr>";
}
echo "</table>";
Its output looks like this ($text = "ABCDEFGH"):
As you can see it's 8x8 table. I want to add the next set of bytes to the side of that table like this:
Each 8x8 table is a group. The two images above is group 1 and group 2:
I want to display the tables like this but I can't find the solution.
I did it in this way. Ignore my css if you are fine with yours. I replaced the id tag with class because each id should be defined once only.
echo "<html><head>";
echo "<style type='text/css'>";
echo " table, td { padding:0px; margin:0px; }";
echo " td.cell { width:15px; height:15px; }";
echo " td.blk { background-color:black; }";
echo " td.wht { background-color:yellow; }";
echo "</style>";
echo "</head><body>";
$text = "ABCDEFGH";
$text.= "ABCDEFGH";
echo "<table><tr><td><table>";
for($a=0; $a<strlen($text); $a++) {
$chr = substr($text,$a,1);
$bits = array(128,64,32,16,8,4,2,1);
$store = array(0,0,0,0,0,0,0,0);
$inp = ord($chr);
for($x=0; $x<count($bits); $x++) {
if($bits[$x] <= $inp) {
$inp = $inp - $bits[$x];
$store[$x] = 1;
} else {
$store[$x] = 0;
}
}
$store_rvs = array_reverse($store);
if($a % 8 === 0) {
echo "</table></td><td><table>";
}
echo "<tr>";
for($b=0; $b<count($store_rvs); $b++) {
if($store_rvs[$b] == '1') {
echo "<td class='cell blk'></td>";
} else {
echo "<td class='cell wht'></td>";
}
}
echo "</tr>";
}
echo "</table></td></tr></table>";
everyone I have the following file and I want to show lines that match on if condition and pass the other.
I have this TXT file:
Doc. number|Date|Price|Description|Name
100|11/11/2015|99|Test 1|Alex
101|11/11/2015|120|Test 2
102|11/11/2015|100|Test 3|John
102|11/11/2015|140||
103|11/11/2015|110|Test 4|
And this is my PHP code:
$file_handle = fopen("file.txt", "rb");
$i = 0;
echo "<table border='1'>";
echo "<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>";
while (!feof($file_handle)) {
$line_of_text = fgets($file_handle);
$parts = explode('|', $line_of_text);
if($i > 1) { // Pass the first line
echo "<tr>";
echo "<td>" . $parts[0] . "</td>"; // Doc. number
echo "<td>" . $parts[1] . "</td>"; // Date
echo "<td>" . $parts[2] . "</td>"; // Price
echo "<td>" . $parts[3] . "</td>"; // Description
echo "<td>" . $parts[4] . "</td>"; // Name
echo "</tr>";
}
$i++;
}
fclose($file_handle);
echo "</table>"
How I can check if there are no "Description" and/or "Name" in table and pass this line. I want to show(get) only line that match on if condition.
I will be very grateful if someone have idea. Thanks in advance.
As simple as
$file_handle = fopen("file.txt", "rb");
$i = 0;
echo "<table border='1'>";
echo "<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>";
while (!feof($file_handle)) {
$line_of_text = fgets($file_handle);
$parts = explode('|', $line_of_text);
if($i > 1 && !empty($parts[3]) && !empty($parts[4])) { // Pass the first line and lines without description / name
echo "<tr>";
echo "<td>" . $parts[0] . "</td>"; // Doc. number
echo "<td>" . $parts[1] . "</td>"; // Date
echo "<td>" . $parts[2] . "</td>"; // Price
echo "<td>" . $parts[3] . "</td>"; // Description
echo "<td>" . $parts[4] . "</td>"; // Name
echo "</tr>";
}
$i++;
}
fclose($file_handle);
echo "</table>"
Only print table row if we have name and description:
if($i > 1 && $parts[3] && $parts[4]) {
You can put condition before echo statement and if it will be false just skip "echo";
if (count($parts) === 5) {
$error = 0;
foreach ($parts as $part) {
if (empty($part)) error++;
}
if($i > 1 && $error === 0) {
echo "<tr>";
echo "<td>" . $parts[0] . "</td>"; // Doc. number
echo "<td>" . $parts[1] . "</td>"; // Date
echo "<td>" . $parts[2] . "</td>"; // Price
echo "<td>" . $parts[3] . "</td>"; // Description
echo "<td>" . $parts[4] . "</td>"; // Name
echo "</tr>";
}
}
I've a solution that can help you.
But why I think you need just scape the heading line. so I changed if($i > 1) to be if($i >0)
$file_handle = fopen("file.txt", "rb");
$i = 0;
echo "<table border='1'>";
echo "<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>";
while (!feof($file_handle)) {
$line_of_text = fgets($file_handle);
$parts = explode('|', $line_of_text);
if($i > 0) { // Pass the first line
if ( (!empty($parts[3])) && (!empty($parts[4])) ){
echo "<tr>";
echo "<td>" . $parts[0] . "</td>"; // Doc. number
echo "<td>" . $parts[1] . "</td>"; // Date
echo "<td>" . $parts[2] . "</td>"; // Price
echo "<td>" . $parts[3] . "</td>"; // Description
echo "<td>" . $parts[4] . "</td>"; // Name
echo "</tr>";
}
}
$i++;
}
fclose($file_handle);
echo "</table>"
Your file has a CSV structure, pipe delimited.
So parse it as a CSV.
Note the using of array_shift to get the header of the CSV and passing the delimiter parameter to fgetcsv.
Also consider using implode instead explicitly passing each member of the array between td tags.
Here's an example using two functions, one for parsing the CSV and returning the data,
and another one for displaying the data and doing the validation.
function getData($file){
$rows = array();
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, null, "|")) !== FALSE) {
$rows[] = $data;
}
fclose($handle);
}
return $rows;
}
function displayData($rows){
$header = array_shift($rows);
$output = '<table border="1">' . PHP_EOL;
$output .= '<tr><th>' . implode('</th><th>',$header) . '</th></tr>' . PHP_EOL;
foreach($rows as $row){
if (isset($row[3]) and isset($row[4]) and $row[3]!='' and $row[4]!=''){
$output .= '<tr><td>' . implode('</td><td>',$row) . '</td></tr>' . PHP_EOL;
}
}
$output .= '</table>';
return $output;
}
$rows = getData("pipe.txt");
print displayData($rows);
This will output the following
<table border="1">
<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>
<tr><td>100</td><td>11/11/2015</td><td>99</td><td>Test 1</td><td>Alex</td></tr>
<tr><td>102</td><td>11/11/2015</td><td>100</td><td>Test 3</td><td>John</td></tr>
</table>
I have this block of text in an array:
"Stefan Olsson"
"Kungsvägen"
"Skolgatan"
xxxx-xx-xx
0735xxxxxx,
"Pär Davidsson"
"Skolgatan"
"Myntvägen"
xxxx-xx-xx
0709xxxxxx,
I parse this type of content to an CSV-file, for later usage in Excel. However, I want to fromat this text to fit in different columns in excell. So, when I open the CSV-file in Execel, I want the name to be in one column, the address in the column besides etcetc. How can I accomplish this? Should I use PHPExcel? Or could it be done with plain old PHP?
Here is my PHP-code
$gatunamn = $_POST['gata'];
$ort = $_POST['omrade'];
$csv_data = array();
$newSpider->fetchPage($gatunamn, $ort, $offset=0);
$obj = json_decode($newSpider->html);
echo "<div id='rightcontent'><table id='one-column-emphasis'>";
echo "<th><input type='checkbox' name='csv_all' id='csv_all'></th><th>Namn</th><th>Adress</th><th>Adress2</th><th>Adress3</th><th>Personnummer</th><th>Telefonnummer</th><th>Telefonnummer2</th>";
$antal_sidor = round($obj->search->wp->totalHits / $obj->search->wp->pageSize);
echo "<td></td>";
foreach($obj->search->wp->features as $fish) //Loopar ut 50st (pageSize)
{
echo "<tr>";
echo "<td><input type='checkbox' value='csv' class='csv'></td>";
echo "<td>" . $fish->name . "</td>";
$csv_data[] .= utf8_decode($fish->name);
foreach($fish->addresses as $ad)
{
echo "<td>" . $ad->label . " " . $ad->postcode . " " . $ad->area . "</td>";
$csv_data[] .= utf8_decode($ad->label . " " . $ad->postcode . " " . $ad->area);
}
if(!empty($fish->dateOfBirth))
{
$convert_date = substr($fish->dateOfBirth, 0, -3); //Gör om datum från timestamp
echo "<td>" . date("Y-m-d", $convert_date) . "</td>";
$convert_datee = date("Y-m-d", $convert_date);
$csv_data[] .= $convert_datee;
}
if(!empty($fish->phoneNumbers))
{
foreach($fish->phoneNumbers as $ph)
{
echo "<td>" . $ph . "</td>";
$csv_data[] .= $ph . ",";
}
}
echo "</tr>";
}
echo "</table>";
$j = 0;
for($i = 1; $i <= $antal_sidor; $i++)
{
echo "<a href='curl2.php?gatunamn=$gatunamn&ort=$ort&offset=$j'>" . $i . "</a> ";
$j += 100;
}
echo "</div>";
echo "<div id='debug'><pre>";
var_dump($csv_data);
echo "</pre></div>";
}
if(isset($_POST['export']))
{
$fp = fopen("eniroo.csv","w");
foreach(explode(",", implode("\n",$csv_data)) as $rad) {
fputcsv($fp, array(implode(',', str_getcsv($rad, "\n"))));
}
echo "<div id='csv_info'>";
echo "<a href='eniro.csv'>Hämta CSV-fil</a>";
echo "</div>";
}
// Restructure the original array into rows
$myDataArray = array_chunk($myDataArray, 5);
// Then write to CSV
$fp = fopen('file.csv', 'w');
foreach($myDataArray as $dataRow) {
fputcsv($fh, $dataRow);
}
fclose($fh)