page-break-after with cycle for - php

I get information from an sql query and then I show it with a cycle for me it appears well the first hour but when I need to show more data it does not show it on the second page.
My code:
<div class="ax7 " style="page-break-after: always; ">
<?php
$num = 1;
for ($i = 0; $i < (count($beneficiarios)); $i++) {
$li = $beneficiarios[$i];
$usuario = $li->Act_escala;
$nombre = $li->Act_Nombre;
$fecha = $li->Act_FechaInicio;
?>
<p>
<?php echo $num . '.-' . $usuario. ' ' . $nombre ?> , a contar del
<?php echo $fecha ?> </p>
<?php
$num++;
}
?>
</div>
also on the first page shows me the information to the end of my sheet and I do not know how to upload it and continue to show the information on the second page from the top: 0

I try the following :
#media print {
footer {page-break-after: always;}
}

Related

PHP Output my XML feed in a random order

I have my XML output working fine but it's in latest date order. What would be cool is if I could get it in a random order. I've tried using shuffle and I just can't get it working (nothing seems to happen).
Here's an example of the code I'm using to build my XML. The output is HTML with PHP to insert variables.
// Build the Feed
$feed = "http://www.reviewswebsite.com/api/consumer-reviews/?username=" . $userName . "&format=xml&reviews_per_page=20&page=" . $pageNumber;
$xml = simplexml_load_file($feed);
for($i = 0; $i < $numberOfForLoops; $i++)
{
$reviewer_name = $xml->reviews->review[$i]->reviewer_name;
$date_of_work = date('l, d m Y' , strtotime($xml->reviews->review[$i]->date_of_work));
$average_reviewer_rating = (float)$xml->reviews->review[$i]->average_rating;
..
?>
<div><?php echo $reviewer_name; ?></div>
...
<?php ;} ?>
#The Fourth Bird helped me by passing this link: Randomize SimpleXML object results
The end result I created for Trust-A-Trader reviews which I built into a Joomla! module was. I'll try to release the module for free on the Joomla! Extensions Directory (JED).
End code was
$feed = "http://www.trustatrader.com/api/consumer-reviews/?username=" . $userName . "&format=xml&reviews_per_page=20&page=" . $pageNumber;
$xml = simplexml_load_file($feed);
// Count Reviews, if total number of reviews is less than the total shown on page set in module then show the lesser amount to avoid an error.
$totalNumberOfReviews = count($xml->reviews->review);
$numberOfForLoops = $totalNumberOfReviews;
if ($totalNumberOfReviews > $numberOfReviews) {
$numberOfForLoops = $numberOfReviews;
}
// If the module sets the order to random then shuffle array, or else do in latest first date order.
if ($params->get('Order') == 0) {
foreach($xml->reviews->review as $val)
$array[]= $val;
shuffle($array);
}
// Loop through the reviews and output them.
foreach($array as $val)
{
$i = 0;
if ($i < $numberOfForLoops)
{
?>
<div class="mod_trust_a_trader_reviews--review g-grid" itemscope itemtype="http://schema.org/Review" itemprop="review" >
<div class="review g-block size-100" itemprop="reviewBody">
<?php echo $val->comments; ?>
</div>
<div class="authorBlock g-grid size-100">
<span class="author g-block size-100" itemprop="author">
<?php echo $val->reviewer_name; ?>
</span>
</div>
<div class="score g-block size-100 g-grid">
<div class="g-block size-100">
<?php starRatingImage($val->average_rating); ?>
</div>
<div class="g-block size-100 reviewRating">
<?php echo '(<span itemprop="reviewRating">' . $val->average_rating . '</span>)'; ?>
</div>
</div>
</div>
<?php
}
$i++;
}
?>

show div tag in php statement

