PHP expression "and-statement"? - php

I have this csv file which has 2 rows.
The first one shows the city name and 2nd shows a number (statistic)
Example:
[City] [Count of dogs]
[Los Angeles] [100]
[New York] [-]
Now my problem is that I don't want to show the city's and the statistic from it that have [-] as [Count of dogs].
This is what I've tried right now:
<?php
echo"<table border='1'>";
if (($handle = fopen("table.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
echo "<tr>";
$num = count($data);
for ($i=0; $i < $num; $i++) {
$string = $data[$i];
$m = preg_match("#\w{2,}#", $string, $match);
if ($m) {
echo "<td>". $data[$i];
$m = preg_match("#\d{2}#", $string, $match);
if ($m) {
echo $data[$i] . "<br /> </td>";
}
}
}
echo"</tr>";
}
fclose($handle);
}
echo"</table>";
?>
It doesn't show the "-" right now but it does show the city, I was wondering if a "and-statement" exist for expressions, or is there a different way to do this?
Picture of how it looks right now: [IMG]http://i59.tinypic.com/14sl4xg.jpg[/IMG]

IS it just "-"? Then you can check for it...
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
if ($data[1] != "-") {
echo "<tr>";
....
echo "</tr>";
}
}

If, count of dog field is the filtering field, then take that field first before display,
Here I have checked for numeric, because the count of dog is always in numeric, You may use any other logic as you have done using preg_match,
<?php
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
if(is_numeric($data[1])) {
//proceed your logic here
}
else {
continue;//to go for the next row
}
?>

Assuming your CSV looks like your example, you can skip the rows where "Number of Dogs" is a dash.
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
if( $data[1] == '-' ) {
continue;
}
echo "<tr>";
// ...
}
Or if the dash can also be any other non-number:
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
if( !is_numeric($data[1]) ) {
continue;
}
echo "<tr>";
// ...
}
Also, I don't know what the other if checks are for, but it looks to me you want to just echo the data
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
if( $data[1] == '-' ) {
continue;
}
echo "<tr>";
$num = count($data);
for ($i=0; $i < $num; $i++) {
echo "<td>" . $data[$i] . "</td>";
}
echo"</tr>";
}

Check the occurrences of - in the second column using strpos function. If the second column doesn't contain any -, then print the row
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
$num = count($data);
if (strpos($data[$num-1],'-') === false) {
echo "<tr>";
for ($i=0; $i < $num; $i++) {
$string = $data[$i];
$m = preg_match("#\w{2,}#", $string, $match);
if ($m) {
echo "<td>". $data[$i];
$m = preg_match("#\d{2}#", $string, $match);
if ($m) {
echo $data[$i] . "<br /> </td>";
}
}
}
echo"</tr>";
}
}

Related

PHP CSV showing in table

