php generating rssfeed filename from field in a table? - php

SOLUTION
<?php
include("authenticate.php");
$user = $_SESSION['UserName'];
$initialdata = $result = mysql_query("SELECT * FROM ccregisterfeed WHERE username = '$user'");
while($row = mysql_fetch_assoc($initialdata)){
$filename = $row["feedlink"];
// var_dump ($filename);
}
$initdata = mysql_fetch_assoc($initialdata);
$result = mysql_query("SELECT * FROM ccshowcontent JOIN ccaudio ON ccshowcontent.id = ccaudio.id WHERE ccshowcontent.username = '$user' ORDER BY ccshowcontent.id DESC")
or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$items[] = $row;
}
$rss = createXML($items,$initdata);
//echo "feed updated!";
$filename = ($filename);
file_put_contents($filename,$rss);
header("Location: ccupload.php?message=".urlencode("Show saved and feed updated"));
?>
FULL SCRIPT
<?php
include("authenticate.php");
$user = $_SESSION['UserName'];
$initialdata = $result = mysql_query("SELECT * FROM ccregisterfeed WHERE username = '$user'");
$initdata = mysql_fetch_assoc($initialdata);
$result = mysql_query("SELECT * FROM ccshowcontent JOIN ccaudio ON ccshowcontent.id = ccaudio.id WHERE ccshowcontent.username = '$user' ORDER BY ccshowcontent.id DESC")
or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$items[] = $row;
}
function createXML($items,$data){
$xml = "<?xml version='1.0' encoding='UTF-8'?>
<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>
<channel>
<atom:link href='".$data['feedlink']."'
rel='self' type='application/rss+xml' />
<title>".$data['feedtitle']."</title>
<link>".$data['websitelink']."</link>
<category domain=''>".$data['category']."</category>
<copyright>".$data['copyright']."</copyright>
<pubDate>".date("D, d M Y H:i:s O", strtotime($data['pubdate']))."</pubDate>
<language>en-us</language>
<description>".$data['feeddescription']."</description>
<image>
<title>".$data['feedtitle']."</title>
<link>".$data['websitelink']."</link>
<url>".$data['imagelink']."</url>
<description>".$data['imagetitle']."</description>
</image>";
$audiodir = "http://thetradingcardgenerator.com/MP3/";
foreach($items as $key => $item){
$path = $audiodir.$item['path'];
$pdate = strtotime($item['pubdate']);
$date = date("D, d M Y H:i:s O", $pdate);
$xml .="
<item>
<title>".$item['title']."</title>
<link>".$path."</link>
<guid>".$path."</guid>
<pubDate>".$date."</pubDate>
<description><![CDATA[".$item['description']."]]></description>
</item>";
}
$xml .="
</channel>
</rss>";
return $xml;
}
$rss = createXML($items,$initdata);
//echo "feed updated!";
$filename = '$feedlink' . ".xml";
file_put_contents($filename,$rss);
header("Location: ccupload.php?message=".urlencode("Show saved and feed updated"));
?>
FIRST ENTRY
I've wrote a script to generate an rss feed from data entered in a table. I want to generate the filename from a field in the table, which I created as follows :
$path = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$feedtitle = str_replace(" ", "", $feedtitle);
$feedtitle = str_replace("_", "", $feedtitle);
$feedtitle = str_replace("-", "", $feedtitle);
$feedtitle = strtolower($feedtitle);
$path = substr($path, 0, strrpos($path, "/"));
$feedlink = "$feedtitle" . '.xml';
Thus if $feedtitle was Last Nights Television the value of $feedlink would be
lastnightstelevision.xml
I've tested the script by specifying the filename and it works, data is pulled from the tables and a feed is created. This is the last part of the script :
$rss = createXML($items,$initdata);
$filename = 'myfeed.xml';
file_put_contents($filename,$rss);
header("Location: uploadcontent.php?message=".urlencode("Show saved and feed updated"));
?>
Which creates a feed name myfeed.xml on my server.
However I want the filename of the feed to be the value of $feedlink. I know this is probably something basic but I can't figure out how to do it.
I tried :
$filename = ".$feedlink";
$filename = ".$feedlink[feedlink]";
And many other combinations.