I am now working on a WordPress theme base with the Advanced Custom Fields plugin, And I want to show a <div> tag when the if statement is true. Here is my code:
<?php
$rows = get_field('classification');
$sort = get_sub_field('sort');
$row_count = count($rows);
for ($i = 1; $i <= $row_count; $i++)?>
<?php if ( $i==1 || $i%5==0) { ?>
<div class="bor"></div>
<h3 style="text-align:center">
<?php echo $sort; ?>
<a id="browser"></a></h3>
<div class="bor"></div>
<?php } ?>
or something like that
<?php
$rows = get_field('classification');
$fenlei = get_sub_field('fenlei');
$row_count = count($rows);
for ($i = 1; $i <= $row_count; $i++)?>
<?php if ( $i==1 || $i%5==0) { ?>
echo '<div class="bor"></div>';
echo '<h3 style="text-align:center">';
<?php echo $fenlei; ?>
echo '<a id="browser"></a></h3>';
<div class="bor"></div>
<?php } ?>
But the content of the div tag doesn't show.
Any reply is appreciated!Thank you very much.
some of your echo statement are out php tags. use this:
<?php
$rows = get_field('classification');
$fenlei = get_sub_field('fenlei');
$row_count = count($rows);
for ($i = 1; $i <= $row_count; $i++){
if ( $i==1 || $i%5==0) {
echo "<div class='bor'></div>
<h3 style='text-align:center'>".$fenlei."
<a id='browser'></a></h3>
<div class='bor'></div>";
}
}
?>
If you wanted to show the html in php, i suggest you use the below code.
<?php
$rows = get_field('classification');
$fenlei = get_sub_field('fenlei');
$row_count = count($rows);
for($i = 1; $i <= $row_count; $i++){
if ( $i==1 || $i%5==0) {
echo '>div class="bor"<>/div<';
echo '>h3 style="text-align:center"<';
echo $fenlei;
echo '>a id="browser"<>/a<>/h3<';
echo '>div class="bor"<>/div<';
}
}
?>
I'm guessing you're using an ACF Pro Repeater field. in this case you need to use the_row() which will set the sub-field's correct content. look at this edited example from the Docs:
<?php
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):
// loop through the rows of data
while ( have_rows('repeater_field_name') ) : the_row();
// display a sub field value
the_sub_field('sub_field_name');
endwhile;
endif;
?>
so, I think your code should look more like:
<?php
if(have_rows('classification')):
while (have_rows('classification') ) : the_row();
// Your Code...
endwhile;
endif;
?>
And, I've learned recently you need to count the rows outside the while loop. othewise it won't catch the rows amount.

Pagination on the API Indeed.com xml feed 25 results per page, How to Paginate?

