Export specified text in CSV to HTML - php

I have a CSV file with this information: CSV Link
As you can see it is a lot of data here, and I am asking if it is possible to convert some of the data to a table in HTML. Like if I only would convert the data in "Kamp" "Række" "Navn1" "Navn2" and "Resultat"
Would it be possible or do I need to convert everything to html?
I have not access to edit the CSV file because it automatically imports new data.
Hopefully there are someone with a gifted mind here who has the solution.
If anything is not clear then please ask me.

You can start from here, you'll have the data of each row inside a php array, then you can echo what you want inside html.
<table>
<?php
$handle=fopen('filename.csv', r);
while($data=fgetcsv($handle, 1000, ';', '"')){?>
<tr>
<td><?php echo $data[0];?></td>
<td><?php echo $data[1];?></td>
<td><?php echo $data[2];?></td>
<td><?php echo $data[3];?></td>
</tr>
<?php }
fclose($handle);
?>
</table>
But I see you have too many " in your csv file.

Related

PHP + HTML in MySQL echo

This will in this case echo multiple authors, release dates, and covers for a book on a single page.
My question is: How do I make it look nice with a table, and my CSS file? I just can't get it right, kinda almost made it work once, but it showed just the one book and then started on a new row, and I want at least 4 books/row, and the borders were completely off.
Really silly question I know, and I've tried googling and experimenting but I'm having serious trouble making it really work.
Also, if I want to echo a variable ($genre) in a link-text, say
Home > Books > CurrentGenreVariable($genre)
how'd I do that neatly? I need a slight kick-start, that's all I think.
Thanks in advance.
You follow the generic looping patterns. I presume you're doing this with a while() loop?
<table>
<?php while ($row = FETCH_THE_DATA): ?>
<tr>
<td><?php echo $row['author']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['cover']; ?></td>
</tr>
<?php endwhile; ?>
</table>

php mysql display table

