Solved

Assistance needed with php code

Posted on 2009-05-14
8
198 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

770 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