• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Assistance needed with php code

Hi all,

I am having problem trying to put some code together.

I have the following code:

...
$query = "select * from tbl_news ORDER BY id DESC" ;
$result = mysql_query($query) or die("Query failed") ;

...

I then have this code:

$url_product = mysql_result($result,$i,'news_title');

which is displaying my domain name and the news title from the database like this:

www.mydomain.comthe-news-title-here ... now...I need to change it to do the following:

www.mydomain.com/news/fullnewsfile.php?news_title=

So, $url_product = mysql_result($result,$i,'news_title'); is dispalying:

this: www.mydomain.comthe-news-title-here

I need to add a "/" after the "www.mydomain.com then this path "news/fullnewsfile.php?news_title=" and after that the "news_title" field from the database.

Hope I've explained properly.

thanks

st3vo


0
ST3VO
Asked:
ST3VO
  • 4
  • 4
1 Solution
 
cevomanCommented:
$query = "select * from tbl_news ORDER BY id DESC" ;
$result = mysql_query($query) or die("Query failed") ;


$urlData = mysql_fetch_array($result);
$url_product = "http://www.mydomain.com/news/fullnewsfile.php?news_title=".urlencode($urlData['news_title']);

0
 
cevomanCommented:
or if your looping

$query = "select * from tbl_news ORDER BY id DESC" ;
$result = mysql_query($query) or die("Query failed") ;

if($rows=mysql_num_rows($result))
{
      for($x=0;$x<$rows;$x++)
      {
            $urlData = mysql_fetch_array($result);
            $url_product = "http://www.mydomain.com/news/fullnewsfile.php?news_title=".urlencode($urlData['news_title']);
      }
}
0
 
ST3VOAuthor Commented:
Perfect thanks :o)
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ST3VOAuthor Commented:
I just have a small problem with the code.

For example...The records are 1 foward so...title1 links to record 2 title2 links to record 3 and so on..

Is that hard to fix pls?
0
 
cevomanCommented:
Could you post the full code please it's probably something obvious
0
 
ST3VOAuthor Commented:
Sure here is the full code

p.s. There are lots of commented code.


thanks

see code attached.

thx


<?php
// Insert here the the title of your feed
$rss_title= "mysite.com RSS feed";  
// Insert your site, in the format site.com
$rss_site= "mysite.com" ;
// Insert the description of your website
$rss_description= "Mysite News RSS feed";
// Applicable language of the feed. For spanish, change to "es"
$rss_language="en";                     
// Address of the logo file. It can be called whatever you want it to be!
$rss_logo="http://www.".$rss_site."/images/rss.jpg";  
// the feed's author email
$emailadmin="st3vo@mysite.com";   
 
// set the file's content type and character set
// this must be called before any output
header("Content-Type: text/xml;charset=iso-8859-1");
 
 
// Open the database
 
  //My local DB
//mysql_connect("localhost","root","");
//@mysql_select_db("db2") or die("Unable to select DB");
 
// Select all the records from the Articles/Blog table - whatever you're using it for
 
$query = "select * from tbl_news ORDER BY id DESC" ;
$result = mysql_query($query) or die("Query failed") ;
 
echo
'<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
    <channel>
      <title>'.$rss_title.'</title>
      <link>http://www.'.$rss_site.'</link>
      <description>'.$rss_description.'</description>
      <language>en-en</language>
    <image>
<url>'.$rss_logo.'</url>
<title>'.$rss_site.'</title>
<link>http://www.'.$rss_site.'</link>
</image>';
 
if($rows=mysql_num_rows($result)) //new
{ //new
 
for($x=i;$i<$rows;$i++) //new
//for($i=0;$i<6; $i++) //will loop through 6 times to generate 6 RSS items
{
	
	$photo_name = 'http://www.mysite.com/photo/me.gif';     
 
	$subject = mysql_result($result,$i,'news_title'); //subject line for the RSS item
 
	
	
	$urlData = mysql_fetch_array($result); //new
                $url_product = "/news/fullnews.php?news_title=".urlencode($urlData['news_title']); //new
	
	
	// Define a description of the item
	
	$description = mysql_result($result,$i,'full_story'); 
	
	// Clean the description
	
	$description = str_replace ("&amp","",htmlspecialchars(strip_tags($description)));
	
	// Pass tags to describe the product - this has been left ouf of this example
	
	$rss_tags = 'tag1, tag2';
	
	//This is a teaser of your article, basically what RSS readers will show the user in their inbox.  
	//This is how you entice users to come over and read the full article
	
	//the easiest way is to just take the first few hundred characters of the content (description)
	
	$short_description = substr($description,0,500) . "...";
	
	//so you can define when it was published
	
	$timestamp = mysql_result($result,$i,'thedate');
	
	//cleans the timestamp into an RSS friendly format
	
	$pubdate = date("r", strtotime($timestamp));
	
	//outputs the RSS item
	
	echo
	'
		<item>
			<title>'.$subject.'</title>
				<link>http://www.'.$rss_site.$url_product.'</link>
				<guid isPermaLink="true">http://www.'.$rss_site.$url_product.'</guid>
					<description>'.$short_description.'</description>
					<pubDate>'.$pubdate.'</pubDate>
		</item>
	
	
	';
	
	
} //end of the for-loop
 
} //new
 
