Im using this PHP to display the contents or text files held in a directory, the text files all follow the same format.
$contents = file_get_contents($filename);
$items = explode('¬', $contents);
echo '<table width="500" border="1" cellpadding="4">';
foreach ($items as $item) {
echo "<tr><td>$item</td></tr>\n";
}
echo '</table>';
There are 7 $items in each text file:
tag,name,description,text1,text2,text3,date
So instead of outputting $item, can i give each its own variable?
Thanks.
Try this:
$contents = file_get_contents($filename);
list($tag, $name, $description, $text1, $text2, $text3, $date) = explode('¬', $contents);
echo '<table width="500" border="1" cellpadding="4">';
echo "<tr><td>$tag</td></tr>\n";
echo "<tr><td>$name</td></tr>\n";
echo "<tr><td>$description</td></tr>\n";
echo "<tr><td>$text1</td></tr>\n";
echo "<tr><td>$text2</td></tr>\n";
echo "<tr><td>$text3</td></tr>\n";
echo "<tr><td>$date</td></tr>\n";
echo '</table>';
Try:
while(list($tag,$name,$desc,$text1,$text2,$text3,$date) = each($items){
// do something
}
Treat the text file as a csv file seperated by the ¬ char.
http://www.php.net/manual/en/function.fgetcsv.php
Then you can either rely upon a header file to describe the fields ( columns ) or access them numerically.
Related
Ty mail2bapi, :)
Im using SimpleXLSX and example script below in code i added this $x = number_format($r);
Just need the numbers to have thousand separator
252252732
to
252,252,732
IM not good with PHP, really appreciate any help
Plus some columns are empty and dates like so 23.01.2020, I think this is what is causing the issue
XMLS File
simplexlsx
Error: number_format() expects parameter 1 to be double, array given in
Error: implode(): Invalid arguments passed
<?php
require_once 'SimpleXLSX.php';
if ( $xlsx = SimpleXLSX::parse('book.xlsx') ) {
echo '<table border="1" cellpadding="3" style="border-collapse: collapse">';
foreach( $xlsx->rows() as $r ) {
$x = number_format($r);
echo '<tr><td>'.implode('</td><td>', $x ).'</td></tr>';
}
echo '</table>';
// or $xlsx->toHTML();
} else {
echo SimpleXLSX::parseError();
}
?>
You are not using number_format() function correctly. Change following code from -
$x = number_format($r);
to
$x = number_format($r, 0, ".", ",");
for more information visit PHP number_format
EDIT
As you mentioned your row could have a different type of values, it is better to check the value for numeric.
Try this code
<?php
require_once 'SimpleXLSX.php';
if ( $xlsx = SimpleXLSX::parse('book.xlsx') ) {
echo '<table border="1" cellpadding="3" style="border-collapse: collapse">';
foreach( $xlsx->rows() as $rowValue ) {
echo "<tr>";
// As $rowValue is an array
foreach($rowValue as $value){
// Check for number_format
if(is_numeric($value)){
$x = number_format($value, 0, ".", ",");
echo "<td>".$x."</td>";
}else{
echo "<td>".$value."</td>";
}
}
echo "</tr>";
}
echo "</table>";
// or $xlsx->toHTML();
} else {
echo SimpleXLSX::parseError();
}
Hope these resolve your issue.
I am using file_get_contents() to import a text file.
In the text file, the format goes as below (example):
3434,83,8732722
834,93,4983293
9438,43933,34983
and so forth... basically it follows the pattern: integer, comma to split it, second integer, another comma to split it, third integer, then a new line begins. I need to get this into a table with the format following accordingly. So in other words, I would have a 3 column table and each new line in the text file would be a new row in the table.
This must be transcoded into a simple html table with <table> <tr> and <td>
I have never worked with multidimensional arrays and splitting text with that. This is why I'm seeking assistance. I really appreciate it! :)
You can do following :
$filename = 'abc.txt';
$content = file_get_contents($filename);
$explodedByBr = explode('<br/>', $content);
$table = "<table border='1'>";
foreach ($explodedByBr as $brExplode) {
$explodedByComma = explode(',', $brExplode);
$table .= "<tr>";
foreach ($explodedByComma as $commaExploded) {
$table .= "<td>" .$commaExploded. "</td>";
}
$table .= "<tr/>";
}
$table .= "</table>";
echo $table;
abc.txt has data in following format :
3434,83,8732722
834,93,4983293
9438,43933,34983
<?php
$file = 'path/to/file.txt';
echo '<table>';
while(!feof($file)) {
$line = fgets($file);
echo '<tr><td>' . implode('</td><td>',explode(',',$line)) . '</td></tr>';
}
echo '</table>';
?>
Try this:
Read the file into an array and then column'ize it by processing each line of the array by passing it through array_walk.
<?php
function addElements( &$v, $k ) {
$v1 = explode( ',', $v ); // break it into array
$v2 = '';
foreach( $v1 as $element ) {
$v2 .= '<td>'.$element.'</td>';
// convert each comma separated value into a column
}
$v = '<tr>'.$v2.'</tr>'; // add these columns to a row and return
}
// read the whole file into an array using php's file method.
$file = file( '1.txt' );
// now parse each line of the array so that we convert each line into 3 columns.
// For this, i use array_walk function which calls a function, addElements,
// in this case to process each element in the array.
array_walk( $file, 'addElements' );
?>
<html>
<head></head>
<body>
<table border="0">
<?php echo implode('',$file);?>
</table>
</body>
</html>
Hope it helps. See the php doc for file and array_walk. These are simple and convenient functions.
I want to put my outputted PHP into a table but it doesn't like it...
The layout I want is like this
ROW - Desctiption URL
ROW2- Meta Description
And it needs to keep this same layout even when I enter more then one URL.
What happens now is that when I input lots of urls it puts all of the same parts into the same cell.. How can I change this?
Sorry its hard to explain...
Here is the code:
<?php
ini_set( "display_errors", 0);
//make the array
$TAarray = explode("\n", strip_tags($_POST['TAData']));
foreach ($TAarray as $key => &$line) {
$line = trim($line);
// get the meta data for each url
$tags = get_meta_tags($line);
echo '<tr>';
echo (isset($tags['description']))?"<tr><td>Description($line)</td> </tr>".$tags['description']:"<tr><td>Description($line)</td></tr><tr><td>No Meta Description</td></tr>.";
echo '</tr>';
}
?>
You've got some awkward HTML there.. Try doing it like this:
//start the table
echo "<table>";
foreach ($TAarray as $key => &$line) {
$line = trim($line);
// get the meta data for each url
$tags = get_meta_tags($line);
//start a new row in the table
echo '<tr>';
if(isset($tags['description'])){
//add two cells to the table.
echo "<td>Description($line)</td>";
echo "<td>{$tags['description']}</td>";
} else {
echo "<td>Description($line)</td>"
echo "<td>No Meta Description</td>";
}
echo '</tr>';
}
echo "</table>";
//make the array
$data='http://stackoverflow.com
http://ebay.com/';
$TAarray = explode("\n", strip_tags($data));
echo "<table>";
foreach ($TAarray as $key => &$line) {
$line = trim($line);
// get the meta data for each url
$tags = get_meta_tags($line);
echo '<tr>';
if(isset($tags['description'])){
echo "<tr><td>Description($line):".$tags['description']:" </td> "; else{
"</tr><tr><td>Description($line)</td></tr><tr><td>No Meta Description</td></tr>.";
echo '</tr>';
}
}
echo "</table>";
I have a php file that contains some data when checked displays the checked data. How do I increment the variable "$visit" every time it is visited and save it to the text file?
new.php
<html>
<body bgcolor="#99FF00">
<table border="1">
<FORM ACTION="new.php" METHOD="POST">
Enter maximum price <input type="text" name="maximumprice"/>
<p><input type="submit" value="Go" name="Go"/>
</form>
<?
$mPrice = $_POST['maximumprice'];
$file1 = "properties.txt";
$filedata = fopen ($file1, "r");
$array1 = file ($file1);
print "<form action=\"visit.php\" method=\"POST\">";
for ($counter1 = 0; $counter1 < count($array1); $counter1++)
{
$arrLine = $array1[$counter1];
$pCode = getvalue ($arrLine, 0);
$price = getvalue ($arrLine, 1);
$picture = getvalue ($arrLine, 2);
$visit = getvalue ($arrLine, 3);
if ($price < $mPrice)
{
print "<tr>";
print "<td>";
print $pCode. "<br>";
print $price. "<br>";
//print $picture. "<br>";
print $visit. "<br>";
print "<input type=\"checkbox\" name=\"box[]\" value=\"$arrLine\" />";
print "</td>";
print "<td>";
printf("<img src='$picture' width='200' height='150'>");
print "</td>";
print "</tr>";
}
}
print '<input type="submit" name="Visit" value="Visit"/>';
// Move the form outside the for loop
print "</form>";
fclose ($filedata);
function getvalue ($text, $arrNo)
{
$intoarray = explode (",", $text);
return $intoarray[$arrNo];
}
?>
</table>
</body>
</html>
this is the second page, display.php
<html>
<body bgcolor="#99FF00">
<?
foreach ($_POST['box'] as $values)
{
echo "$values <hr/>";
}
?>
</body>
</html>
Step 1: Use a database :-)
No, but really, since you are storing the entire line as the value of the checkbox, you can compare that to the line in the file and update your visit field for matching lines ...
For example, in your processing file:
$filename = "properties.txt";
$data = file($filename, FILE_IGNORE_NEW_LINES);
$checked = $_POST['box'];
foreach($data as $id => $line){
if(in_array($line, $checked)){
//Explode the line into parts
$tmp = explode(',', $line);
//Increment the visit field
$tmp[3]++;
//Put the updated data back in the file data array
$data[$id] = implode(',', $tmp);
//Unset the tmp array
unset($tmp);
}
}
//implode the data back into a text block
$data = implode("\n",$data);
file_put_contents($filename, $data);
This is untested, but should yield what you are looking for ...
As a side note, you do not have to do the fopen call to use the file function. It will open and read the file.
EDIT: Since it appears that visit is the last column in each row and without any flags, the file function will keep the newlines at the end of each line, I added the appropriate flag to the file function.
My php function looks like that
function generateTour ($lang, $db)
{
echo '<table border="0" cellpadding="0" cellspacing="0" width="100%">';
$title='title_'.$lang;
$txt='txt_'.$lang;
$result = $db->query("SELECT id, $title, $txt FROM 1_table");
$count= $result->num_rows;
$i=1;
while($row=$result->fetch_object())
{
if($i%3==0) echo '<tr>';
echo '<td width="33%">
<div class="tour_item" style="background-image:url(core/content/img/pages/1/slide/'.$row->id.'.png)">
<div class="tour_item_title"><a href="?id='.$row->id.'">';
echo '</a></div><div class="tour_item_text"><a href="?id='.$row->id.'"></div>
</div>';
if($i==$count-1) echo '</td></tr>';
else if($i%3==0) echo '</td></tr><tr>';
$i++;
}
echo '</table>';
}
As you see it echoes result line by line. Not all at once. I want to collect all variables to one array and return this array. Is it possible? how to do it?
Please don't post your ideas about security holes .. etc. I'm filtering $title, $txt varibales against sql injections. (i have array for of possible field names. My filter function checks theese variables' values every time. )
$data = array();
while ($row = $result->fetchObject()) {
$data[] = $row;
}
Is the absolutely most basic method of cacheing a result set in an array. You'd just modify this to store your generated html instead.
Try this:
function generateTour ($lang, $db)
{
$output = '<table border="0" cellpadding="0" cellspacing="0" width="100%">';
$title='title_'.$lang;
$txt='txt_'.$lang;
$result = $db->query("SELECT id, $title, $txt FROM 1_table");
$count= $result->num_rows;
$i=1;
while($row=$result->fetch_object())
{
if($i%3==0)
{
$output .= '<tr>';
}
$output .= '<td width="33%">
<div class="tour_item" style="background-image:url(core/content/img/pages/1/slide/'.$row->id.'.png)">
<div class="tour_item_title"><a href="?id='.$row->id.'">
</a></div><div class="tour_item_text"><a href="?id='.$row->id.'"></div>
</div>';
if($i==$count-1)
{
$output .= '</td></tr>';
}
else if($i%3==0)
{
$output .= '</td></tr><tr>';
}
$i++;
}
$output .= '</table>';
return $output;
}
Edit:
Now it's easier to read the code. This solution put all content in one variable. You don't need a array.
You can use ob_start() and ob_get_clean() to buffer all output into a variable, then output that variable, like this:
ob_start();
print "Hello"; // Prints to buffer instead of screen
$out = ob_get_clean(); // Contains: Hello
echo $out; // Prints: Hello