I'm creating a table using PHP from a CSV file.
I am trying to sort the output (DESCENDING) by one column from the CSV file which is: $line_of_text[13]
But I have no idea how to this as it is not as straight forward as sorting the output if I was using mysql.
This is my entire code:
<?php
$file_handle = fopen("MY-CSV-FILE.csv", "r");
print "<table style='width:100%; float:left;'>\n";
while (!feof($file_handle) ) {
//print '<tr>';
$line_of_text = fgetcsv($file_handle, 1024);
$line_of_text = str_replace('http', '<a href=""><img title="Click To Enlarge" class="fancybox" style="width:210px; height=210px; float:left;" src ="http', $line_of_text);
$line_of_text = str_replace('jpg', 'jpg"/></a>', $line_of_text);
$line_of_text = str_replace(',', '', $line_of_text);
$line_of_text = str_replace('PictureRefs', '', $line_of_text);
asort($line_of_text);
foreach($line_of_text as $key => $value)
print "<tr><td>".$line_of_text[9].'</td><td>'.$line_of_text[10].'</td><td>'.$line_of_text[2].'</td><td>'.$line_of_text[5].'</td><td>'.$line_of_text[6].'</td><td>'.$line_of_text[7].'</td><td>'.$line_of_text[8].'</td><td>'.$line_of_text[3].'</td><td>'.$line_of_text[4].'</td><td>'.$line_of_text[11].'</td><td>'.$line_of_text[12].'</td><td>'.$line_of_text[13].'</td><td>'.$line_of_text[14].'</td><td>'.$line_of_text[15]."</td></tr>\n<tr><td width='100%' colspan='100'><div style=' width:100%;'>".$line_of_text[16]."</div></td></tr>";
}
print '</table>';
fclose($file_handle);
?>
As you can see I have started doing this:
asort($line_of_text);
foreach($line_of_text as $key => $value)
but I don't think this is correct!
could someone please advise on this ?
any help would be appreciated.
You need to load all of your rows into an array, sort, and then echo the table. You need to split this into two loops.
Related
I'm loading some columns from a csv file using php.
I am using divs to display the columns, but the issue is that the contents of the div for each heading is not perfectly aligned underneath the heading.
this is my php code:
<?php
$file_handle = fopen("myCSV.csv", "r");
while (!feof($file_handle) ) {
//print '<tr>';
$line_of_text = fgetcsv($file_handle, 1024);
print "<div style='float:left; margin-right:15px;'>".$line_of_text[2].' '.$line_of_text[3].' '.$line_of_text[4].' '.$line_of_text[5].' '.$line_of_text[6].' '.$line_of_text[7].' '.$line_of_text[8].' '.$line_of_text[9]."</div><br /> ";
}
fclose($file_handle);
?>
How can I align the columns under each related headings properly?
here is the output data:
http://jsfiddle.net/w9v5q8vp/
This is how I tried to use a table:
print "<table>\n";
print "<tr><td>".$line_of_text[2]."</td></tr><tr><td>".$line_of_text[3]."</td></tr>";
print '</table>';
When you output a table you should loop only over the rows:
$file_handle = fopen("myCSV.csv", "r");
print "<table>\n";
while (!feof($file_handle) ) {
//print '<tr>';
$line_of_text = fgetcsv($file_handle, 1024);
print "<tr><td>".$line_of_text[2].'</td><td>'.$line_of_text[3].'</td><td>'.$line_of_text[4].'</td><td>'.$line_of_text[5].'</td><td>'.$line_of_text[6].'</td><td>'.$line_of_text[7].'</td><td>'.$line_of_text[8].'</td><td>'.$line_of_text[9]."</td></tr>\n";
}
print '</table>';
fclose($file_handle);
The beginning/end table tags should be printed only once.
You can read more about table tr td tags at w3schools.
I'm reading a text file of email addresses and outputting the domain only (with the # symbol). I need to alphabetize the list and then output to display on screen
Here is my code thus far:
<?php
$file_handle = fopen("file.txt", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
$parts = explode("#", $line);
$Id = $parts[count($parts) - 1];
echo "#" . $Id . "<br>";
}
fclose($file_handle);
?>
How can I initiate a sort to alphabetize the list?
This should work for you:
(Here I just get every line of the file with file(). Then I go through each line with array_map() where I only return the domain into the array $lines. At the end I sort the array with sort() and print it)
<?php
$lines = array_map(function($v){
return "#" . explode("#", $v)[1];
}, file("test.txt"));
sort($lines);
foreach($lines as $line)
echo $line . "<br />";
?>
Example input/output:
a.b#x.com
a.c#a.de
e.s#b.cu
#a.de
#b.cu
#x.com
I have created the code below and it all works completely fine my problem is that is will add as it goes along and display everything whereas i just need the last array element i have tried array_pop and the end function to no avail any ides?
$Count = 0;
$file_handle = fopen("test2.txt", "rb");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(' ', $line_of_text);
$arr = array($parts[8]);
for($i=0;$i<count($arr);){
$count = $count + $arr[$i]/1024;
$results= array($count);
}
echo '<p>';
print_r($results);
echo '</p>';
}
fclose($file_handle);
Try using end()
echo '<p>';
end($results);
echo '</p>';
Assuming your array is numeric, couldn't you simply only display the last element like this:
$lastRec=count($results);
print_r($results[$lastRec]);
I am using WP Alchemy to create a meta box with multiple check boxes.
The trouble I'm having is, I have a csv list of academic courses that I want to create the check box options from. So I'm taking the csv turning it into an array that the boxes are made from. However with the code I have now, only the LAST line is created in the array.
<?php
$file_handle = fopen('curriculum.csv', 'r');
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(',', $line_of_text);
$items = array ($parts[2] .$parts[3], );
}
fclose($file_handle);
?>
<?php foreach ($items as $i => $item): ?>
<?php $mb->the_field('cb_ex3'); ?>
<!-- similar to test #2, the same thing can be accomplished by simply
adding array brackets "[]" to the name -->
<input type="checkbox" name="<?php $mb->the_name(); ?>[]" value="<?php echo $item; ?>"<?php $mb->the_checkbox_state($item); ?>/> <?php echo $item; ?><br/>
<?php endforeach; ?>
So out of the few hundred lines of code, all I get is the last line.
Here is the actually file I'm working with: http://www.ouhsd.k12.ca.us/educational_services/curriculum/curriculum.csv
In each iteration of the loop you replace $items when you want to add to it, thus it only having the last value.
Also http://php.net/fgetcsv might be of some use to you.
You should be using the fgetcsv() (docs) function rather than trying to make sense of the CSV data yourself. Also, you should build the $items array (rather than overwriting it) as you loop over the file, adding a new item each time.
$file_handle = fopen('curriculum.csv', 'r');
fgets($file_handle); // this skips the csv header line
while (($parts = fgetcsv($file_handle)) !== FALSE) {
$items[] = $parts[2] . ' ' . $parts[3];
}
fclose($file_handle);
foreach ($items as $item) {
// Your HTML code goes here
echo $item . PHP_EOL;
}
Bonus points (you'll look cooler, using objects and iterators!)
$file = new SplFileObject('curriculum.csv');
$file->setFlags(SplFileObject::READ_CSV);
foreach (new LimitIterator($file, 1) as $parts) {
$items[] = $parts[2] . ' ' . $parts[3];
}
foreach ($items as $item) {
// Your HTML code goes here
echo $item . PHP_EOL;
}
items should be an array in your case. Initialize it outside the while loop
$items = array();
And then inside the while loop
$items[] = array ($parts[2] .$parts[3], );
should work.
Also look at fgetcsv as well as suggested.
You need an extra dimenson to your array.
$i=0;
$items= new array(); // i think you need this for some fun reason
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(',', $line_of_text);
$items[$i] = array ($parts[2] .$parts[3], );
}
You are overwriting the $items array. I would like to suggest a much simpler method to
<code><pre><?php
// in one step, load the datafile as an array, and loop over each line
// then explode the line by comma, and add to $lines array
foreach(file('curriculum.csv') as $line)
$lines[] = explode(',',$line);
// display the structure of the captured data
print_r($lines);
?></pre></code>
I think once you understand the structure of the captured data here, you can use the values as you wish in your output loop.
code, pre and php tags added for convenience
I need to loop through some records within an excel file and output them as headings for an HTML table but I don't know what is the best approach as I am new to PHP.
Each the column headings begins as a Title followed by an index so you have:
Country1, Country2, Country3 ....through to Country50.
I would like to have a more automatic way to retrieve these values rather than stick to the
$result .= "$Country1";
each must also contain the id of the country which should also be converted into an array i.e. The other thing so they do not require to be printed if that's the case, so the results could be
<th id="Country1">Value of Country1</th>
<th id="Country2">Value of Country2</th>
<th id="Country3">Value of Country3</th>
<th id="Country8">Value of Country8</th>
<th id="Country24">Value of Country24</th>
<th id="Country30">Value of Country30</th>
What is the best "code light" approach to doing this?
Regards!
something like that should work:
<?php
$handle = #fopen("/tmp/myfile.csv", "r");
if ($handle) {
// first line should be the header, we make it an array of strings
if ( ($buffer = fgets($handle, 4096)) !== false )
$headers[] = explode(",", $buffer);
else
echo "Error: empty file...\n";
// second line should be the values, for each line we output the xml markups
while ( ($buffer = fgets($handle, 4096)) !== false )
{
$values[] = explode(",", $buffer);
if( count($headers) != count(values) )
echo "Error: the 2 lines do not have same number of columns...\n";
else
{
for( $i = 0 ; $i < count($headers) ; $i++ )
echo "<th id='".$headers[$i]."'>".$values[$i]."</th>\n";
}
}
else
echo "Error: second line empty...\n";
if ( !feof($handle) )
echo "Erreur: fgets() failed...\n";
fclose($handle);
}
?>