Solved

Assistance needed with php code

Posted on 2009-05-14
8
200 Views
Last Modified: 2012-05-07
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
Comment
Question by:ST3VO
  • 4
  • 4
8 Comments
 
LVL 3

Expert Comment

by:cevoman
ID: 24390958
$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
 
LVL 3

Accepted Solution

by:
cevoman earned 500 total points
ID: 24390984
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
 

Author Closing Comment

by:ST3VO
ID: 31581746
Perfect thanks :o)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 

Author Comment

by:ST3VO
ID: 24391163
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
 
LVL 3

Expert Comment

by:cevoman
ID: 24391175
Could you post the full code please it's probably something obvious
0
 

Author Comment

by:ST3VO
ID: 24391224
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
 
LVL 3

Expert Comment

by:cevoman
ID: 24391275
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
 

Author Comment

by:ST3VO
ID: 24391323
Perfect!!! Thanks a million for all your help! :o)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question