If you're trying to get that filename from RSS, try this
$attribute = $rss->channel->children("atom", true)->link->attributes();
$filename = $attribute["href"];
Or this, if you want to get it from results returned from your first query
$filename = $initdata['feedlink'];
The result above is a whole path, so if you need only the filename from that path, use this
$feedlink = "http://mywbsite/mydir/lastnightstelevision.xml";
$filename = basename($feedlink);
Output:
lastnightstelevision.xml
Working demo

Related

Sitemap not working empty page show

My sitemap not working its not showing any url or data when i run my sitemap its showing blank empty page
Here is my code for sitemap and keywords is main table name and check image for my table
<?php
require dirname(__FILE__) . ("/includes/includes.php");
header("Content-Type: text/xml");
print "<?xml version='1.0' encoding='utf-8'?>";
$pageLimit = 50000;
$base = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
$base = substr($base,0,strrpos($base,"/")+1);
$link = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_name,$link);
if (isset($_GET["page"]))
{
print "<urlset xmlns='http://www.google.com/schemas/sitemap/0.84' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd'>";
$page = intval($_GET["page"]);
$from = (($page-1)*$pageLimit);
$sql = "SELECT * FROM keywords LIMIT ".$from.",".$pageLimit;
$result = mysql_unbuffered_query($sql,$link);
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
print "<url>";
print "<loc>".genSearchUrl(implode('-',array_filter(explode('-', str_replace(' ', '-', xmlentities(iconv("utf-8", "utf-8//ignore", $row["keyword"])))))))."</loc>";
print "</url>";
}
print "</urlset>";
}
else
{
print "<sitemapindex xmlns='http://www.google.com/schemas/sitemap/0.84' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/siteindex.xsd'>";
$sql = "SELECT count(*) as count FROM keywords";
$result = mysql_query($sql,$link);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$pages = ceil($row["count"] / $pageLimit);
for($i=1;$i<=$pages;$i++)
{
print "<sitemap>";
$loc = $base."sitemap.php?page=".$i;
print "<loc>".$web_path."sitemap_".$i.".xml</loc>";
print "</sitemap>";
}
print "</sitemapindex>";
}
exit();
function xmlentities($text)
{
$search = array('&','<','>','"','\'');
$replace = array('&','<','>','"','&apos;');
$text = str_replace($search,$replace,$text);
return $text;
}
?>

Return data from mysql where value equals URI

I'm trying to return a row from my database however, when I replace the page='url' with $filePath = $_SERVER["REQUEST_URI"]; page='.$filePath.' it returns nothing.
I'm assuming the answer is simple however, I can't see to find the solution.
Full Code
$filePath = $_SERVER["REQUEST_URI"];
$query = "SELECT * FROM Meta WHERE page=' . $filePath . '";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) { ?>
<title><?php echo $row["title"]; ?></title>
<?php } ?>
try the query like this
$query = "SELECT * FROM Meta WHERE page = '" . $filePath . "' ";

How to use parameters in url php?

