php to display csv file in a paginated format - php

I am new to PHP and, I have a CSV file which, am displaying in my web page. I want the CSV file to be displayed with the pagination option so that the web page would look nice. This is the code I have so far.
<?php
$names = file('demo.csv');
$page = $_GET['page'];
//constructor takes three parameters
//1. array to be paged
//2. number of results per page (optional parameter. Default is 10)
//3. the current page (optional parameter. Default is 1)
$pagedResults = new Paginated($names, 20, $page);
echo "<ul>";
while($row = $pagedResults->fetchPagedRow()) {
//when $row is false loop terminates
echo "<li>{$row}</li>";
}
echo "</ul>";
//important to set the strategy to be used before a call to fetchPagedNavigation
$pagedResults->setLayout(new DoubleBarLayout());
echo $pagedResults->fetchPagedNavigation();
?>
However, the CSV file gets displayed with the commas in the screen. Let us consider the below example. Let's assume we have 40 records in my csv file. The contents of the CSV file are as below.
Author1,Name1,Name2,email
1,John,Smith,john.smith#gmail.com
2,Jack,Gibbs,Jack.gibbs#gmail.com
3,Mike,Dell,Mike.dell#gmail.com
and so on.
In my web page, I am getting the output in 2 pages (as I have set my pagination option to display 20 records in each page.
$pagedResults = new Paginated($names, 20, $page);
The output however still contains the comma from the original CSV file. I want my output to be like below.
First Page:
Author1 Name1 Name2 Email
1 John Smith john.smith#gmail.com
and so on.
Second Page:
Author1 Name1 Name2 Email
and so on.

This is because you are pulling the line as a row, but not parsing the line and outputting it cleanly.
The easiest solution is to parse it and then output it separated by div's with each row wrapped in a div. Then make the whole thing pretty with CSS.
Like this:
<?php
$names = file('demo.csv');
$page = $_GET['page'];
/*
Constructor takes three parameters:
1. array to be paged
2. number of results per page (optional parameter. Default is 10)
3. the current page (optional parameter. Default is 1)
*/
$pagedResults = new Paginated($names, 20, $page);
echo "<div class='CSVtable'>";
while($row = $pagedResults->fetchPagedRow()) {
$data = str_getcsv($row);
$dataRow = implode("</div><div class='csvCol'>", $data);
echo "<div class='csvRow'><div class='csvCol'>{$dataRow}</div>";
}
echo "</div>";
//important to set the strategy to be used before a call to fetchPagedNavigation
$pagedResults->setLayout(new DoubleBarLayout());
echo $pagedResults->fetchPagedNavigation();

Related

How do I make a data automatically move to a new page if the data is separate in the php excel?

I have dynamic data. So my data can change
besides that I also have a data signature that cannot be separated.
This signature data must be on the same page. See the red mark in this image :
It is a unit and cannot be separated. Signature data
must be on the same page
My problem is because my data is dynamic. This makes the position of the signature data can be located in any position. See image below :
Because my data increases, the position of the headmaster in the signature data is separate
How do I make signature data (see picture 1), which red marks automatically move to the next page if the data is separate?
You can set manual page breaks with something like this:
<?php
//All lines are written from the sheet code at this moment
//The code will insert a page break and the repeated header
//Page margins
$sheet->getPageMargins()->setTop(0.5);
$sheet->getPageMargins()->setRight(0.75);
$sheet->getPageMargins()->setLeft(0.75);
$sheet->getPageMargins()->setBottom(1);
//Use fit to page for the horizontal direction
$sheet->getPageSetup()->setFitToWidth(1);
$sheet->getPageSetup()->setFitToHeight(0);
$headerItems = array(); //add your header items here as array
$headerRowsHeight = 0;// calculated height of header and images of the top
$rowCounter = 100; //get last written row
//add (other) modifier of page hight here
$pageHeight=25+50 + $headerRowsHeight; //Current used page height Header + Footer + $headerRowsHeight
$reset = $pageHeight; //If you will have the firstpage diffrent change reset value and/or pageheight
$pageMaxHeight = 980 ; //Maximale page height DIN A4 arround this
$pageClearance = 15; //Clearance of footer
//Iterate trough all written lines
for ($row = 1; $row <= $rowCounter; ++$row) {
$height=15.1; //standard row height
//get the row height
$dim = $sheet->getRowDimension($row)->getRowHeight();
//get special cell heights (non standard)
if($dim != -1){
$height=$dim;
}
//add height for line to pageheight = get current used space
$pageHeight = $pageHeight + $height;
//Check if the space is still in the range of page
$leftOverSpace = $pageMaxHeight-$pageHeight;
//Change $pageClearance to your preferd space before footer
if( $leftOverSpace < $pageClearance){
//Set pagebraek
$sheet->setBreak('A'.$row, \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::BREAK_ROW);
//Reset page height
$pageHeight=$reset;
//Add page header to new page
createHeader($sheet, $row+1, $headeritems);
}
}
//Creates a header for every page
function createHeader($sheet, $row, $texts){
$count = $row;
// Iterate trough the header text array
foreach($texts as $text){
//Insert a new line with header
$sheet->insertNewRowBefore($count, 1);
//Do your header stuff here
$count++;
}
//Add two lines after the header
$sheet->insertNewRowBefore($count, 2);
//Return row number
return $count;
}
?>
Place not that the calculation is not 100% exact and that you must adapt this to your code.

Simple html dom always loading the default first page and not the specified url

I want to scrape few web pages. I am using php and simple html dom parser.
For instance trying to scrape this site: https://www.autotrader.co.uk/motorhomes/motorhome-dealers/bc-motorhomes-ayr-dpp-10004733?channel=motorhomes&page=5
I use this load the url.
$html = new simple_html_dom();
$html->load_file($url);
This loads the correct page. Then I find the next page link, here it will be:
https://www.autotrader.co.uk/motorhomes/motorhome-dealers/bc-motorhomes-ayr-dpp-10004733?channel=motorhomes&page=6
Just the page value is changed from 5 to 6. The code snippet to get the next link is:
function getNextLink($_htmlTemp)
{
//Getting the next page links
$aNext = $_htmlTemp->find('a.next', 0);
$nextLink = $aNext->href;
return $nextLink;
}
The above method returns the correct link with page value being 6.
Now when I try to load this next link, it fetches the first default page with page query absent from the url.
//After loop we will have details of all the listing in this page -- so get next page link
$nxtLink = getNextLink($originalHtml); //Returns string url
if(!empty($nxtLink))
{
//Yay, we have the next link -- load the next link
print 'Next Url: '.$nxtLink.'<br>'; //$nxtLink has correct value
$originalHtml->load_file($nxtLink); //This line fetches default page
}
The whole flow is something like this:
$html->load_file($url);
//Whole thing in a do-while loop
$originalHtml = $html;
$shouldLoop = true;
//Main Array
$value = array();
do{
$listings = $originalHtml->find('div.searchResult');
foreach($listings as $item)
{
//Some logic here
}
//After loop we will have details of all the listing in this page -- so get next page link
$nxtLink = getNextLink($originalHtml); //Returns string url
if(!empty($nxtLink))
{
//Yay, we have the next link -- load the next link
print 'Next Url: '.$nxtLink.'<br>';
$originalHtml->load_file($nxtLink);
}
else
{
//No next link -- stop the loop as we have covered all the pages
$shouldLoop = false;
}
} while($shouldLoop);
I have tried encoding the whole url, only the query parameters but the same result. I also tried creating new instances of simple_html_dom and then loading the file, no luck. Please help.
You need to html_entity_decode those links, I can see that they are getting mangled by simple-html-dom.
$url = 'https://www.autotrader.co.uk/motorhomes/motorhome-dealers/bc-motorhomes-ayr-dpp-10004733?channel=motorhomes';
$html = str_get_html(file_get_contents($url));
while($a = $html->find('a.next', 0)){
$url = html_entity_decode($a->href);
echo $url . "\n";
$html = str_get_html(file_get_contents($url));
}

I have to manually add array values after split on img tag

I got articles that I split on the first image tag, so I can put the image in a container-fluid (full width of the page instead of a standard bootstrap format). This works fine, except when I add more images, the array is further split, and so just having $article[0] (introtext) $article[1] (full width image) and $article[2] (rest of the text) is not enough. Since the next image and text are in value 3 and 4.
How can I make sure all the following data after the first split stays inside value 2? Or maybe automatically check how long the array is and add those values dynamically?
My code now:
// Product \\ Content
$content = "SELECT * FROM `lb_content` WHERE alias = '".$conn->real_escape_string($_GET['alias'])."' ";
$contentcon = $conn->query($content);
$contenti = array();
while ($contenti[] = $contentcon->fetch_array());
$introtext = $contenti[0]['introtext'];
preg_match_all('/(<img[^>]+\>)/i', $introtext, $artikelimages);
$artikelimages = $artikelimages[0];
$splitartikel = preg_split('/(<img[^>]+\>)/i', $introtext, -1, PREG_SPLIT_DELIM_CAPTURE);
echo $splitartikel[0];
echo $splitartikel[1]; (inside container fluid but ill just add the relevant code)
echo $splitartikel[2];
Now when there is another image, I have to add:
echo $splitartikel[3];
echo $splitartikel[4];
manually. For the new image and text after that image.
After echoing the first two pieces, join the rest of the array (if any) and echo the result:
echo implode(array_slice($splitartikel,2));

PHP To Show Title From Referring URL

I need to display title from referring URL and here is the code I'm using to achieve that:
<?php
if (isset($_SERVER['HTTP_REFERER'])) {
$url_to_load = $_SERVER['HTTP_REFERER'];
$f = file_get_contents($url_to_load);
$p1 = strpos($f, "<title>");//position start
$qe = substr($f, $p1);//string from start position
$p2 = strpos($qe, "</title>");//position end
$query = substr($qe, 7, $p2-2);//cuts from start position +7 (<title>) untill end position -2...
echo $query;}
else{
$ref_url = 'No Reffering URL'; // show failure message
}//end else no referer set
echo "$ref_url";
?>
When i visit page with this code from URL that has the following code:
<title>Title Of Referrer</title>
Code works, but there is still the piece of the closing tag and when i check source code this is what i'll get:
Title Of Referrer</tit
What i need to change to remove the closing tag completely?
$query = substr($qe, 7, $p2-7);//cuts from start position +7 (<title>) untill end position -2...
You only subtract 2 at the end on end title but you add 7 on start title.
Try the code above and see if that works
EDIT:
Another solution is to do like this.
$query = strip_tags(substr($qe, 0, $p2));
This saves all of the title tags but then delete them with strip_tags()
EDIT2:
There are some other things in the code I would suggest.
$f = file_get_contents($url_to_load);
$query = strip_tags(substr($f, strpos($f, "<title>"), strpos($f, "</title>")));
This code brings it down to two lines of code and uses fewer variables. You can also get ridd of $f, but it may be useful to something else and it's only one variable.

How to crawl a site with server-generated content?

I am writing a simple php crawler that gets data from a website and inserts it into my database. I start with a predefined url. Then I go through the the contents of the page (from php's file_get_contents) and eventually use file_get_contents on links of that page. The url's I am getting from the links are fine when I echo them and then open them from my browser on their own. However, when I use file_get_contents and then echo the result, the page does not appear correctly because of errors related to dynamically created server-side data from the site. The echo'd page contents do not include the listed data from the server that I need, because it cannot find necessary resources for the site.
It appears relative paths in the echo'd webpage are not allowing the desired content to be generated.
Can anyone point me in the right direction here?
Any help is appreciated!
Here is some of my code so far:
function crawl_all($url)
{
$main_page = file_get_contents($url);
while(strpos($main_page, '"fl"') > 0)
{
$subj_start = strpos($main_page, '"fl"'); // get start of subject row
$main_page = substr($main_page, $subj_start); // cut off everything before subject row
$link_start = strpos($main_page, 'href') + 6; // get the start of the subject link
$main_page = substr($main_page, $link_start); // cut off everything before subject link
$link_end = strpos($main_page, '">') - 1; // get the end of the subject link
$link_length = $link_end + 1;
$link = substr($main_page, 0, $link_length); // get the subject link
crawl_courses('https://whatever.com' . $link);
}
}
/* Crawls all the courses for a subject. */
function crawl_courses($url)
{
$subj_page = file_get_contents($url);
echo $url; // website looks fine when in opened in browser
echo $subj_page; // when echo'd, the page does not contain most of the server-side generated data i need
while(strpos($subj_page, '<td><a href') > 0)
{
$course_start = strpos($subj_page, '<td><a href');
$subj_page = substr($subj_page, $course_start);
$link_start = strpos($subj_page, 'href') + 6;
$subj_page = substr($subj_page, $link_start);
$link_end = strpos($subj_page, '">') - 1;
$link_length = $link_end + 1;
$link = substr($subj_page, 0, $link_length);
//crawl_professors('https://whatever.com' . $link);
}
}
Try advance html dom parser. It is here....
http://sourceforge.net/projects/advancedhtmldom/

Categories