PHPExcel row break not working - phpexcel

. . . .
I am using Codeigniter 3.1 and PHPExcel 1.8.
I have a function that creates a PHPExcel Object and returns it and the other function outputs the Excel to browser
Everything is working perfectly fine. Now on specific rows I need to add page breaks.
if($count == 4 || ($count > 4 && (($count - 4) % 6 == 0))){
//echo("A - $row <br>Count - $count<br><hr>");
$sheet->setBreak('A' . $row , PHPExcel_Worksheet::BREAK_ROW );
}
The echo is giving me my required rows, so the condition is working fine. The only issues is . . . . . page break not working. So any suggestions?
Following is the code used for generating the file
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file_name");
header("Cache-Control: max-age=0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel5");
$objWriter->save("php://output");

Problem solved. Initially I was using the setbreak function while inserting rows. and whenever the condition meets, call the function. I was going through the code, got an idea, implemented it and solved. Instead of calling the function setbreak during row generation, i stored the row reference in an array, and then after doing all page settings, at the end looped the array and called setbreak on the rows and it worked :). Thanks Mark as discussing with you has helped me twice now :)

Related

WC Ajax Product Filter - Price Range issue

I'm using WC Ajax Product Filter for filtering options .
Unfortunately the price range is showing :
Min Price: ₹NaN
Max Price: ₹NaN
The actual price is not displaying. Please help me out . I'm new to woocommerce .
The issue found out !
The minimum and maximum price range difference for the products was very less. For example - minimum was $65 and maximum was $66, that was the issue.
I've found a temporary solution, edit the plugin file: wcapf.php :you can find this in the root of the plugin directory: wc-ajax-product-filter
Now search for the code: getPriceRange
This occurs in 2 spots around line #467 and line #773
On both 2 spots you see an if statement below which uses sizeof() function.
There seems to be some sort of issue with this.
I have fixed the issues of NaN by commenting out the entire if and else statement that uses the sizeof() function, see below code for example.
It's not the best solution, but well it works, untill update.. I will post this to plugin creator as well.
#469:
//if (sizeof($unfiltered_price_range) === 2) {
#522:
//}
#781:
//if (sizeof($price_range) > 2) {
#840:
//} else {
// empty array
// return array();
//}
Solved it this way.
Edit widget-price-filter.php file.
It's in the folder /ajax-product-filter/widgets/
Replace the following code in line 125:
<span class="wcapf-slider-value" id="wcapf-noui-slider-value-min"></span> - <span class="wcapf-slider-value" id="wcapf-noui-slider-value-max"></span>
By:
if ($min_val!=0 && $max_val!=0 ) {
echo '<span class="wcapf-slider-value" id="wcapf-noui-slider-value-min"></span> - <span class="wcapf-slider-value" id="wcapf-noui-slider-value-max"></span>';
} else{ echo '-';}?>
It will replace the NaN values bellow the slider by (-), just change the last line if you want another text there.

Fetch a post under certain no of words or characters in wordpress?

Is it possible to fetch a post with content under 140 characters or 25 words ?
if possible how to do it
here is my random post code
// Random post link
function randomPostlink(){
$RandPostQuery = new WP_Query(array('post_type'=>array('tip'),'posts_per_page' => 1,'orderby'=>'rand'));
while ( $RandPostQuery->have_posts() ) : $RandPostQuery->the_post();
echo the_permalink();
endwhile;
wp_reset_postdata();
}
Character count is easy, you can just add the condition AND CHAR_LENGTH(post_content) < 140 to your where clause.
Word count is more difficult because there is no built in MySQL function for counting words. You can find simple solutions that don't work in every use case as well as complete solutions that use stored functions. I'll use a simple solution for the sake of example.
What you need to do is add a filter to the where clause and apply your additional conditions there:
add_filter( 'posts_where', 'venki_post_length_limit' );
function venki_post_length_limit($where = '') {
remove_filter( 'posts_where', 'venki_post_length_limit' );
$where .= ' AND (
CHAR_LENGTH(post_content) < 140 OR
(LENGTH(post_content) - LENGTH(REPLACE(post_content, ' ', ''))+1) < 25
) ';
return $where;
}
Notice that I remove the filter as soon as the function is called. This is so you don't apply this same condition to every query.
You should also be aware that both of those conditions are costly compared to a simple lookup on a column value (especially the word count). Neither can utilize indexes. If you have a large number of posts you may run into performance issues if you're running this query frequently. A better solution might be to calculate the word and character count when the post is created/updated and store that as meta data.

Static content before loop

