Displaying Blogs according to publish time - php

I have a doubt am displaying posts of blogs[more than 1] and now i want to display blogs according to publish date mean new post 1st next 2nd and so on...
MY CODE
require_once('rss_fetch.inc');
$dateArray= "";
$urls = array(
'http://rajs-creativeguys.blogspot.com/feeds/posts/default?alt=rss',
'http://raghuks.wordpress.com/feed'
);
foreach($urls as $url) {
/*'http://raghuks.wordpress.com/feed/'*/;
$rss = fetch_rss($url);
foreach ($rss->items as $i => $item ) {
$title = strtoupper ($item['title']);
$url = $item['link'];
$date = substr($item['pubdate'],0,26);
$dateArray=array();
//code to fetch only some text
$desc = '';
$max = 30;
$arr = explode(' ', strip_tags($item['description']));
$l = count($arr);
if($l < $max) $max = $l;
for($j=0;$j<$max;++$j)
{
$desc .= $arr[$j] . ' ';
}
$desc .= '.....';
echo "<div class=\"blog\"><a target=\"_blank\" href=$url><h1>$title</h1>$desc<br/><br/>DATED : $date <br/><br/></a></div> ";
if($i == 1) break;
}
}
Only recent 4 posts should display from any blog but that should be according to date
Please help..
What i tried is putting all date into an array and using bubble sort but its not working.. Please Help Me..
Thanks In Advance

require_once('rss_fetch.inc');
$dateArray= "";
$urls = array(
'http://rajs-creativeguys.blogspot.com/feeds/posts/default?alt=rss',
'http://raghuks.wordpress.com/feed'
);
$result_array = array();
foreach($urls as $url) {
/*'http://raghuks.wordpress.com/feed/'*/;
$rss = fetch_rss($url);
foreach ($rss->items as $i => $item ) {
$title = strtoupper ($item['title']);
$url = $item['link'];
$date = substr($item['pubdate'],0,26);
$dateArray=array();
//code to fetch only some text
$desc = '';
$max = 30;
$arr = explode(' ', strip_tags($item['description']));
$l = count($arr);
if($l < $max) $max = $l;
for($j=0;$j<$max;++$j)
{
$desc .= $arr[$j] . ' ';
}
$desc .= '.....';
$tm = strtotime($date);
$result_array[$tm]['title'] = $title;
$result_array[$tm]['url'] = $url;
$result_array[$tm]['desc'] = $desc;
$result_array[$tm]['date'] = $date;
if($i == 1) break;
}
ksort($result_array);
foreach($result_array as $result)
{
echo "<div class=\"blog\"><a target=\"_blank\" href=$result['url']><h1>$result['title']</h1>$result['desc']<br/><br/>DATED : $result['date'] <br/><br/></a></div> ";
}
}

Related

create div from each item in tags column

My tags column is like this:
first row: sky - earth - sea
second row: iron - silver - gold
third row: apple - fruit - food
...and so on
Want to create a div from each item, like this:
<div class='tagdown'>sky</div>
<div class='tagdown'>earth</div>
$st = $db->query("select tags from posts");
$arr = array();
$items = "";
while ($row = $st->fetch()) {
array_push($arr, explode(' - ', $row['tags']));
}
foreach($arr as $item) {
$items .= "<div class='tagdown'>" . $item . "</div>\n";
}
echo $items;
Notice: Array to string conversion...
Another Try:
for ($i = 0; $i < count($arr); ++$i) {
$items .= "<div class='tagdown'>" . $arr[$i] . "</div>\n";
}
echo $items;
Notice: Array to string conversion...
Any help?
Dont push and again traverse your array. just print out data in while loop. Try following code:
$items = "";
while ($row = $st->fetch()) {
$arr = explode(' - ', $row['tags']);
$items .= "<div class='tagdown'>".implode("</div>\n<div class='tagdown'>",$arr)."</div>\n";
}
echo $items;
Try like shown below
Example :
<?php
$items = "";
$str = "sky-earth-sea";
$arr = explode("-", $str);
$count = count($arr);
for($i = 0; $i < $count; $i++) {
$items .= "<div class='tagdown'>".$arr[$i]."</div></br>";
}
echo $items;
?>
explode() returns an array and you are pushing an array into an other array
its making 1 2D array you can check thar using print_r($arr);
use this
while ($row = $st->fetch()) {
$tag=explode('-', $row['tags'];
foreach($tag as $t){
array_push($arr,$t ));
}
}
You can also use fetch associative if using mysqli_connect
while ($row = $result->fetch_assoc()) {
array_push($arr, explode(' - ', $row['tags']));
}
foreach($arr as $a) {
foreach($a as $v){
$items .= "<div class='tagdown'>" . $v . "</div>\n";
}
}
echo $items;
-------------- OR -------------
$arr = array();
$items = "";
while ($row = $result->fetch_assoc()) {
$tag = explode(' - ', $row['tags']);
foreach($tag as $v){
$items .= "<div class='tagdown'>" . $v . "</div>\n";
}
}
echo $items;