I am a beginner and somehow made to get the query (php & Mysql) I want and using echo i got the output as few lines without difficulty. But now I want the output inside the cell of a table. I tried something like this:
This does not work:
<tr>
<th>subject</th>
<th>grade</th>
</tr>";
echo "<tr>";
echo "<td>".$Row['name1']."</td>;
echo "<td>".$Row['subject1'].</td>";
echo "</tr>";
echo "</table>";
Whereas this work:
echo $line['name1']."<tr></td>"."";
echo $line['subject1']."<tr></td>"."";
The echo $line statement echoes the value of name1 and subject1 without any difficulty. but the echo Row is not showing the output. As my data has only one row I dont have to use any loop. I actually want two fields in first row (name1 and subject1) and then in next row the fields of name2 and subject2 and till name7, subject7. It looks like the format inside the table is wrong. Could someone help me plz?
First of all replace
echo "<td>".$Row['name1']."</td>;
with
echo "<td>".$Row['name1']."</td>";
you are missing (") at the end before (;)
Updated with the missing table tag. Try this
<?php
echo '<table>';
echo '<tr>';
echo '<th>subject</th>';
echo '<th>grade</th>';
echo '</tr>';
echo "<tr>";
echo "<td>".$Row['name1']."</td>";
echo "<td>".$Row['subject1']."</td>";
echo "</tr>";
echo "</table>";
?>
Just to expand the current answers, I'd suggest you use a single echo and concatenate the strings or even better, just use one single string and concatenate only the necessary variables:
<?php
echo '
<table>
<tr>
<th>subject</th>
<th>grade</th>
</tr>
<tr>
<td>'.$Row['name1'].'</td>
<td>'.$Row['subject1'].'</td>
</tr>
</table>';
?>
This of course works better if the amount of PHP code is greater than the amount of HTML code. But if you were to write more HTML than PHP, it'd make more sense to just open and close <?php?> tags and echoing the variable you want.
I used an answer instead of a comment for the sake of the example. Feel free to try this approach when you are dealing with several html elements and need to insert your values within them.

PHP export to excel file size is geting too much bigger than actual file size

I am exporting some data to excel by PHP. which merely around 17k records. All the MySQL queries are returning results in less than 4 mins of time. and I have not used any complex looping for the excel generation just as follows :
<? php foreach($this->records as $type=>$record)
{
?>
<table>
<tr><td><b>
<?
php ($type == 'installation')? $type1 = 'ABCD':$type1= 'EFGH';
echo strtoupper($type1);
?>
</b></td></tr>
</table>
<table>
<?php foreach ($record as $k=>$newrecords) {
?><tr>
<?php
foreach($newrecords as $id=>$var1) {
?><td <?php echo $class10; ?>> <?php echo " ".$var1.""; ?> </td>
<?php
} echo "</tr>";
} ?>
</table>
<?php
} ?>
File is taking too much of time to export and when gets generated it comes of 47.6 MB and take too much time to open. And when I open the file and save it without doing any change file size reduces to mere 400KB
If anyone knows anything about this issue please reply....
THANKS in advance.
Amit

What's the meaning of this syntax ?> <?php

<?php
for($i=0;$i<=100;$i++)
{
?> // why this
<tr>
<td> <?php echo $i; ?></td>
<td><?php echo "tên sách $i"; ?></td>
<td><?php echo "noi dung sach $i";?></td>
</tr>
<?php
}
?>
So that is the scenario I'm looking to understand. Thanks
The <?php opens a php script sequence so it is saying that inside this is php code. ?> closes that sequence and says that I am not longer using php code.
In your case the php opens up and starts a for loop. Inside of the for loop a table is made but it is done using html not php. Then in each table piece, php is being used to echo (write something to the screen) some content into the table. Then finally at the end the php for loop must be finished with a closed bracket. I hope that makes sense.
The meaning of that syntax, is essentially telling the server to stop processing PHP. What follows in the page, is HTML code. That is <?php tells the server process this as PHP script and the ?> says stop processing PHP script.
Normally, you'll not see HTML outputted in this manner, but instead using PHP's echo to write the HTML
<?php
for($i=0;$i<=100;$i++)
{
echo "
<tr>
<td>{$i}</td>
<td>\"tên sách {$i}\"</td>
<td>\"noi dung sach {$i}\"</td>
</tr>
";
}
?>
?> is the end tag of PHP much like </td> is an end tag of a table cell.
The PHP parser lets you enable and disable parsing by including the PHP start and end tags <?php and ?> in your document. These two samples have the same output:
One
-------------------
echo '<td>' . $foo . '</td>';
Two
-------------------
?>
<td><?php echo $foo; ?></td>
<?php
As far as which is easier to read, well, that's a matter of preference I suppose.
See also: the shortcut tag <?= for echoing a value.
You can come in and out of php execution any time you like. If text is not between php tags, it will be output rather than executed.
<?php // start executing
if($test){ ?> <!-- start a php if statement -->
<p>This will only be output if the php test is true</p>
<?php } ?> <!-- don't forget to close the if statement -->
<p>This will always be output</p>
PHP allows you to min PHP code and HTML markup in the same file, so it needs a way to tell them apart.
If you don't enclose your PHP code between <?php and ?>, it won't be processed, and will be output as HTML.
You should read a minimum of documentation before start a project (and ask basic questions).
First part (l.1 to 4) is PHP code, here you start a for loop.
Second part (l.5 to 9) is HTML code, which is include in the loop.
Third and last part is here to close the loop.
You can do the same with :
<?php
for($i=0;$i<=100;$i++)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>tên sách".$i."</td>";
echo "<td>noi dung sach".$i."</td>";
echo "</tr>";
}
?>
This is a way to embed the HTML code without having to use the php print or echo function. using
?>
Another way to get at the same result would be this:
";
echo " $i ";
echo " tên sách $i ";
echo " noi dung sach $i ";
echo "";
}
?>

sql, php, html table question

I want to grab data from a mysql database by using php. The data looks something like this:
apple 3
orange 2
banana 4
I want to take the data and put it in a html table and use css to make it look pretty, but I dont want to deal with it inside <?php ?>
After I grab the
$result = mysql_query("SELECT * FROM Table");
can I reference the result variable outside the <? php ?> tags?
No. PHP can only be done in <?php ... ?> or <?= ... ?>. Use a template engine such as Smarty if you want substitution in this manner.
in short, no you cant, it is a php variable (technically a resource in this case) so you have to parse it through the php engine, which requires the php tags
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr><td>'.$row['fruit'].'</td><td>'.$row['id'].'</td></tr>';
}
echo '</table>';
Short answer is no. HTML cannot deal with dynamic content.
If you want to cut down the amount of echo statements within your code you can store the html within a given variable and then make reference to it.
I find it better to do the following:
<table>
<?php foreach($result as $row): ?>
<tr>
<td><?php echo $row['fruit']?></td>
<td><?php echo $row['id']?></td>
</tr>
<?php endforeach; ?>
</table>
This provides clarity and minimizes concatenation.

Categories