Am new to XQuery. I have written below query which is working perfectly and producing results in csv format from an
xml stored in Marklogic.
xquery version "0.9-ml"
let $data := someQuery
return
for $i in $data
return
fn:string-join((
$i/PATHTOFILED_1/text(),
$i/PATHTOFILED_2/text(),
.
.
.
.
$i/PATHTOFILED_N/text()
),","
)
Output:
abc,def,adc,
dff,eef,ddf,
.
.
.
.
fff,eed,ddg,
I have a new requirement to add static headers before the data.
And the expected output will be like
Expected Output:
HEAD1, HEAD2, HEAD3,
abc,def,adc,
dff,eef,ddf,
.
.
.
.
fff,eed,ddg,
Just adding HEAD1, HEAD2, HEAD3, in the top row. The headers are not part of the XML. They should be part of query in simple Strings with static data and can be modified at anytime in query itself.
I have tried to add the below code segment to the query. But the query is not running in Marklogic query console.
concat("HEAD1,"HEAD2","HEAD3"),
Any solution is highly appreciated.
Thanks in advance.
Add it before the FLWOR expression:
xquery version "1.0-ml";
string-join(("HEAD1","HEAD2","HEAD3"),","),
let $data := someQuery
return
for $i in $data
return
fn:string-join((
$i/PATHTOFILED_1/text(),
$i/PATHTOFILED_2/text(),
.
.
.
.
$i/PATHTOFILED_N/text()
),","
)
BTW, I really wouldn't use to 0.9-ml dialect unless you have a very compelling reason to do so. It works, but it is really only still there for compatibility.

mysql_fetch_row loop?

I set a session variable upon login, I want to find all rows in a table that have the username in the "Createdby" field and I want to list them on a page. I'm using this code:
<?php
$result = mysql_query("SELECT email FROM members WHERE createdby = '" . $_SESSION['myusername'] ."'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($row = mysql_fetch_row($result))
{
echo ($row[0]);
}
?>
It works great but it doesn't space them, it echoes out like: data1data2 and not separate like data1, data2. How can I customize the results without messing it up? I tried to add
echo ("<p>".$row[0]."</p>");
But received: 11, I'm kind of new to PHP.
This is simply string concatenation. You're adding strings to other strings.
I'd suggest the following just to get started:
echo $row[0].", ";
I also suggest you read up on PHP more, as this is a basic concept in the language.
You can add spaces between each $row[0] like this:
echo $row[0] . ' ';
The dot followed by a space means that you're concatenating a space.

Why does calling createSheet twice in a row corrupt doc in PHPExcel?

$workbook = new PHPExcel();
$workbook->createSheet()->setTitle('whatt')->setCellValue( 'A1', 'Another sheet' );
$workbook->createSheet()->setTitle('whatt')->setCellValue( 'A1', 'Another sheet' );
$objWriter = new PHPExcel_Writer_Excel2007($workbook);
$objWriter->setOffice2003Compatibility(true);
ob_start();
$objWriter->save( "php://output" );
$contents = ob_get_contents();
ob_end_clean();
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
$size = count($contents);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-Type: application/vnd.msexcel; charset=utf-8');
header("Content-Disposition: attachment; filename=\"test.xlsx\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: {$size}");
echo $contents;
exit;
When I download the sheet it comes down as 1 byte, with one sheet where the first cell is "P" ( because the file only contains that letter )
If I comment out the second createSheet line it works perfect. Any idea?
EDIT
I accepted the answer below which told me it wasn't PHPExcel's fault and it wasn't.
If I remove the headers and keep output buffering, as a test, it still works.
ob_start();
$objWriter->save( "php://output" );
$contents = ob_get_contents();
ob_end_clean();
file_put_contents( "/mypath/uploads/test.xlsx", $contents );
I can download the file no problem. This leads me to believe the headers are screwing it up somehow, which is very bizarre considering there's no issue if I only used createSheet once. I guess I'll post that here when I figure it out.
....
And I figured out why the headers were messing it up. Apparently using count($contents) with one added sheet was fine, but with two added sheets it would only return 1. I changed it to strlen($contents) and it worked. Bonus points if anyone can tell me why that happened :)
No idea without a lot of debugging. Get rid of the redundant output buffering, and it works. This is the cause of your problem, nothing in PHPExcel.
So what are you doing subsequently with $contents?
Calling createSheet() twice in a row isn't corrupting anything: though why are you trying to create a workbook containing two worksheets with the same title anyway? PHPExcel will actually rename the second added worksheet as whatt2 to avoid problems.

Resources