I want to reorder my array from last bottom line to first on the top. I'm trying use array_reverse, but i fail all runs.
Anyone of you, have an idea how i could implement this into my code?
Everything works, but i only need array in reverse
Thanks
this is my code:
<?php
$f = fopen("list.txt", "r");
while (!feof($f)) {
$arrM = explode("###",fgets($f));
echo "<p align='center'><b><font color='red' size='6'>" . $arrM[5]. " PLN</font></b><br><b><font size='5'>" . $arrM[3]. "</b></font><br><a target='_blank' href='" . $arrM[1] . "'><img src=" . $arrM[2]. " width='100%' /></a><br><br><br><br></p>";
}
fclose($f);
?>
Try This
<?php
$data = [];
$f = fopen("list.txt", "r");
// store all data line by line in a 2D array
while (!feof($f)) {
$data[] = explode("###",fgets($f));
}
// reverse your data array
$arrM = array_reverse($data);
// echo data
foreach ($arrM as $row) {
echo "<p align='center'>
<b><font color='red' size='6'>" . $row[5]. " PLN</font></b><br>
<b><font size='5'>" . $row[3]. "</b></font><br>
<a target='_blank' href='" . $row[1] . "'><img src=" . $row[2]. " width='100%' /></a></p>";
}
fclose($f); ?>
Related
I have the following attempt at my function, but it's just printing out everything in the contacts.txt file on one line...
function contactsTable(){
$file = fopen("contacts.txt", "r") or die("Unable to open file!");
echo "<tr><th>";
while (!feof($file)){
echo "<tr><th>";
$data = fgets($file);
echo "<tr><td>" . str_replace(',','</td><td>',$data) . '</td></tr>';
}
echo "<tr><th>";
echo '</table>';
fclose($file);
}
contacts.txt example like this;
Row 1 is headers ---> [value1, value2, value3, value4]
Row 2 is data ---> [value5, value6, value7, value8]
Is it possible to change my function so that Row 1 is using <th> tags so they are formatted as headers and the rest of the rows go into <td> tags for table data? I've tried to amend the logic but can't seem to get it.
TIA
Here is your contactsTable() function that prints the first contacts.txt line as table header and some basic HTML formatting for easy reading/debugging.
function contactsTable() {
$file = fopen('contacts.txt', 'r') or die('Unable to open file!');
$row = 0;
echo '<table>' . PHP_EOL;
while (!feof($file)) {
$row++;
$data = fgets($file);
if ($row === 1) {
echo '<tr>' . PHP_EOL;
echo '<th>' . str_replace(',', '</th><th>', $data) . '</th>' . PHP_EOL;
echo '</tr>' . PHP_EOL;
} else {
echo '<tr>' . PHP_EOL;
echo '<td>' . str_replace(',', '</td><td>', $data) . '</td>' . PHP_EOL;
echo '</tr>' . PHP_EOL;
}
}
echo '</table>' . PHP_EOL;
fclose($file);
}
I 'm a total PHP beginner and need some help :-)
Have this xml:
http://pastebin.com/ZSpNPhXH
And this scrypt:
<?php
// Retrieve XML File
$file = file_get_contents('livescore-feed.xml');
// Parse XML with SimpleXML
$livescore_data = new SimpleXMLElement($file);
foreach ($livescore_data->league as $league) {
echo "<table class='table table-striped table-bordered table-condensed'>";
echo "<thead><tr><th colspan='6' id='league'>" . $league->attributes()->name . "</th></tr></thead>";
echo "<tbody>";
foreach ($league->match as $match) {
$status = $match->attributes()->status;
$home = $match->home->attributes()->name;
$away = $match->away->attributes()->name;
$score = $match->home->attributes()->goals . " - " . $match->away->attributes()->goals;
// If match not yet started, there will be a ":" in 'status' attribute
if (strpos($match->attributes()->status,':') !== false) {
$score = "-";
}
echo "<tr><td class='status' id='match'>" . $status . "</td><td id='match' colspan='2'>" .
$home . "</td><td class='score' id='match'>" . $score . "</td><td id='match' colspan='2'>" .
$away . "</td>
</tr>";
}
echo "</tbody></table>";
}
?>
How to show/parse "events" also
Thank you very much for your help and suggestions
In the same way you parse other data!
foreach( $livescore_data->league as $league )
{
(...)
foreach( $league->match as $match )
{
(...)
# ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
foreach( $match->events->event as $event )
{
echo $event['assist'] . '<br>';
echo $event['player'] . '<br>';
(...)
}
}
}
You have to perform a loop through $match->events->event and you can get each <event> attribute using $event[attribute] syntax. So, $event['extra_min'], $event['minute'], etc...
You have only to adapt the code to your table structure.
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)
I'm looking for a way to do a function with php when clicking on a link.
I mean to delete an item when clicking on this link. I got my code set up like this:
$files = glob("upload/*.*");
if(count($files) == 0){
echo "No files present";
} else {
foreach ( $files as $file ) {
$array = explode("/", $file);
//echo $array[1] . "<br>";
echo "<tr>";
echo "<td>" . $array[1] . "</td>";
echo '<td><img height="70" width="auto" src=" ' . $file . '" ></td>';
echo '<td>Delete item</td>';
echo "</tr>";
}
}
In your main script:
$files = glob("upload/*.*");
if(!count($files)){
echo "No files present";
} else {
foreach ($files as $file) {
$array = explode("/", $file);
echo "<tr>";
echo "<td>" . $array[1] . "</td>";
echo '<td><img height="70" width="auto" src=" ' . $file . '" ></td>';
echo '<td>Delete item</td>';
echo "</tr>";
}
}
In your delete script:
if (isset($_GET['file']) && $file = rawurldecode($_GET['file'])) {
if (file_exists('upload/' . $file))
unlink($file);
}
You might want to pass in another $_GET parameter (secret token) for added security - otherwise it'll open up your uploads directory to permanent threat. You can send a random sha1() hash or system-defined configuration value, and have your delete script expect a 40-char string.