Abu Barrett
Abu Barrett

Reputation: 149

joomla sql query

ok so i dont know much about sql but im trying to make a dinamic redirect for my website, the idea is to redirect each user to an article called the same as the user is called, so here's a little code but i dont know how to finish it :(

$database->setQuery("SELECT id "." FROM #__content "." WHERE state='1' "." ");
$rows = $database->loadObjectList();
foreach($rows as $user_name){   
$article_id = $row->id; 
break;
}
$redirect_url = 'index.php?option=com_content&view=article&id='.$article_id;

$user_name and $redirect_url are "premade" options so dont worry about it, i just need to know how to actually do the query hehe, thanks for the help :D

Upvotes: 1

Views: 1004

Answers (1)

Mihai Todor
Mihai Todor

Reputation: 8239

It looks like you intend to return a single article_id, so that SQL query should always return a single row (value), in which case, that foreach must disappear. The thing is that you need to rewrite your SQL query and add another condition to the WHERE clause. Perhaps something like this:

$user =& JFactory::getUser();
$database->setQuery("SELECT id FROM #__content WHERE state='1' AND title = '".$user->name."'");
$row = $database->loadAssoc();
$redirect_url = 'index.php?option=com_content&view=article&id='.$row['id'];

Please note that I have not tested the above code.

Upvotes: 1

Related Questions