I have a custom component being used on a Joomla 3.9 site.
The below code creates hyperlinks with proper anchors ( for 2 pages ) from a database and works OK except that it's also creating irrelevant hidden links.
You can only see the href links if you view the source of the page.
i.e. <a href="https://www.example.com/main-category/sub-cat1/"></a>
What can I try here to make sure the code doesn't create these hidden links?
else:
JFactory::getDocument()->addStyleDeclaration('@media (max-width:540px){table.all-regions tr td{width: 100% !important;display: inline-block !important;text-align: center !important;}}');
$regionsList = '';
$regionsList .= '<table >';
$regions = $this->get('regions');
for($i = 0;$i <= count($regions); $i =3):
$regionsList .= '<tr>';
$regionsList .= '<td style="width: 1%;padding: 9px; line-height: 24px;"><a href="'.JUri::root().'main-category/'.$country.'/'.strtolower(implode('-',explode(' ',$regions[$i]->region_name))).'">'.$regions[$i]->region_name.'</a></td>';
$regionsList .= '<td style="width: 1%;padding: 9px; line-height: 24px;"><a href="'.JUri::root().'main-category/'.$country.'/'.strtolower(implode('-',explode(' ',$regions[$i 1]->region_name))).'">'.$regions[$i 1]->region_name.'</a></td>';
$regionsList .= '<td style="width: 1%;padding: 9px; line-height: 24px;"><a href="'.JUri::root().'main-category/'.$country.'/'.strtolower(implode('-',explode(' ',$regions[$i 2]->region_name))).'">'.$regions[$i 2]->region_name.'</a></td>';
$regionsList .= '</tr>';
endfor;
$regionsList .= '</table>';
$text = JString::str_ireplace('{%regions_list%}', $regionsList, $this->article->text);
$this->setBreadcrumbs(
array(
'country'
)
);
endif;
echo $text;
Details that may or may not help:
One page shows 3 hidden links and the other 2 hidden links. The hidden links point to the same page I'm currently on. The page with 3 hidden links is showing 3 even columns of links while the one with 2 hidden links has one row with 5 links and the other 2 rows with 4 links
CodePudding user response:
There are better ways to do this whole thing, but without rewriting the code, you need to see if those elements are set and aren't empty or else you will get empty anchors:
if(!empty($regions[$i]->region_name)) {
$regionsList .= '<td style="width: 1%;padding: 9px; line-height: 24px;"><a href="'.JUri::root().'main-category/'.$country.'/'.strtolower(implode('-',explode(' ',$regions[$i]->region_name))).'">'.$regions[$i]->region_name.'</a></td>';
}
if(!empty($regions[$i 1]->region_name)) {
$regionsList .= '<td style="width: 1%;padding: 9px; line-height: 24px;"><a href="'.JUri::root().'main-category/'.$country.'/'.strtolower(implode('-',explode(' ',$regions[$i 1]->region_name))).'">'.$regions[$i 1]->region_name.'</a></td>';
}
if(!empty($regions[$i 2]->region_name)) {
$regionsList .= '<td style="width: 1%;padding: 9px; line-height: 24px;"><a href="'.JUri::root().'main-category/'.$country.'/'.strtolower(implode('-',explode(' ',$regions[$i 2]->region_name))).'">'.$regions[$i 2]->region_name.'</a></td>';
}