what are the different ways for this code to work

The following is part of a personal budgeting program I'm writing.
This code pulls line item information from multiple tables and writes it into an array and then displays the information by transid => family => category => lineItems. Everything works, and I get the results I want out of it. My question is if there is a more efficient way to accomplish this task?
Since this is a personal program, I'm only asking so that I can improve my coding abilities.
<?php
include ('../cfg/connect.php');
$s = " : ";
$br = "<br>";
$ul = "<ul>";
$li = "<li>";
$_ul = "</ul>";
$_li = "</li>";
$data = [];
$itemCount = 0;
$arrayItemCount = 0;
$categoryQry = "SELECT a.itemQty, b.transDate, b.transID, b.amount, a.itemPrice, a.itemCategory, c.catFamily, a.itemName, a.itemSource FROM budget.lineItems AS a JOIN budget.quickEntry AS b ON a.transID = b.transID JOIN budget.categories AS c ON a.itemCategory = c.catName WHERE b.processed = 'y' ORDER BY c.catFamily, c.catName, b.transDate";
$categories = $conn->prepare ($categoryQry);
$categories->execute ();
$categories->store_result ();
$categories->bind_result ($itemQty, $transDate, $transID, $totalPrice, $itemPrice, $category, $family, $itemName, $source);
while ($categories->fetch ()) {
if (!isset($data[$transID]['amount'])) {
$data[$transID]['amount'] = 0;
}
if (!isset($data[$transID]['line'])) {
$data[$transID]['line'] = '';
}
if (!isset($data[$transID]['line'][$family]['amount'])) {
$data[$transID]['line'][$family]['amount'] = 0;
}
if (!isset($data[$transID]['line'][$family]['line'])) {
$data[$transID]['line'][$family]['line'] = '';
}
if (!isset($data[$transID]['line'][$family]['line'][$category]['amount'])) {
$data[$transID]['line'][$family]['line'][$category]['amount'] = 0;
}
if (!isset($data[$transID]['line'][$family]['line'][$category]['line'])) {
$data[$transID]['line'][$family]['line'][$category]['line'] = '';
}
$itemCount++;
$qtyPrice = $itemPrice * $itemQty;
$data[$transID]['amount'] += $qtyPrice;
$data[$transID]['transDate'] = $transDate;
$data[$transID]['source'] = $source;
$data[$transID]['line'][$family]['amount'] += $qtyPrice;
$data[$transID]['line'][$family]['line'][$category]['amount'] += $qtyPrice;
$data[$transID]['line'][$family]['line'][$category]['line'][$itemName] = ['itemQty' => $itemQty, 'itemPrice' => $itemPrice];
}
foreach ($data as $transID => $transValue) {
echo $transID .$s.$transValue['transDate'].$s.$transValue['source'].$s.$transValue['amount']. $ul;
foreach ($transValue['line'] as $category => $categoryValue) {
echo $li . $category .$s.$categoryValue['amount']. $ul;
foreach ($categoryValue['line'] as $line => $lineValue) {
echo $li . $line .$s.$lineValue['amount']. $ul;
foreach ($lineValue['line'] as $item => $details) {
echo $li . $item .$s . $details['itemQty'] . $s . $details['itemPrice'] . $_li;
}
echo $_ul . $_li;
}
echo $_ul . $_li;
}
echo $_ul . $br;
}

Why isn't this returning all of the results in this while loop?

I'm trying to write a video ripper and my while loop is only returning 1 result.
<?php
$url = 'http://www.SITE.com/categories/redhead';
$url2 = 'http://www.SITE.com/movies';
$search = file_get_contents($url);
$results = explode('"/movies', $search);
$count = count($results);
$i = 1;
while($i < 5) {
$final = $url2 .$results[$i];
$goodfinal = str_replace('">', ' ', $final);
echo $goodfinal.'<br>';
$i++;
}
?>
Perhaps use your iterator to access the correct $results entry:
$url = 'http://www.SITE.com/categories/redhead';
$url2 = 'http://www.SITE.com/movies';
$search = file_get_contents($url);
$results = explode('"/movies', $search);
$i = 0;
$length = count($results);
while($i < $length) {
$final = $url2 .$results[$i];
$goodfinal = str_replace('">', ' ', $final);
echo $goodfinal.'<br>';
$i++;
}
As an alternative you could loop over your results like this:
foreach($results as $result) {
echo str_replace('">', ' ', $url2 . $result) . '<br>';
}

