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++;
}
?>
Related
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;}
}
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.
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));
I have the code below that is selecting a random set of questions from Wordpress.
<?php
$rows = get_field('step_by_step_test');
$row_count = count($rows);
$rand_rows = array();
$questions = get_field('select_number_of_questions');
for ($i = 0; $i < min($row_count, $questions); $i++) {
$r = rand(0, $row_count - 1);
while (array_search($r, $rand_rows) !== false) {
$r = rand(0, $row_count - 1);
}
$rand_rows[] = $r;
echo $rows[$r]['question'];
}
?>
I want to incorporate a bit of extra code (below), how can I make sure it's selecting the same random question?
<?php if(get_sub_field('answer_options')): ?>
<?php while(has_sub_field('answer_options')): ?>
<?php echo the_sub_field('answer'); ?>
<?php endwhile; ?>
<?php endif; ?>
Why dont you change your approach slightly?
<?php
$rows = get_field('step_by_step_test'); // Get the test
$question_count = get_field('select_number_of_questions'); // Get the number of questions
$rows = shuffle($rows); // Randomize your questions
$rows = array_slice($rows, $question_count); // Now set the array to only contain the number of questions you wanted
foreach ($rows as $row) {
echo $row['question']; // Show the question
if(get_sub_field('answer_options', $row['id'])) {
while(has_sub_field('answer_options', $row['id'])) {
echo the_sub_field('answer');
}
}
}
?>
I made the assumption that you can alter "get_sub_field" to include the ID of the question, so you can then include the ID in your "where" field of "answer_options". This will allow you to link the question.
I think that what you need is to set up the whole thing in a loop. query by custom field
Or you could store the ids of the questions you got above, an then, below, query for the answers for those specific posts.
Here's how I randomized my WordPress slider using the Advanced Custom Fields plugin + Royal Slider with a modified version of TheSwiftExchange's code above
<div id="full-width-slider" class="royalSlider heroSlider rsMinW">
<?php
/*
* Slider Repeater field shuffled
* http://stackoverflow.com/questions/12563116/incorporating-extra-loop-into-random-selection
*/
$rows = get_field('slider');
// For Debugging:
// echo "<pre>";
// var_dump($rows);
// echo "</pre>";
$quotes = get_field('slide_text'); // Get the number of images
shuffle($rows); // Randomize your slides
foreach ($rows as $row) {
$slideImageID = $row['slide_image'];
$slideImage = wp_get_attachment_image_src( $slideImageID, 'full' );
$slideText = $row['slide_text'];
?>
<div class="rsContent">
<div class="royalCaption">
<div class="royalCaptionInner">
<div class="infoBlock">
<?php if(!empty($slideText)) {
echo $slideText;
}; ?>
</div>
</div>
</div>
<img src="<?php echo $slideImage[0]; ?>" class="" />
</div><!-- /.rsContent -->
<?php } // end foreach ?>
</div><!-- /slider-wrap -->
I have a problem that concerns blog posts and displaying the tag words from another table.
I seem to be able to pull the info out of the tables fine, however when I try to display the posts and the tags, I get one tag per post. In other words if I have 7 tags for a post, I get 7 iteration's of that post each with one tag instead of 1 post with 7 tags.
My Controller ( do have a question about the $this->db->get(posts, tags) is that correct
$this->db->order_by('posts.id', 'DESC');
$where = "publish";
$this->db->where('status', $where);
$this->db->join('tags', 'tags.post_id = posts.id');
$this->db->limit('7');
$query = $this->db->get('posts', 'tags');
if($query->result())
$data = array();
{
$data['blog'] = $query->result();
}
$data['title'] = 'LemonRose';
$data['content'] = 'home/home_content';
$this->load->view('template1', $data);
The view.
$limit = 5; // how many posts should we show in full?
$i = 1; // count
foreach ($blog as $row):
$permalink = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING'];
$url = CompressURL ("$permalink");
$description = $row->title . $row->post;
$twittermsg = substr($description, 0, 110);
$twittermsg .= "...more " . $url;
if ($i < $limit) // we are under our limit
{ ?>
<div class="titlebox">
<div class="title"><? echo ucwords($row->title); ?></div>
<span><? echo $row->date, nbs(10), $row->author; ?></span>
</div>
<div class="clear"></div>
<? $str = str_word_count($row->post, 0);
if ($str >= 500) {
$row->post = html_entity_decode($row->post);
$row->post = $this->typography->auto_typography($row->post); // display?>
<div class="split"> <? echo $row->post = word_limiter($row->post, 480); ?>
<div class="tags"><? echo $row->tag; ?></div>*** These 3 lines seem to be where I am confused and getting the wrong display
<p><h3>More <?php echo anchor("main/blog_view/$row->id", ucwords($row->title)); ?> </h3></p>
<p>Trackback URL: <? echo base_url() . "trackbacks/track/$row->id"; ?></p>
<!-- tweet me -->
<?echo anchor("http://twitter.com/home?status=$twittermsg", 'Tweet'); ?>
This is my first attempt with join and I have very little experience getting the display with implode, if that is the right way to go.
Thank you in advance.
Try
<div class="tags"><? echo implode(', ', $row->tag); ?></div>
and remove the 2 rows before this one.