I have a number pages namely
change=1.php
change=2.php
change=3.php
They all have similar coding but leaving 1 or 2 variable values.
And I know its a very bad idea! How can I make a link work like below:
change.php?id=1
change.php?id=2
change.php?id=3
http://oi62.tinypic.com/708gfm.jpg
<?php
include 'connection.php';
session_start();
include 'details.php';
/*$pkmn_id = $_SESSION['pkmn_id'];
$poke = $_SESSION['path'];*/
$data = mysql_query(" SELECT * FROM user_pokemon_db WHERE team = 1 AND user_id = '".$id."' ");
while($rows = mysql_fetch_array($data))
{
$rep_id = $rows[0];
$pkmn_id = $rows['pkmn_id'];
$path = mysql_query(" SELECT * FROM pokemons WHERE pk_id = '".$pkmn_id."' ");
$poke = mysql_result($path, 0, "path");
echo $poke;
echo "<br />";
$level = $rows['level'];
echo $level;
echo "<br />";
$exp = $rows['exp'];
echo $exp;
echo "<br />";
echo "<br />";
}
$data = mysql_query(" SELECT * FROM user_pokemon_db WHERE user_id = '".$id."' AND team = 0");
while($rows = mysql_fetch_assoc($data))
{
$db_id = $rows['id'];
$array[] = $db_id;
$level = $rows['level'];
$array1[] = $level;
$exp = $rows['exp'];
$array2[] = $exp;
$pkmn_id = $rows['pkmn_id'];
$data1 = mysql_query(" SELECT * FROM pokemons WHERE pk_id = '".$pkmn_id."' ");
while($rows = mysql_fetch_assoc($data1))
{
$poke = $rows['path'];
$array3[] = $poke;
}
}
$team = 1;
$_SESSION['team'] = $team;
$_SESSION['rep_id'] = $rep_id;
?>
My PHP code.
You probably want to use GET variables, for which you need to combine all the files into one, named change.php. In this file you need the line $foo = $_GET["id"] which will get the value of the variable "id" in the url change.php?id=1.
if (isset($_GET["id"])) {
$foo = $_GET["id"];
//your code here
}else{
echo 'ERROR!!! No id in URL';
}
You can have several variables in the URL like this: change.php?id=1&a=bar&b=toofoo
You can get current script's file name and parse integer.
__FILE__
gives current script's name. Then,
$myStr = preg_replace('/\.php$/', '', __FILE__);
$result = preg_replace('/change=$/', '', $myStr);
echo $result; // it's your id

How to create a new file in every iteration of a PHP program?

