I have been working on importing a csv file into a mysql database. Problem is my client gets the files already with some bad formatting. the rows end at 43 but the file goes down to 65078!!
When importing, the script takes forever. What can I do to make sure just the rows from 0 to 44 get imported? Here's the code i'm using.
<table>
<?php
//fgetcsv($handle, 1024, delimiter)
$file = fopen("prices.csv", "r");
$row = 0;
while($csv_line = fgetcsv($file,1024)) {
if($row == 4){
echo "How many headers do we have: ".count($csv_line)."<tr>";
foreach($csv_line as $header){
if($header !== NULL){
print "<td>$header</td>";
}
}
echo "</tr>";
}
if($row > 6) {
echo '<tr>';
for ($i = 0, $j = count($csv_line); $i < $j; $i++) {
echo '<td>'.$csv_line[$i].'</td>';
}
echo "</tr>\n";
} else {
$row++;
}
}
?>
</table>
If the number of filled rows isn't fixed, you can use similar approach with:
if (empty($line)) { break; }
Your comment give me an idea for a better solution:
Here http://php.net/manual/en/function.fgetcsv.php appears this "A blank line in a CSV file will be returned as an array comprising a single null field" so maybe this helps:
while(($csv_line = fgetcsv($file,1024)) && $csv_line[0]) {
Will read only until finds an empty row.
Add a if ($row >= 44) { break; } at the bottom of the loop so it'll exit once you reach row 44.
Related
I want to write a code to remove extra delimiters for each row for a .csv file. With another code i have already determined that the .csv file only contains rows with too many delmiters. I further know which column (after nth delimiter) has extra delimiters. I have already written most of the code, but it's not working yet. Help is very much appreciated.
My PHP skills are still basic.
<?php
$delimiter = ';'; //type delimiter
$delimiter_start_column =23; //column-to-be-cleaned starts after this delimiter
$exp_delimiter =63; //expected delimiters per row
$total_delimiter =substr_count($line,$delimiter); //total delimiters in row
$delimiter_end_column =($exp_delimiter - $delimiter_start_column) + ($total_delimiter - $exp_delimiter); //column-to-be-cleaned ends before this delimiter
function splitleft($line,$delimiter,$delimiter_start_column){
$max = strlen($line);
$n = 0;
for($i=0;$i<$max;$i++){
if($line[$i]==$delimiter){
$n++;
if($n>=$delimiter_start_column){
break;
}
}
}
$arr[] = substr($line,0,$i);
return $arr;
}
function splitright($line,$delimiter,$delimiter_end_column){
$max = strlen($line);
$n = 0;
for($i=0;$i<$max;$i++){
if($line[$i]==$delimiter){
$n++;
if($n>=$delimiter_end_column){
break;
}
}
}
$arr[] = substr($line,$i,$max);
return $arr;
}
// determine start time in microseconds for runtime calculation
$file['datestamp'] = date("Y-m-d_H-i-s", $start);
$input['folder'] = 'input\\';
$input['file'] = ''; //enter filename
$output['folder'] = 'output\\';
$output['file_cleaned'] = $file['datestamp'].'_cleaned_';
// open input file read-only
$handle['input'] = #fopen($input['folder'].$input['file'], "r");
// initialize line, clean and dirty counters
$counter['total'] = 0;
$counter['cleaned'] = 0;
if($handle['input']) {
// open output files. set point to end of file.
$handle['cleaned'] = #fopen($output['folder'].$output['file_cleaned'].$input['file'], "a");
while(($line = fgets($handle['input'])) !== false) {
// increment line counter
$counter['total']++;
$result = substr_count($line, $delimiter);
if($result == $exp_delimiters AND $counter['line'] != 1 AND $line != $header) {
// if the number of delimiters matches the expected number as represented by $exp_delimiters
// increment clean lines counter
$counter['cleaned']++;
$output_file = $handle['cleaned'];
}
else {
// else, if the number of delimiters does not match the expectation
// remove extra delmiters from column
$line_cleaned = splitleft + str_replace(";","",substr($line,strlen(splitleft()),(strlen($line)-strlen(splitleft())-strlen(splitright()))) + splitright());
$output_file = $handle['cleaned'];
}
// prefix line number
$line = $counter['total'].$delimiter.$line;
// write line to correct output file
fwrite($output_file, $line_cleaned);
// output progress every 20.000 processed lines
if($counter['total'] % 20000 == 0) {
echo number_format($counter['total'], 0, ',', '.')."\r\n";
}
}
if(!feof($handle['input'])){
echo "Error: unexpected fgets() fail\n";
}
// close all input and output files
foreach($handle AS $close) {
fclose($close);
}
}
?>
I am trying to limit the printing of table cells to 3 per row. This worked in one example, but clearly not working when I tried to use the same code somewhere else in the site. This is the code:
$n=3;
echo "<table cellpadding='10' cellspacing='10' style='margin-right:-70px;'><tr>";
$users_count = count($users);
for($i=0; $i<$users_count;$i++)
{
$temp = array();
$temp = $users[$i];
echo "<td>";
echo "<div id='kitchen_box'>";
echo "<div id='kitchen_box_details'>";
echo "<h4>".$temp->fullname . "</h4><br>";
if(strcmp($temp->address, '') == 0)
echo $temp->city;
else
echo $temp->address.", ".$temp->city;
echo "</div>";
echo "<div id='kitchen_box_pic'><img id='kitchen_image' src='".$temp->profilepic."' /></div>";
echo "</div>";
echo "</td>";
if($i != 0){
if($i % $n == 0 && $i != $users_count-1){
echo "</tr><tr>";
}
else{
echo ""; //if it is the last in the loop - do not echo
}
}
}
echo "</table>";
I can't see why this wouldn't work! I would really appreciate support on the matter :)
There are several issues with your code. But your main issue is that you're using a zero-based increment, but doing a 1-based check. So, a table of your the results of an $i!=0 && $i%$n==0 goes like this:
$i $result
0 false
1 false
2 false
3 true
So, you see, the result closes the row after the fourth, not the third cell. To fix this, change the line to:
if($i % $n == $n-1 && $i != $users_count-1){
You should also include a closing </tr> tag with your closing </table> tag.
Incidentally, you shouldn't give the same ID to multiple elements on a page. Each of your kitchen_box and kitchen_box_div DIV tags will have the same ID. If you want this for CSS, use classes. Otherwise, you might try adding the value of $i to each ID.
Nitpicking on request:
The line $temp = array(); seems a little pointless, especially since you don't want $temp to be an array, but an object.
The else{ echo ""; } lines are also redundant.
You don't need the if($i != 0) check now because that case will not pass the next test anymore.
Otherwise the code seems fine to me.
I think the problem is that your $i starts at zero.
Let's look at your conditions before creating a new row :
if($i != 0){
if($i % $n == 0 && $i != $users_count-1)
If $i = 0, the first condition isn't matched. Then the second isn't for $i = 1 and $i = 2. And then your script creates another ... in your first row before matching your if conditions for the first time.
I guess you could move this part of the code right after the beginning of the for instructions :
for($i=0; $i<$users_count;$i++)
{
if($i != 0)
{
if($i % $n == 0 && $i != $users_count-1){
echo "</tr><tr>";
}
else{
echo ""; //if it is the last in the loop - do not echo
}
}
// Echo your <td> ... </td>
}
echo "</tr></table>";
I'm printing out a table with 27 cols from a database, so its obvious that it'll be aesthetically displeasing if 27 cols were visible on my screen. so this is one of the conditions i've been using to see if a particular col is empty, if it is empty then the table header will not be printed and if that isnt printed another if isset condition will not print the table data. But it isn't working out as planned. These are the variations i've tried and none of them are working P.S. $result = number of rows being returned by the query.
$i = 1;
while ($i <= $result)
{
if (!empty($array['Others'][$i]))
{
$others = print "<th>Others</th>";
break;
}
$i++;
}
$i = 0;
while ($i <= $result)
{
$emptyothers = !empty($array['Others'][$i]);
if ($emptyothers == '1')
{
$others= print "<th>Others</th>";
break;
}
$i++;
}
Your code should be like this:
$sql = mysql_query("SELECT * FROM table");
if (mysql_num_rows($sql) > 0) {
//your code...
} else {
print 'is empty';
}
Could you use array_key_exists()?
foreach($row in $result) {
if(array_key_exists('Others', $row)) {
if(!empty($row['Others']) {
print "<th>Others</th>";
break;
}
}
}
Got this code which is troubling me and am eager to accept any help!
$x=0;
while($row = mysql_fetch_array($result))
{
if ($me == $x)
{
echo "You are $me";
break;
}
else
{
$x++;
}
}
When I include the break; it returns :
You have selected the 1
However, when I remove the break; it returns
You have selected the 1 You have selected the 1 You have selected the 1 You have selected the 1
There are currently 6 records in the database and if the code were to work it would display "You are 4th"
Any ideas?
Your $x++; only executes on else. In order to have it increment on every iteration you need to remove the else:
$x=0;
while($row = mysql_fetch_array($result))
{
if ($me == $x)
{
echo "You are $me";
break;
}
$x++;
}
Just a side note: $row doesn't seem to be related to $me and $x here. I'll assume your loop contains some other code that you've omitted, but this alone will probably answer your question.
You are icrementing $x in the else, this is never getting executed.
You need to do the increment in the same part as the echo.
So either:
$x=0;
while($row = mysql_fetch_array($result))
{
if ($me == $x)
{
echo "You are $me";
$x++;
}
else
{
$x++;
}
}
Or if you never need to increment $x without echoing:
$x=0;
while($row = mysql_fetch_array($result))
{
if ($me == $x)
{
echo "You are $me";
x++;
}
}
I have a xml file with 100 records, but I want it to limit it to just 5 records
for ($i=0;$i<=5;$i++) {
foreach($xml->entry as $result){
if ($result->updated == $result->published) {
}
}
}
When I put in the above code, it display one record 5 times.
Thanks
Jean
$count = 0;
foreach($xml->entry as $result)
{
if ($result->updated == $result->published) {
}
$count++;
if ($count++ == 5) break;
// if ($count++ == 5) break; think this might work aswell
}
It seems that the foreach loop runs only once, because there is only one entry, and the for loop prints it 5 times. If there were more than one, this code would print each entry 5 times. If $xml->entry is an array, you can do it like this:
for($i = 0; $i < 5; $i++) {
$result = $xml->entry[$i];
if($result->updated == $result->published) {
}
}
Check if there are more than one <entry> tags in your XML file.