Reputation: 2659
I am trying to read the intro image from a joomla database. I have a list of products, that each need to show the intro image that matches the ID of the product.
// content
$content = "SELECT * FROM `snm_content` WHERE catid = 13";
$contentcon = $conn->query($content);
$contentcr = array();
while ($contentcr[] = $contentcon->fetch_array());
$img = $contentcr[0]['images'];
$plaatje = explode('/', $img);
$plaatje = explode('"', $plaatje[2]);
$plaatje = $plaatje[0];
$img = preg_replace('/[^a-zA-Z0-9\']/', '_', $img);
$img = explode('___', $img);
$img = $img[1];
Then in a foreach I put the following code:
<img src="cms/images/website/'.$plaatje.'" alt="" class="company_logo">
This shows the same image for every product though.
I read something about json_decode to get the image path from the database field.
How can I get the correct image for every product?
The following code prints the correct array:
print_r($content['images']);
Example of a field in the database with all the possible images (intro, full etc)
{"image_intro":"images\/website\/Airco_blog.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
Upvotes: 2
Views: 590
Reputation: 324
I had the same problem. I was trying to get Twitter to add my link just as facebook does automatically but had to investigate the CARD Validator here https://cards-dev.twitter.com/validator ...Tthe way I have it working on Joomla 3.9.11 is adding this code in the head of my template default.php ... I was able to properly add the title, the description and the right picture, usually the first one on the article.
<head>
<?php
//Added for TWITTER
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& JTable::getInstance("content");
$article->load($article_id);
$theArticle = $article->get("title");
$theImages = $article->get("images");
$pictures = json_decode($theImages); // Split the parameters apart
$timage= "http://yourdomain.com/".$pictures->{'image_intro'};
}
$doc =& JFactory::getDocument();
$doc->addCustomTag( '
<meta name="twitter:title" content="'.mb_strimwidth(strip_tags($theArticle),0,225, " ...").'"=""/>
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="yoursite">
<meta name="twitter:creator" content="yourcreator">
<meta name="twitter:url" content="'.JURI::current().'">
<meta name="twitter:description" content="'.mb_strimwidth(strip_tags($doc->getMetaData( 'description' )),0,225, " ...").'"=""/>
<meta name="twitter:image" content="'.$timage.'">
');
?>
<jdoc:include type="head" />
<?php $this->loadBlock('head') ?>
</head>
Upvotes: 0
Reputation: 1523
I guess this will work for you :
$article_images = $content['images'] // Get image parameters of the article
$pictures = json_decode($article_images); // Split the parameters apart
echo "<img src='" . $pictures->{'image_intro'} . "' alt='" . $pictures->{'image_intro_alt'} . "'>"; // get the intro image
// Edited your post to fix the image tag.
Upvotes: 4