×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

one array from two data sources

one array from two data sources

one array from two data sources

(OP)
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

RE: one array from two data sources

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

RE: one array from two data sources

(OP)
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 }	?> 

RE: one array from two data sources

Hi

This is how I would do it generally ( no idea about WordPress, it may have something suitable built in ) :

CODE --> PHP

<?php

$the_query = new WP_Query(array('category_name' => 'News', 'posts_per_page' => 3));

$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;
	}
}

if ($the_query->have_posts() || $feed) {
	$feed_item_count = count($feed->data['child']['']['rss'][0]['child']['']['channel'][0]['child']['']['item']);
	$feed_item_nr = 0;

	echo '<ul>';
	while ($the_query->have_posts() || $feed_item_nr < $feed_item_count) {
		if ($the_query->have_posts()) {
			$the_query->the_post();
			echo '<li>' . get_the_title() . '</li>';
		}

		if ($feed_item_nr < $feed_item_count) {
			echo '<li>', $feed->data['child']['']['rss'][0]['child']['']['channel'][0]['child']['']['item'][$feed_item_nr]['child']['']['description'][0]['data'], '</li>';
			$feed_item_nr++;
		}
	}
	echo '</ul>';
} else {
// no posts nor feed found
}
/* Restore original Post Data */
wp_reset_postdata();

?> 
Warning : Of course, not tested.

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

RE: one array from two data sources

(OP)
Thank you, this put me in the right direction and it is up and running now smile

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close