How to get URL to article from ID?

Discussion and education for beginner / novice programmers interested in embarking on the development process to take advantage of the extensible nature of the Joomla! CMS.
Locked
ShiRaTo
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Fri Sep 21, 2007 9:58 am

How to get URL to article from ID?

Post by ShiRaTo » Mon Nov 26, 2007 4:37 pm

Hi all,

I'm using Joomla 1.5
I just wondering how can I create a link to article link this -> index.php?view=article&catid=10&id=20&option=com_content&Itemid=30
if I know only id of article (in this example it's =20)

I try to write code like this
">link

but It's not use the template that I assign for that article..  it's only work if I add more parameter, the Itemid=30 to it..  but It's cannot hard-code..  it's depend..

so..  are there any API or how to make a link if I know only article id?

Thank you very much,


ps. I can't query database to find it..  because It will be very slow since on that page I want to show a lot of articles..
Last edited by ShiRaTo on Mon Nov 26, 2007 4:39 pm, edited 1 time in total.

ShiRaTo
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Fri Sep 21, 2007 9:58 am

Re: How to get URL to article from ID?

Post by ShiRaTo » Fri Nov 30, 2007 1:27 pm

now my problem is solved by this.

Code: Select all

include dirname(__FILE__).'/../../components/com_content/helpers/route.php';

// Search Joomla to get Category ID of each article.
$db = &JFactory::getDBO();
$idList = "";
foreach ($resArticle->docs as $no => $doc) {
	$idList .= $doc->sums["cityguidearticleid"].",";
}
$idList = substr($idList, 0, -1);
$db->setQuery('SELECT id,catid FROM #__content WHERE id in ('.$idList.')');
$rows = $db->loadObjectList();

foreach ( $rows as $row ) {
	$articleLink[$row->id] = $row->catid;
}

and when I want to show..
<a href="<?php echo ContentHelperRoute::getArticleRoute($articleId, $articleLink[$articleId]); ?>">Article <?php echo $articleId; ?></a>

explain:  I query DB for all article I have to get category ID list...  when I want to show I call ContentHelperRoute::getArticleRoute and send article id with category id...


I'm not sure this's the best way or not..  but I use 1.5RC3 and I can't find how to do this...
I've read JApplication class that says "Deprecated, use ContentHelper::getItemid instead."  but I think that'll query db everytime?  or not?  I can't find that source code in google or search forums here

seanray
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Wed Mar 12, 2008 6:40 am

Re: How to get URL to article from ID?

Post by seanray » Sun Jun 08, 2008 9:09 am

I believe you are doing the right thing here. When I look at the source code of Joomla and try to find out how they handle artitcle link in module "Latest News", they use the same way as your mention in above, see below:
$lists[$i]->link = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catslug, $row->sectionid));

ah72
Joomla! Explorer
Joomla! Explorer
Posts: 290
Joined: Sat Mar 15, 2008 9:43 pm
Contact:

Re: How to get URL to article from ID?

Post by ah72 » Sun Jun 08, 2008 9:16 am

Hello,

you can link easily to articles with this

Code: Select all

http://yoursite.com/index.php?option=com_content&view=article&id=20
or even simpler

Code: Select all

http://yoursite.com/index.php?option=com_content&id=20
Regards
web2host.co.cc - Quality Free Hosting without ads - Joomla! auto-installation!!

seanray
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Wed Mar 12, 2008 6:40 am

Re: How to get URL to article from ID?

Post by seanray » Sun Jun 08, 2008 10:14 am

hi ah72,
I think your method works as a general, but what we need here is a Search Engine Friendly URLs.

thanks,
Ray.


Locked

Return to “Joomla! Coding 101”