I have set up the Indeed.com xml feed on my site. Their API only allows 25 results per query. How can I paginate the results if there are more than 25?
I have not found a satisfactory or thorough enough answer anywhere online. I've searched for weeks on this.
Here is what I have in my code:
PHP:
// Indeed.com API URL parameters
$url = 'http://api.indeed.com/ads/apisearch'.'?';
$publisher = 'xxxxxxxxxxxxxxxx';
$q = $query;
$location = '';
if (isset($_POST['location'])) {
$location = $_POST['location'];
} else {
$geo = geoCheckIP($_SERVER['REMOTE_ADDR']);
if (isset($geo) && ($geo != "not found, not found")) {
$location = $geo;
}
}
$sort = 'date';
$radius = '20';
$st = '';
$jt = '';
$start = '0';
$limit = '25';
$fromage = '';
$highlight = '0';
$filter = '1';
$latlong = '0';
$co = 'us';
$chnl = '';
$userip = $_SERVER['REMOTE_ADDR'];
$useragent = isset($_SERVER['HTTP_USER_AGENT']) ? ($_SERVER['HTTP_USER_AGENT']) : 'unknown';
$v = '2';
$xml = simplexml_load_file($url."publisher=".$publisher."&q=".$q."&l=".$location."&sort=".$sort."&radius=".$radius."&st=".$st."&jt=".$jt."&start=".$start."&limit=".$limit."&fromage=".$fromage."&highlight=".$highlight."&filter=".$filter."&latlong=".$latlong."&co=".$co."&chnl=".$chnl."&userip=".$userip."&useragent=".$useragent."&v=".$v);
HTML BODY
<div class="paradiv">
<h1><?php echo $xml->totalresults . " " . $jobroll_title . " Near " . $location ?></h1>
<!-- BEGIN INDEED ORDERED LIST-->
<ol class="jobs">
<?php
foreach($xml->results->result as $result) { ?>
<li class="job <?php echo (++$liBgColor%2 ? 'odd' : 'even'); ?>">
<div class="title_wrapper">
<div id="jobtitle"><strong><a onmousedown="<?php echo $result->onmousedown;?>" rel="nofollow" href="<?php echo $result->url;?>" target="_blank"><?php echo $result->jobtitle;?></a></strong></div>
<div id="company"><?php echo $result->company;?></div>
</div>
<div id="snippet">
<?php $result->snippet = str_replace(" ", ". ", $result->snippet); echo $result->snippet;?>
</div>
<div id="location"><strong>Location:</strong> <?php echo $result->formattedLocationFull;?></div>
<div id="date"><span class="posted <?php echo (++$locationBgColor%2 ? 'even' : 'odd'); ?>">Posted <?php echo $result->formattedRelativeTime;?></span></div>
<div id="details-2"><strong><a onmousedown="<?php echo $result->onmousedown;?>" rel="nofollow" href="<?php echo $result->url;?>" target="_blank">Details</a></strong></div>
</li>
<?php } ?>
</ol>
<!-- END INDEED ORDERED LIST -->
<!-- THIS IS WHERE THE PAGINATION WILL DISPLAY -->
<div class="pagenumber"><?php echo "Page Number " . "" . $xml->pageNumber . "" ?></div>
</div>
This is how it works. A user arrives on the web page, then the page loads with the job results based on the users location. If less than 25 results are found for their zip code then there is no problem and pagination isn't needed.
But if the xml feed has more than 25 results, it will show 25 and that's it. If I want to display the rest, I have to paginate. This is what I need help with.
Here is how their API url works.
http://api.indeed.com/ads/apisearch?publisher=xxxxxxxxxxxxxxx&q=java&l=austin%2C+tx&sort=&radius=&st=&jt=&start=0&limit=25&fromage=&filter=&latlong=1&co=us&chnl=&userip=1.2.3.4&useragent=Mozilla/%2F4.0%28Firefox%29&v=2
The part that says &start=0&limit=25 is how to display the results based on page number of the xml.
So for example: &start=0&limit=25 would be page 0 showing 25 results, &start=25&limit=25 would be page 1 showing the next 25 results and &start=50&limit=25 would be page 2 showing the remaining 25 results. This example is based on if there are a total of 75 results in the xml feed.
And in my // Indeed.com API URL parameters above I have it set to start on page 0 and limit to 25. They do not allow beyond 25 in limit. If set higher it will default to 25.
$start = '0';
$limit = '25';
I need some help on implementing a way to paginate using my current PHP code above. How can I add on to what I have in my PHP code?
has_more function returns true if there are 25 results in the xml
$start = 0;
do {
$xml = simplexml_load_file(...$start...);
// process $xml
$start += 25;
} while(has_more($xml));

close div tag after 4 entries - mysql loop

i want my script to add a closing div tag after every fourth entrie of the db. i tried something like this:
<div class="row">
$ergebnis = $mysqli->query("SELECT name FROM pages Where city = '1';");
while($zeile = $ergebnis->fetch_array()) {
echo "<div class=\"col-sm-4 col-md-3\">
echo "<h3>".$zeile['name']."</h3>";
..
echo "</div>";
$i=0;
i++;
if ($i == 4){
echo "</div>";}
}
?>
would be great if you can help me here. thx
1) Get names of all cities
2) Initiate an indexing variable
3) Ieterate through the loop
4) Open <div class="row"> when $i==0 (Very First time) OR $i%4==0 (When fifth entry is to be printed). (Remember you are using 0 based indexing i.e. initializing $i=0).
5) Close the div tag for <div class="row"> when fourth city name has been printed i.e. $i%3==0 (Remember you are using 0 based indexing i.e. initializing $i=0).
Here's the code:
<?php
$ergebnis = $mysqli->query("SELECT name FROM pages Where city = '1';");
$i=0;
while($zeile = $ergebnis->fetch_array()) {
if ($i == 0 || $i%4==0){ // <div class="row"> opens on first entry and every fifth entry
echo "<div class=\"row\">";
}
echo "<div class=\"col-sm-4 col-md-3\">";
echo "<h3>".$zeile['name']."</h3>";
/*
Rest of your code
*/
echo "</div>";
$i++;
if ($i % 3 == 0){
echo "</div>"; // <div class="row"> closes here on every fourth entry
}
}
?>
Try this code,
<div class="row">
$ergebnis = $mysqli->query("SELECT name FROM pages Where city = '1';");
$i=0;
while($zeile = $ergebnis->fetch_array()) {
echo "<div class=\"col-sm-4 col-md-3\">
echo "<h3>".$zeile['name']."</h3>";
..
echo "</div>";
$i++;
if ($i == 4){
echo "</div>";
$i = 0;
}
}
?>

