Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Assistance needed with php code

Posted on 2009-05-14
8
Medium Priority
?
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

610 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