I am creating a file using PHP but I am unable to create a new file in every execution of the application. I am generating an XML file using a database. I want a new file in every execution of my code, how can I do that?
This is my code:
<?php
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "root";
$config['mysql_pass'] = "root";
$config['db_name'] = "dcu";
$config['table_name'] = "readingsThreePhase";
//$config['table_name1'] = "miosD1";
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
#mysql_select_db($config['db_name']) or die( "Unable to select database");
function writeMsg()
{
//echo "Hello world!";
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$root_element = $config['table_name']."D1";
$xml .= "<$root_element>";
$sql = "SELECT * FROM dcu.readingsThreePhase where meterId=3 order by recordTime DESC limit 1";
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if(mysql_num_rows($result)>0)
{
while($result_array = mysql_fetch_assoc($result))
{
//$xml .= "<".$config['table_name'].">\n";
$xml .= "<".$config['miosD1'].">\n";
foreach($result_array as $key => $value)
{
if($value==NULL)
{
continue;
}
$xml .= "<$key>";
$xml .= "$value";
$xml .= "</$key>\n";
}
$xml.="</".$config['miosD1'].">";
}
}
$xml .= "</$root_element>";
header ("Content-Type:text/xml");
echo $xml;
$file=fopen("xmlfilefirst.xml","a");
fwrite($file,$xml);
fclose($file);
}
writeMsg();
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$root_element = $config['table_name']."s";
$xml .= "<$root_element>";
$sql = "SELECT * FROM dcu.readingsThreePhase where meterId=3 order by recordTime DESC limit 1";
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if(mysql_num_rows($result)>0)
{
while($result_array = mysql_fetch_assoc($result))
{
$xml .= "<".$config['table_name'].">\n";
foreach($result_array as $key => $value)
{
if($value==NULL)
{
continue;
}
$xml .= "<$key>";
$xml .= "$value";
$xml .= "</$key>\n";
}
$xml.="</".$config['table_name'].">";
}
}
$xml .= "</$root_element>";
header ("Content-Type:text/xml");
echo $xml;
$file=fopen("xmlfilefirst.xml","a");
fwrite($file,$xml);
fclose($file);
?>
use file_put_contents
This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.
If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.
To create a new file every time, you need a way to generate a unique filename. You can either create this name with some sort of gibberish, such as:
do {
$filename = 'my_file_' . substr(md5(time( ), 0, 8 ) . '.xml';
} while( file_exists( $filename ) );
Or, you can attempt with some sort of numeric naming convention ('myfile1.xml', 'myfile2.xml', 'myfile3.xml' etc). This would mean getting a listing of the current directory. If no files are present, start with 1. Otherwise, start with the count of files + 1 and name your file with that.
See also the documentation for fopen. Most of the modes will attempt to create a file if the specified filename does not exist, so just coming up with a unique filename is the trick.

Search and erase db rows with file checker

Hi I am trying to get a file path from the embed field, use that path to see if a file exists in that path. If the file dose not exist then delete that entry. I am running a game site and the script that downloads the game file skipped a few so its like finding a needle in a 4000 entry db haystack. Any help is appreciated. Here is my code:
<?php
if(isset($_POST['clean'])) {
$query = "SELECT * FROM games WHERE embed";
$result = mysql_query($query) or die ("no query");
$result_array_path = array();
while($row = mysql_fetch_assoc($result))
{
$result_array_path = $row;
}
$count = mysql_num_rows($result);
for($counter=0;$counter<$count;$counter++){
if (file_exists($result_array_path[$counter])){
}else{
mysql_query("DELETE FROM games WHERE embed".$result_array_path[$counter]);
echo $result_array_path[$counter]." ";
}
}
}
?>
--------------EDIT-------------
I Updated the code to BUT it decides to delete my entire database instead of deleting the missing game entries. Here is the revised code:
<?php
if(isset($_POST['clean'])) {
$query = "SELECT * FROM games WHERE embed NOT LIKE '%mochiads.com%'";
$result = mysql_query($query) or die ("no query");
$result_array_path = array();
while($row = mysql_fetch_assoc($result))
{
$result_array_path[] = $row['embed'];
}
foreach($result_array_path as $path) {
if(!file_exists($path)) {
mysql_query("DELETE FROM games WHERE embed = '" . $path . "'");
echo $path." | ";
}
}
}
?>
-----------EDIT--------------
I debugged the program so it works now. Had to add a "$_SERVER['DOCUMENT_ROOT']" to the program. Here is the finished program
<?php
if(isset($_POST['clean'])) {
$query = "SELECT * FROM games WHERE embed NOT LIKE '%mochiads.com%'";
$result = mysql_query($query) or die ("no query");
$result_array_path = array();
while($row = mysql_fetch_assoc($result))
{
$result_array_path[] = $row['embed'];
}
foreach($result_array_path as $path) {
$rel_path = str_replace('http://www.flamegame.net', '', $path);
$rel_path = str_replace('http://flamegame.net', '', $rel_path);
$rel_path = str_replace('%20', ' ', $rel_path);
if(! file_exists($_SERVER['DOCUMENT_ROOT'] . $rel_path)) {
mysql_query("DELETE FROM games WHERE embed = '" . $path . "'");
echo $rel_path." | ";
}
}
}
?>
Thanks For all of the help Especially Gargron.
For those who are wondering this program is for my website:
http://www.FlameGame.net
I see one typo that might be your problem:
$result_array_path = array();
while($row = mysql_fetch_assoc($result))
{
// You want to append the row to the results array
// instead of replacing the array each time, so []
$result_array_path[] = $row['embed'];
// That's assuming the table field containing the path is "embed"
}
And instead of using mysql_num_rows you could instead iterate through the items in $result_array_path like this:
foreach($result_array_path as $path) {
if(! file_exists($path)) {
// Delete
// You missed a = in the query too
mysql_query("DELETE FROM games WHERE embed = '" . $path . "'");
}
}
That should make it work.

Categories