Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

one array from two data sources 1

Status
Not open for further replies.

tyutghf

Technical User
Apr 12, 2008
258
GB
Hi

I have two pieces of data coming from two different sources, one an rss feed and another from the database.

At present I display data from the database first, then data from the rss feed.

However they now want to display one of each alternating i.e.

database data row
rss feed data row
database data row
rss feed data row

How on earth do I do that? I suppose I create a big array of them all then split somehow?

Thanks
 
Hi

You use a single loop to iterate over the longer dataset and each time fetch/read/load/whatever one record/line/entry/whatever from each input source.


Feherke.
feherke.ga
 
Thanks for the reply Fehérke, how do I iterate through one to feed in to the other? Would feeding the rss in to the database feed or vice versa be the best bet? Here is how I am displaying each news feed at the moment, it's a wordpress website as you'll see below.

Ideally I want to grab three of each to make a list of 6 total items.

Code:
Database:
<?php
$the_query = new WP_Query( array ( 'category_name' => 'News', 'posts_per_page' => 3 ) );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

RSS:

<?php
$feed = false;
if (get_field('news_rss')) { //advanced custom field text field where the rss feed is passed
				
$cacheId = 'news_rss' . get_the_id();
$feed = wp_cache_get( $cacheId );
if ( ! $feed ) {
$feed = fetch_feed( get_field('news_rss') );
wp_cache_set( $cacheId, $feed );
}
						
if (empty($feed->data['child']['']['rss'][0]['child']['']['channel'][0]['child']['']['item'])) {
$feed = false;
}
}
					
foreach ($feed->data['child']['']['rss'][0]['child']['']['channel'][0]['child']['']['item'] as $news) {
						
?>
<li>
<?php echo $news['child']['']['description'][0]['data']; ?>
</li>
<?php }	?>
 
Hi

This is how I would do it generally ( no idea about WordPress, it may have something suitable built in ) :
PHP:
[teal]<?php[/teal]

[navy]$the_query[/navy] [teal]=[/teal] [b]new[/b] [COLOR=orange]WP_Query[/color][teal]([/teal][b]array[/b][teal]([/teal][i][green]'category_name'[/green][/i] [teal]=>[/teal] [i][green]'News'[/green][/i][teal],[/teal] [i][green]'posts_per_page'[/green][/i] [teal]=>[/teal] [purple]3[/purple][teal]));[/teal]

[navy]$feed[/navy] [teal]=[/teal] [b]false[/b][teal];[/teal]
[b]if[/b] [teal]([/teal][COLOR=orange]get_field[/color][teal]([/teal][i][green]'news_rss'[/green][/i][teal])) {[/teal] [gray]//advanced custom field text field where the rss feed is passed[/gray]

	[navy]$cacheId[/navy] [teal]=[/teal] [i][green]'news_rss'[/green][/i] [teal].[/teal] [COLOR=orange]get_the_id[/color][teal]();[/teal]
	[navy]$feed[/navy] [teal]=[/teal] [COLOR=orange]wp_cache_get[/color][teal]([/teal][navy]$cacheId[/navy][teal]);[/teal]
	[b]if[/b] [teal](![/teal] [navy]$feed[/navy][teal]) {[/teal]
		[navy]$feed[/navy] [teal]=[/teal] [COLOR=orange]fetch_feed[/color][teal]([/teal][COLOR=orange]get_field[/color][teal]([/teal][i][green]'news_rss'[/green][/i][teal]));[/teal]
		[COLOR=orange]wp_cache_set[/color][teal]([/teal][navy]$cacheId[/navy][teal],[/teal] [navy]$feed[/navy][teal]);[/teal]
	[teal]}[/teal]

	[b]if[/b] [teal]([/teal][b]empty[/b][teal]([/teal][navy]$feed[/navy][teal]->[/teal]data[teal][[/teal][i][green]'child'[/green][/i][teal]][[/teal][i][green]''[/green][/i][teal]][[/teal][i][green]'rss'[/green][/i][teal]][[/teal][purple]0[/purple][teal]][[/teal][i][green]'child'[/green][/i][teal]][[/teal][i][green]''[/green][/i][teal]][[/teal][i][green]'channel'[/green][/i][teal]][[/teal][purple]0[/purple][teal]][[/teal][i][green]'child'[/green][/i][teal]][[/teal][i][green]''[/green][/i][teal]][[/teal][i][green]'item'[/green][/i][teal]])) {[/teal]
		[navy]$feed[/navy] [teal]=[/teal] [b]false[/b][teal];[/teal]
	[teal]}[/teal]
[teal]}[/teal]