PHP Search: Using Jquery to alter a php a value

I have a comics website. A feature it has is to allow users to search comics... the search will instantly parse the input and return thumbnail results based on matching title and keywords.
Originally, the search would return all of the results, and the bounding search box would expand infinitely downward, holding every single comic result. I thought it may be a nice touch to limit the results to 4, and display a message like "load 5 remaining images" if the user chooses to do so.
If they click on that message, I wanted limiting php variable to be removed or changed.
So far, it loads with the limit, and shows a link...
EDIT: Latest Code:
search_field.php (the search file that get's included on a page... this file calls search.php via JQuery):
<?php $site = (isset($_GET['site']) ? ($_GET['site']) : null); ?>
<div id="sidebar" class="searchborder">
<!--Allow users to search for comic-->
<!--<span class="search">Search for <?php// echo (($site == "artwork") ? 'artwork' : 'a comic'); ?> </span>-->
<script type="text/javascript">
function GetSearch(mySearchString){
$.get("./scripts/search.php", {_input : mySearchString, _site : '<?php echo $site ?>'},
function(returned_data) {
$("#output").html(returned_data);
}
);
}
</script>
<center>
<table>
<tr>
<td>
<span class="search">
<img src="./images/SiteDesign/Search.png" />
<input type="text" onkeyup="GetSearch(this.value)" name="input" value="" />
<!--<input id="site" type="hidden" value="<?php// echo $site; ?>">-->
</span>
</td>
</tr>
</table>
</center>
<span id="output"> </span>
</div>
search.php, the file that's called to parse the string and return the results:
<?php
//Query all images:
include 'dbconnect.php';
$site = $_GET['_site'];
$input = (isset($_GET['_input']) ? ($_GET['_input']) : 0);
$siteChoice = (isset($_GET['_choice']) ? ($_GET['_choice']) : $site);
$start = (isset($_GET['_start']) ? ($_GET['_start']) : null);
echo "start: " . $start;
//if user goes to hittingtreeswithsticks.com... no "site" value will be set... so I need to set one
if ($site == null) {
$site = "comics";
}
if ($siteChoice == "artwork") {
$sql = "SELECT id, title, keywords, thumb FROM artwork";
$thumbpath = "./images/Artwork/ArtThumbnails/";
}
else if ($siteChoice == "comics") {
$sql = "SELECT id, title, keywords, thumb FROM comics";
$thumbpath = "./images/Comics/ComicThumbnails/";
}
else {
$sql = "SELECT id, title, keywords, thumb FROM $site";
if ($site == "artwork") {
$thumbpath = "./images/Artwork/ArtThumbnails/";
}
else {
$thumbpath = "./images/Comics/ComicThumbnails/";
}
}
/* For this to work, need all comics replicated in an "All Comics" file along with "All Thumbnails"
else {
$sql = "SELECT id, title, thumb FROM comics
UNION
SELECT id, title, thumb FROM artwork";
$thumbpath = "./images/AllThumbnails/";
}*/
$imgpaths = $mysqli->query($sql);
mysqli_close($mysqli);
$idresult = array();
$imgresult = array();
$thumbresult = array();
//CHECK IF $INPUT == IMAGE PATH
if (strlen($input) > 0)
{
while ($row = $imgpaths->fetch_assoc())
{
//query against key words, not the image title (no one will remember the title)
if (stripos($row['keywords'], $input) !== false || strtolower($input)==strtolower(substr($row['title'],0,strlen($input))))
//if (strtolower($input)==strtolower(substr($row['title'],0,strlen($input))))
{
array_push($idresult, $row['id']);
array_push($imgresult, $row['title']);
array_push($thumbresult, $row['thumb']);
}
}
//ECHO RESULTS ARRAY
if(count($imgresult) == 0)
{
echo "<p>no suggestions</p>";
}
else
{
echo "<ul>";
$k = 0;
$max = 4;
if (count($imgresult) > $max) {
while ($k < count($imgresult) && $k < $max)
{
echo '<li>
<span class="sidebarimages"><a href=".?action=viewimage&site=' . $siteChoice . '&id=' . $idresult[$k] . '">
<img src="./scripts/thumber.php?img=.'.$thumbpath.$thumbresult[$k].'&mw=90&mh=90"/></a></span>
</li>';
$k++;
}
$difference = count($imgresult)-$k;
echo "<br/><i><a href='.?action=homepage&site=" . $siteChoice . "&start=4' class='loadSearch'>load " . $difference . " more result" . (($difference != 1) ? 's' : '') . "... </a></i>";
}
else {
while ($k < count($imgresult))
{
echo '<li>
<span class="sidebarimages"><a href=".?action=viewimage&site=' . $siteChoice . '&id=' . $idresult[$k] . '">
<img src="./scripts/thumber.php?img=.'.$thumbpath.$thumbresult[$k].'&mw=90&mh=90"/></a></span>
</li>';
$k++;
}
}
echo "</ul>";
}
}
?>
<script type="text/javascript">
$(".loadSearch").click(function() {
//alert("Test");
$.get("./search.php", {_start : 4},
function (returned_data) {
$("#moreResults").html(returned_data);
}
);
});
</script>
Try this:
<script type="text/javascript">
$("#loadSearch").click(function() {
$.get('URL WITH QUERY', function(data) {
$('#results').html(data);
});
});
</script>
From what i get all you need is when "load more" is clicked only new results should be shown.
Load more has to be a url same as your search url.
Search/Autocomplete URL - example.com/autocomplete?q=xkd
Load More URL - example.com/autocomplete?q=xkd&start=4&max=1000
Just add two parameters to your url. start and max. Pass them to your queries and you get exact result.
Only verify Start < Max and are integers intval() and not 0 empty(). Also if Max <= 4 then dont show load more.
I would give all of your results back, then try to determine your results. If more then 4, loop out the first 4 results. If the user clicks on the load more button your start looping from your 4th element. That way you only need to hit the server once (per search).
Try to give back your results in json, so you can format it the way you like in your html file.
In pseudo code:
searchTerm = 'hello';
resultsFromServer = getResults($searchterm);
resultcounter = count(resultsFromServer);
if(resultcounter > 4)
loop 4 results
else
loop all results
$(".loadSearch").click(function(e) {
//alert("Test");
e.preventDefault();
$.get("./search.php", {_start : 4},
function (returned_data) {
$("#moreResults").html(returned_data);
}
);
I ended up going with jquery show and hide functions.
PHP Snippet:
//ECHO RESULTS ARRAY
if(count($imgresult) == 0)
{
echo "<p>no suggestions</p>";
}
else
{
echo "<ul>";
$k = 0;
$max = 4;
while ($k < count($imgresult) && $k < $max)
{
echo '<li>
<span class="sidebarimages"><a href=".?action=viewimage&site=' . $siteChoice . '&id=' . $idresult[$k] . '">
<img src="./scripts/thumber.php?img=.'.$thumbpath.$thumbresult[$k].'&mw=90&mh=90"/></a></span>
</li>';
$k++;
}
$difference = count($imgresult)-$k;
echo '<div id="moreResults">';
while ($k < count($imgresult))
{
echo '<li>
<span class="sidebarimages"><a href=".?action=viewimage&site=' . $siteChoice . '&id=' . $idresult[$k] . '">
<img src="./scripts/thumber.php?img=.'.$thumbpath.$thumbresult[$k].'&mw=90&mh=90"/></a></span>
</li>';
$k++;
}
echo '</div>';
if (count($imgresult) > $max) {
?>
<br />Load <?php echo $difference; ?> more result<?php echo (($difference != 1) ? 's' : ''); ?>...
<?php
}
echo "</ul>";
}
}
Jquery:
<script type="text/javascript">
$("#moreResults").hide();
$("#showMore").click(function() {
$("#moreResults").show();
$("#showMore").hide();
});

Categories