I want to show this CSV File the same way as the picture but then in PHP.
Right now I know how to show it if they were all under "A",
but I'm stuck now on how to show it with more then one cell.
<?php
echo "<table border='1'>";
if (($handle = fopen("top10.csv", "r")) !== FALSE) {
$data = fgetcsv($handle, 0, ",");
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
echo "<tr>";
$num = count($data);
for ($i=0; $i < $num; $i++) {
echo"<td>" . $data[$i] . "<br /> </td>";
}
echo "</tr>";
}
fclose($handle);
}
?>
This is how I did it with 2 rows.
Image:
Result right now:
You might want to use SplFileObject which can parse CSV files. For example:
$file = new SplFileObject("/path/to/your/csv/file.csv");
$file->setFlags(SplFileObject::READ_CSV);
$rows = "";
foreach ($file as $row) {
list ($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l) = $row;
$rows .= <<<HTML
<tr>
<td>${a}</td>
<td>${b}</td>
<td>${c}</td>
<td>${d}</td>
<td>${e}</td>
<td>${f}</td>
<td>${g}</td>
<td>${h}</td>
<td>${i}</td>
<td>${j}</td>
<td>${k}</td>
<td>${l}</td>
</tr>
HTML;
}
echo "<table>".$rows."</table>";
PHP Documentation
you read file 2times in your code:
$data = fgetcsv($handle, 0, ",");// 1st (just remove this line)
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) { // 2nd
and another solution
try this:
echo "<table border=\"1\">";
$row=#explode("\n",#file_get_contents('test.csv'));
if(count($row)>0) foreach($row as $r){
$col=#explode(",",$r);
echo "<tr>";
foreach($col as $c){
echo "<td>$c</td>";
}
echo "</tr>";
}
echo "</table>";

Read and load two CSV files

I have two very small CSV files that I want to load into array and compare each row and their individual cells, and if there is a change in any cell highlight it.
At the moment I am writing a script to load files and display these onto the browser as they appear on the files.
CSV structure:
My PHP Code:
<?php
$row = 1;
$row2 = 1;
if (($file = fopen("Workbook1.csv", "r")) !== FALSE && ($file2 = fopen("Workbook2.csv", "r")) !== FALSE ) {
while (($data = fgetcsv($file, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row Orginal File: <br /></p>\n";
$row++;
for ($i=0; $i < $num; $i++) {
echo $data[$i] . "<br />\n";
}
while(($data2 = fgetcsv($file2, 1000 , ",")) !== FALSE){
$num2 = count($data2);
echo "<p> $num2 fields in line $row2 Updated File: <br /></p>\n";
$row2++;
for ($x=0; $x < $num2; $x++) {
echo $data2[$x] . "<br />\n";
}
}
}
fclose($file);
fclose($file2);
}
?>
Browser Result:
Not to sure why the Array is structured like above image as i understand fgetcsv() read line by line.
Any one can see my my stake in code..?
You are not closing the first while loop in the right place.
Try this
<?php
$row = 1;
$row2 = 1;
if (($file = fopen("Workbook1.csv", "r")) !== FALSE && ($file2 = fopen("Workbook2.csv", "r")) !== FALSE ) {
while (($data = fgetcsv($file, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row Orginal File: <br /></p>\n";
$row++;
for ($i=0; $i < $num; $i++) {
echo $data[$i] . "<br />\n";
}
} // end first while loop
while(($data2 = fgetcsv($file2, 1000 , ",")) !== FALSE){
$num2 = count($data2);
echo "<p> $num2 fields in line $row2 Updated File: <br /></p>\n";
$row2++;
for ($x=0; $x < $num2; $x++) {
echo $data2[$x] . "<br />\n";
}
}
fclose($file);
fclose($file2);
}
?>
After reading this enter link description here
I managed to fix it by adding this line of code at the top of the file before fopen()
ini_set('auto_detect_line_endings',TRUE);
If you need to set auto_detect_line_endings to deal with Mac line
endings, it may seem obvious but remember it should be set before
fopen, not after:
Having this fixers how can i Compare the values between these two files..?

SQL being executed twice in FOR loop

I think I need a second pair of eyes on this one. For the life of me I can't figure out why my SQL INSERT query is running twice every iteration:
if (($handle = fopen($spreadsheet_url, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($current_row == 1 || $current_row == 2) {
$num = count($data);
//echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
//echo $data[$c] . "<br />\n";
try {
set_time_limit(0);
$stmt = $db_temp_kalio->prepare('INSERT INTO invupdate (sku,prod) VALUES(:sku, :prod)');
$stmt->execute(array(':sku'=> $data[0], ':prod'=> $data[1])); }
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
exit; }
}
}
$current_row++;
}
fclose($handle);
}
Well, I probably should have put forth a little more effort before asking for assistance. I was able to fix this by adding a row reset counter in the loop:
if (($handle = fopen($spreadsheet_url, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($current_row == 1 || $current_row == 2) {
$row_reset = 0;
$num = count($data);
//echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
//echo $data[$c] . "<br />\n";
if ($row_reset == 0) {
try {
set_time_limit(0);
$stmt = $db_temp_kalio->prepare('INSERT INTO invupdate (sku,prod) VALUES(:sku, :prod)');
$stmt->execute(array(':sku'=> $data[0], ':prod'=> $data[1])); }
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
exit; }
}
$row_reset++;
}
}
$current_row++;
}
fclose($handle);
}
I'm still curious if there is a better way.
I looks like you were using some code to iterate through your columns, but actually only have two, containing the sku and prod ($data[0] and $data[1].) If that is the case, then you don't need the for loop inside. (BTW, it was that loop that was causing the query to be executed twice, as the query was inside it.) It also looks like you have two counters going for the row (current_row and row) so those can be combined. If you are using the if ($currentRow == 1... statement to ignore the header on row 0 and then only process 2 rows, you will want to switch it to if ($currentRow > 0) when you are ready to run the whole spreadsheet. Using break in the catch clause instead of exit will cause the code to drop out of the while loop and hit the fclose statement:
if (($handle = fopen($spreadsheet_url, "r")) !== FALSE) {
$currentRow = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($currentRow == 1 || $currentRow == 2) {
$num = count($data);
//echo "<p> $num fields in line $currentRow: <br /></p>\n";
//echo "<p> The data for this row is: " . $data[0] . " " . $data[1] . "\n";
try {
set_time_limit(0);
$stmt = $db_temp_kalio->prepare('INSERT INTO invupdate (sku,prod) VALUES(:sku, :prod)');
$stmt->execute(array(':sku'=> $data[0], ':prod'=> $data[1]));
}
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
break;
}
}
$currentRow++;
}
fclose($handle);
}

Create HTML table from multiple lines of strings

I want to take a text file formatted like so:
*note I can change the format of the stored messages, basically there are delimiters and different lines
;68.229.164.10:4/5/2013:Hello
;71.73.174.13:4/6/2013:Oh Hey
(;IPADDRESS:TIMESTAMP:MESSAGE)
and put it in a table that looks like so:
IP Time Message
68.229.164.10 4/6/2013 Hello
71.73.174.13 4/6/2013 Oh Hey
I prefer to use something like the following:
http://php.net/manual/en/function.fgetcsv.php
And then format the output accordingly.
From Example 1 on the above referenced page:
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
so, you could do something like this...
<?php
$row = 1;
if (($handle = fopen("path_to_your_data_file", "r")) !== FALSE) {
echo '<table>';
echo '<tr><td></td>IP<td>Time</td><td>Message</td></tr>';
while (($data = fgetcsv($handle, 1000, ":")) !== FALSE) {
$num = count($data);
$row++;
if ($num > 2) {
echo '<tr>';
for ($c=0; $c < $num; $c++) {
echo '<td>'.$data[$c].'</td>';
}
echo '</tr>';
}
}
echo '</table>';
fclose($handle);
}
?>
$a=";68.229.164.10:4/5/2013:Hello
;71.73.174.13:4/6/2013:Oh Hey";
preg_match_all('{;(.*?):(.*?):(.*)}',$a,$d);
//set th
$d[0][0]='IP';
$d[0][1]='TIME';
$d[0][2]='Message';
$table = '<table>'.PHP_EOL;
foreach($d AS $tr){
$row = PHP_EOL;
foreach($tr AS $td){
$row .= "<td>{$td}</td>".PHP_EOL;
}
$table .= "<tr>{$row}</tr>".PHP_EOL;
}
$table .= "</table>".PHP_EOL;
echo $table;

formatting fgetcsv output

Im trying to figure out how to take the data returned by fgetcsv, and format it into a readable/editable table, and then use fputcsv to save that table
so far i have this
<?php
$row = 1;
$handle = fopen("csv.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "\n";
}
}
fclose($handle);
?>
The following code will output the CSV in an html table. To make it editable wrap the echo ..$val.. with tags and add a php form handler that takes the result and reforms a CSV
<?php
$row = 1;
$handle = fopen("csv.csv", "r");
echo("<table>");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo("<tr>\r\n");
foreach ($data as $index=>$val) {
echo("\t<td>$val</td>\r\n");
}
echo("</tr>\r\n");
}
echo("</table>");
fclose($handle);
?>

Categories