[b]if[/b] [teal]([/teal][navy]$the_query[/navy][teal]->[/teal][COLOR=orange]have_posts[/color][teal]() ||[/teal] [navy]$feed[/navy][teal]) {[/teal]
	[navy]$feed_item_count[/navy] [teal]=[/teal] [COLOR=orange]count[/color][teal]([/teal][navy]$feed[/navy][teal]->[/teal]data[teal][[/teal][i][green]'child'[/green][/i][teal]][[/teal][i][green]''[/green][/i][teal]][[/teal][i][green]'rss'[/green][/i][teal]][[/teal][purple]0[/purple][teal]][[/teal][i][green]'child'[/green][/i][teal]][[/teal][i][green]''[/green][/i][teal]][[/teal][i][green]'channel'[/green][/i][teal]][[/teal][purple]0[/purple][teal]][[/teal][i][green]'child'[/green][/i][teal]][[/teal][i][green]''[/green][/i][teal]][[/teal][i][green]'item'[/green][/i][teal]]);[/teal]
	[navy]$feed_item_nr[/navy] [teal]=[/teal] [purple]0[/purple][teal];[/teal]

	[b]echo[/b] [i][green]'<ul>'[/green][/i][teal];[/teal]
	[b]while[/b] [teal]([/teal][navy]$the_query[/navy][teal]->[/teal][COLOR=orange]have_posts[/color][teal]() ||[/teal] [navy]$feed_item_nr[/navy] [teal]<[/teal] [navy]$feed_item_count[/navy][teal]) {[/teal]
		[b]if[/b] [teal]([/teal][navy]$the_query[/navy][teal]->[/teal][COLOR=orange]have_posts[/color][teal]()) {[/teal]
			[navy]$the_query[/navy][teal]->[/teal][COLOR=orange]the_post[/color][teal]();[/teal]
			[b]echo[/b] [i][green]'<li>'[/green][/i] [teal].[/teal] [COLOR=orange]get_the_title[/color][teal]() .[/teal] [i][green]'</li>'[/green][/i][teal];[/teal]
		[teal]}[/teal]

		[b]if[/b] [teal]([/teal][navy]$feed_item_nr[/navy] [teal]<[/teal] [navy]$feed_item_count[/navy][teal]) {[/teal]
			[b]echo[/b] [i][green]'<li>'[/green][/i][teal],[/teal] [navy]$feed[/navy][teal]->[/teal]data[teal][[/teal][i][green]'child'[/green][/i][teal]][[/teal][i][green]''[/green][/i][teal]][[/teal][i][green]'rss'[/green][/i][teal]][[/teal][purple]0[/purple][teal]][[/teal][i][green]'child'[/green][/i][teal]][[/teal][i][green]''[/green][/i][teal]][[/teal][i][green]'channel'[/green][/i][teal]][[/teal][purple]0[/purple][teal]][[/teal][i][green]'child'[/green][/i][teal]][[/teal][i][green]''[/green][/i][teal]][[/teal][i][green]'item'[/green][/i][teal]][[/teal][navy]$feed_item_nr[/navy][teal]][[/teal][i][green]'child'[/green][/i][teal]][[/teal][i][green]''[/green][/i][teal]][[/teal][i][green]'description'[/green][/i][teal]][[/teal][purple]0[/purple][teal]][[/teal][i][green]'data'[/green][/i][teal]],[/teal] [i][green]'</li>'[/green][/i][teal];[/teal]
			[navy]$feed_item_nr[/navy][teal]++;[/teal]
		[teal]}[/teal]
	[teal]}[/teal]
	[b]echo[/b] [i][green]'</ul>'[/green][/i][teal];[/teal]
[teal]}[/teal] [b]else[/b] [teal]{[/teal]
[gray]// no posts nor feed found[/gray]
[teal]}[/teal]
[gray]/* Restore original Post Data */[/gray]
[COLOR=orange]wp_reset_postdata[/color][teal]();[/teal]

[teal]?>[/teal]
[maroon][small]Warning : Of course, not tested.[/small][/maroon]

This displays all data : first alternating then the remaining of the longer dataset. Changing it to display the first 3 only should not be too hard, if you know what you want to happen if one of the datasets has less data.


Feherke.
feherke.ga
 
Thank you, this put me in the right direction and it is up and running now :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top