mysql_close(); //close the DB
 
echo //close the XML file
' </channel>
</rss>';
 
 
?>

Open in new window

0
 
cevomanCommented:
When you call "mysql_fetch_array()" it puts the information from the current row of the DB and puts into the variable it's assigned to, it then calls the next row so there is no need to use "mysql_result()" each time which is then moving the DB pointer forward 1.

I've replaced the code below
after             $dbData = mysql_fetch_array($result); //new

if you put print_r($dbData);
it will show you exactly what I mean

Kind regards
James
<?php
// Insert here the the title of your feed
$rss_title= "mysite.com RSS feed";  
// Insert your site, in the format site.com
$rss_site= "mysite.com" ;
// Insert the description of your website
$rss_description= "Mysite News RSS feed";
// Applicable language of the feed. For spanish, change to "es"
$rss_language="en";                     
// Address of the logo file. It can be called whatever you want it to be!
$rss_logo="http://www.".$rss_site."/images/rss.jpg";  
// the feed's author email
$emailadmin="st3vo@mysite.com";   
 
// set the file's content type and character set
// this must be called before any output
header("Content-Type: text/xml;charset=iso-8859-1");
 
 
// Open the database
 
  //My local DB
//mysql_connect("localhost","root","");
//@mysql_select_db("db2") or die("Unable to select DB");
 
// Select all the records from the Articles/Blog table - whatever you're using it for
 
$query = "select * from tbl_news ORDER BY id DESC" ;
$result = mysql_query($query) or die("Query failed") ;
 
echo
'<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
    <channel>
      <title>'.$rss_title.'</title>
      <link>http://www.'.$rss_site.'</link>
      <description>'.$rss_description.'</description>
      <language>en-en</language>
    <image>
<url>'.$rss_logo.'</url>
<title>'.$rss_site.'</title>
<link>http://www.'.$rss_site.'</link>
</image>';
 
if($rows=mysql_num_rows($result)) //new
{ //new
 
	for($x=i;$i<$rows;$i++) //new
	//for($i=0;$i<6; $i++) //will loop through 6 times to generate 6 RSS items
	{
		
		$photo_name = 'http://www.mysite.com/photo/me.gif';     
	
		
	
		
		
		$dbData = mysql_fetch_array($result); //new
		$url_product = "/news/fullnews.php?news_title=".urlencode($dbData['news_title']); //new
		
		$subject = $dbData['news_title']; //subject line for the RSS item
		
		// Define a description of the item
		
		$description = $dbData['full_story'];
		
		
		
		// Clean the description
		
		$description = str_replace ("&amp","",htmlspecialchars(strip_tags($description)));
		
		// Pass tags to describe the product - this has been left ouf of this example
		
		$rss_tags = 'tag1, tag2';
		
		//This is a teaser of your article, basically what RSS readers will show the user in their inbox.  
		//This is how you entice users to come over and read the full article
		
		//the easiest way is to just take the first few hundred characters of the content (description)
		
		$short_description = substr($description,0,500) . "...";
		
		//so you can define when it was published
		
		$timestamp = $dbData['thedate']
		
		//cleans the timestamp into an RSS friendly format
		
		$pubdate = date("r", strtotime($timestamp));
		
		//outputs the RSS item
		
		echo
		'
			<item>
				<title>'.$subject.'</title>
					<link>http://www.'.$rss_site.$url_product.'</link>
					<guid isPermaLink="true">http://www.'.$rss_site.$url_product.'</guid>
						<description>'.$short_description.'</description>
						<pubDate>'.$pubdate.'</pubDate>
			</item>
		
		
		';
		
		
	} //end of the for-loop
 
} //new
 
mysql_close(); //close the DB
 
echo //close the XML file
' </channel>
</rss>';
 
 
?>

Open in new window

0
 
ST3VOAuthor Commented:
Perfect!!! Thanks a million for all your help! :o)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now