About displaying posts from two or more blogs

I want to display posts of two or more blogs in my website now am using magpierss-0.72 for fetching the posts and my code is
require_once('rss_fetch.inc');
$url = 'http://rajs-creativeguys.blogspot.com/feeds/posts/default?alt=rss'
/*'http://raghuks.wordpress.com/feed/'*/;
$rss = fetch_rss($url);
foreach ($rss->items as $i => $item ) {
$title = strtoupper ($item['title']);
$url = $item['link'];
$date = substr($item['pubdate'],0,26);
//code to fetch only some text
$desc = '';
$max = 30;
$arr = explode(' ', strip_tags($item['description']));
$l = count($arr);
if($l < $max) $max = $l;
for($j=0;$j<$max;++$j) {
$desc .= $arr[$j] . ' ';
}
$desc .= '.....';
echo "<div class=\"blog\"><a target=\"_blank\" href=$url><h1>$title</h1>$desc<br/><br/>DATED : $date <br/><br/></a></div> ";
if($i == 3) break;
}
Here i can specify only one url of feeds and can fetch but now i want to display posts of two or more blogs Please give me the solution
Thanks in advance
Just use an array and throw in another foreach:
<?php
require_once('rss_fetch.inc');
$urls = array(
'http://rajs-creativeguys.blogspot.com/feeds/posts/default?alt=rss',
' more urls ... ',
);
foreach($urls as $url) {
/*'http://raghuks.wordpress.com/feed/'*/;
$rss = fetch_rss($url);
foreach ($rss->items as $i => $item ) {
$title = strtoupper ($item['title']);
$url = $item['link'];
$date = substr($item['pubdate'],0,26);
//code to fetch only some text
$desc = '';
$max = 30;
$arr = explode(' ', strip_tags($item['description']));
$l = count($arr);
if($l < $max) $max = $l;
for($j=0;$j<$max;++$j)
{
$desc .= $arr[$j] . ' ';
}
$desc .= '.....';
echo "<div class=\"blog\"><a target=\"_blank\" href=$url><h1>$title</h1>$desc<br/><br/>DATED : $date <br/><br/></a></div> ";
if($i == 3) break;
}
}

About Limiting Rss Feed

I want only the latest 5 feeds to be shown on my website.
I am using the following code to fetch rss feed... Can any one help to limited feeds to be shown... Thank You In ADVANCE :)
CODE THAT AM USING
<?php
require_once('rss_fetch.inc');
$url = 'http://news.google.com/news?ned=us&topic=h&output=rss';
$rss = fetch_rss($url);
echo "Site: ", $rss->channel['title'], "<br>\n";
foreach ($rss->items as $item ) {
$title = $item['title'];
$url = $item['link'];
$desc = $item['description'];
$category = $item['category'];
echo "<a href=$url>$title</a>$desc <br/>CATEGORY : $category <br/><br/> ";
}
?>
Limit it using foreach?
foreach ($rss->items as $i => $item ) { // use $i as counter
$title = $item['title'];
$url = $item['link'];
$desc = $item['description'];
$category = $item['category'];
echo "<a href=$url>$title</a>$desc <br/>CATEGORY : $category <br/><br/> ";
if($i == 4) break; // add this, == 4 is because $i starts from 0
}
If you're looking to limit the number of posts, you just need to keep track of them and break out of the foreach loop when applicable, e.g.
<?php
require_once('rss_fetch.inc');
$url = 'http://news.google.com/news?ned=us&topic=h&output=rss';
$rss = fetch_rss($url);
echo "Site: ", $rss->channel['title'], "<br>\n";
$numposts = 0;
$maxposts = 5;
foreach ($rss->items as $item ) {
$numposts++;
if ($numposts<=$maxposts) {
$title = $item['title'];
$url = $item['link'];
$desc = $item['description'];
$category = $item['category'];
echo "<a href=$url>$title</a>$desc <br/>CATEGORY : $category <br/><br/> ";
} else {
break;
}
}
?>

Categories