In template page_list get image attribute Concrete5 - php

My following code is based on
1.Get current URL
2.Go through array and check if in url value = to value in array
do this:
$on_this_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
foreach ($city_array as $sandwich) {
if (strpos($on_this_link, $sandwich) == true) {
$sandwich = trim($sandwich, '/');
$city = $sandwich;
if ($city == 'newyork') {
foreach ($category_array as $double_sandwich) {
if (strpos($on_this_link, $double_sandwich) == true) {
$double_sandwich = trim($double_sandwich, '/');
$category_is = $double_sandwich;
Loader::model('page_list');
$nh = Loader::helper('navigation');
$pl = new PageList();
$pl->filterByAttribute('city', '%' . $city . '%', 'like');
$pl->filterByAttribute('category','%'.$category_is.'%','like');
$pl->sortByDisplayOrder();
$pagelist = $pl->get();
foreach ($pagelist as $p) {
echo '<li> ' .htmlspecialchars($p->getCollectionName()) . ' </li>';
?>
}
}
}
}
So It will show me only pages that have the same attribute with URL
Each of this page has image attribute that I want to show.
How can I pass this Image Attribute??

Check out the comments in the page list block's view template:
https://github.com/concrete5/concrete5/blob/master/web/concrete/blocks/page_list/view.php#L33
You can get image attributes by putting some code like this inside your foreach ($pagelist as $p) loop:
$img = $p->getAttribute('example_image_attribute_handle');
if ($img) {
//you could output the original image at its full size like so:
echo '<img src="' . $img->getRelativePath() . '" width="' . $img->getAttribute('width') . '" height="' . $img->getAttribute('height') . '" alt="" />';
//or you could reduce the size of the original and output that like so:
$thumb = Loader::helper('image')->getThumbnail($img, 200, 100, false); //<--200 is width, 100 is height, and false is for cropping (change to true if you want to crop the image instead of resize proportionally)
echo '<img src="' . $thumb->src . '" width="' . $thumb->width . '" height="' . $thumb->height . '" alt="" />';
}

Thx but I did it already just another way!
I didnt used
Loader::model('page_list');
Instead I used:
$blockType = BlockType::getByHandle('page_list');
And hardcoded the block!
$th = Loader::helper('text');
$ih = Loader::helper('image');
$page_current = Page::getCurrentPage();
$page_2 = $page_current->getCollectionHandle();
$img = $page->getAttribute('product'); $thumb =
$ih->getThumbnail($img, 240,150, false);
And after I changed a little bit my Code above:
$on_this_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
foreach ($city_array as $sandwich) {
if (strpos($on_this_link, $sandwich) == true) {
$sandwich = trim($sandwich, '/');
$city = $sandwich;
if ($city == 'newyork') {
foreach ($category_array as $double_sandwich) {
if (strpos($on_this_link, $double_sandwich) == true) {
$double_sandwich = trim($double_sandwich, '/');
$category_is = $double_sandwich;
$city_att = $page->getAttribute('city', '%' . $city . '%', 'like');
$sub_cat_att = $page->getAttribute('category','%'.$category_is.'%','like'); ?>
<?php if($city == $city_att && $category_is == $sub_cat_att){ ?><li><img src="<?php echo $thumb->src ?>" width="<?php echo $thumb->width ?>" height="<?php echo $thumb->height ?>" alt="" />
<h3> <?php echo $title ?></h3>
<div class="product_description">
<?php echo $description ?>
</div>
Read More...
</li> <?php } ?> <?php
}
}
}
So everything it is working But still thx for respond! Apreciate

Related

PHP echo linking

How can I add a link to the following line?
if ($image) echo $image->resize('w=272&h=170');
I have tried adding something like
if ($image) echo ''$image->resize('w=272&h=170')'';
if ($image) {
echo '' . $image->resize('w=272&h=170') . '';
}
Try the following (untested):
if ($image) echo '<img src="' . $image->resize('w=272&h=170') . '"/>';
The result from $image->resize() in wordpress renders the destination... A better solution would be the following:
if($image && ($imageResized = $image->resize('w=272&h=170'))) {
echo '' . $imageResized . '';
}
You can do this:
echo ($image ? ''. $image->resize('w=272&h=170'). '' : '');

how to make an image title array

I am using the following code to populate images from MySQL database. I want to have image_title for every image which will be a textbox below the image. I know how to populate it but how can I make it named so when user changes the title it will only change for the specific image?
<?php
$mysql_command = "CALL sp_populate_member_article_media(:param_article_guid, :param_member_id)";
$mysql_query = $mysql_connection->prepare($mysql_command);
$mysql_query->bindParam(':param_article_guid', $_GET["aid"], PDO::PARAM_STR);
$mysql_query->bindParam(':param_member_id', $_SESSION["volow_member_id"], PDO::PARAM_STR);
$mysql_query->execute();
while($mysql_row = $mysql_query->fetch())
{
list($width, $height) = getimagesize($_SESSION["volow_absolute_path"] . "/gallery/members/" . $_SESSION["volow_member_guid"] . "/" . $_SESSION["volow_channel_guid"] . "/" . $_GET["aid"] . "/". $mysql_row['file_name'] . ".png");
if ($width > 650) { $width = 650; };
if ($height > 271) { $height = 271; };
?>
<tr><td align="right" valign="top"><img src="<?php echo $_SESSION["volow_domain_name"]; ?>gallery/members/<?php echo $_SESSION["volow_member_guid"]; ?>/<?php echo $_SESSION["volow_channel_guid"]; ?>/<?php echo $_GET["aid"]; ?>/<?php echo $mysql_row['file_name']; ?>.png" width="<?php echo $width; ?>" height="<?php echo $height; ?>" /></td></tr>
<tr><td><hr></td></tr>
<?php } ?>
You need to give a specific name for input files of each image. You need a unique identifier for each image for this purpose.
Assuming you have file_id field for each image, you can generate form elements as follows:
<input type="text" name="title<?=$mysql_row['file_id']?>" />
Then when handling form submission, you need to iterate over $_POST array to find out which image ids were submitted:
foreach ($_POST as $name => $value) {
if (substr($name, 0, 5) == 'title') {
$id = (int) substr($name, 5);
$title = $value;
$sql = "UPDATE imagetable SET title = " . mysqli_real_escape_string($title) . " WHERE id = " . $id;
...
// run sql
...
}
}

Count filled SQL array fields PHP

I´m fetching an array from a mysql query like this.
$viewerSQL = mysql_query("SELECT * FROM `this` WHERE id = ".
mysql_real_escape_string($_GET[id]) ."");
$pageData = mysql_fetch_array($pageSql);
I get an array with like 40 entries, i want to check seven of them by name if they are filled.
What i´ve tried and what I thought about so far:
$oioi = 0;
while($oioi<7){
$oioi++;
$pic = "pic".$oioi;
$active="";
$path = "./upload/objectoffers/$viewerData[id]/$pic";
$picDesc = "pic".$oi."Desc";
$picArray = '$viewerData['.$pic.']';
echo $pic."<br>";
echo $picArray."<p>";
if(!empty($path)){
if($oioi==1){$active = "active";}?>
<div class="item <?=$active;?>">
<img style="width: 100%;" src="./images/<?=$viewerData[$pic];?>" alt="<?=$viewerData[$picDesc]?>">
</div>
<?
}}
?>
But this doesn´t work because: $path is NEVER empty...I wanted to avoid to write "if(!empty)" for every single $viewerData[pic1] to $viewerData[pic7] array manual...
that´s why i´m here...
maybe you can help me improving this or offer some tips.
Thanks!
edit: This is what I get as "output":
pic1
$viewerData[pic1]
pic2
$viewerData[pic2]
pic3
$viewerData[pic3]
pic4
$viewerData[pic4]
pic5
$viewerData[pic5]
pic6
$viewerData[pic6]
pic7
$viewerData[pic7]
I think you should use file_exists instead !empty:
$oioi = 0;
while ($oioi < 7) {
$oioi++;
$pic = "pic" . $oioi;
$active = "";
$path = "./upload/objectoffers/$viewerData[id]/$pic";
$picDesc = "pic" . $oi . "Desc";
$picArray = '$viewerData[' . $pic . ']';
echo $pic . "<br>";
echo $picArray . "<p>";
if (file_exists($path)) {
if ($oioi == 1) {
$active = "active";
}
?>
<div class="item <?= $active; ?>">
<img style="width: 100%;" src="./images/<?= $viewerData[$pic]; ?>" alt="<?= $viewerData[$picDesc] ?>">
</div>
<?php
}
}

PHP/WordPress: override page_id in foreach loop

I have a WordPress site and currently the code it set to create titles based on the page title. The titles are: Interior House Painting, Exterior House Painting and Commercial Painting. I would like to override the titles to remove the word "house". This is the code currently:
<?php
// interior_painting: 18
// exterior_painting: 25
// other services: 36
$page_ids = array(18, 25, 36);
$images = array('servicesInterior.jpg', 'servicesExterior.jpg', 'servicesOther.jpg');
foreach ($page_ids as $key => $page_id) {
$page_post = get_post($page_id);
$page_custom_key = 'home_page_info';
$page_link = $page_post->post_name;
$li_class = $page_id == 36 ? 'noMargin' : '';
$title = $page_id == 18 ? 'Interior Painting' : $page_post->post_title . " ";
$title = $page_id == 36 ? 'Commercial Projects' : $page_post->post_title . " ";
$title = $page_id == 25 ? 'Exterior Painting' : $page_post->post_title . " ";
echo '<li class="' . $li_class . '"> <a href="' . $page_link . '">';
echo '<img class="alignright size-full wp-image-349" title="servicesInterior" src="/wp-content/uploads/2011/04/' . $images[$key] . '" alt="" width="200" height="95" /></a>';
echo '<h2>' . $title . '</h2>';
echo get_post_meta($page_id, $page_custom_key, true);
echo '<a class="btn-find-more" href="' . $page_post->post_name . '">FIND OUT MORE</a></li>';
}
?>
The output is this: Interior House Painting, Exterior Painting, Commercial Painting. How do I get "house" removed from "Interior House Painting"?
$title = ucwords(trim(strtolower(str_replace('house', '', $page_post->post_title))));
Though, I am a little curious as to why you would not prefer to just remove the word from the title altogether.
ADDITIONALLY: Somebody else mentioned using preg_replace. That's also entirely possible, but it would be best to do a case-insensitive search:
$title = trim(preg_replace('/house/i', '', $page_post->post_title));
Just use preg_replace('House'|| 'house','',/*Variable Page title is in*/);
You could try replacing the line:
echo '<h2>' . $title . '</h2>';
With something like this:
if (is_page('Interior House Painting')) { echo '<h2>Interior Painting</h2>'; }
if (is_page('Exterior House Painting')) { echo '<h2>Exterior Painting</h2>'; }
else { echo '<h2>' . $title. '</h2>'; }

Adding alt img tags to an existing php website

I am currently trying to add in alt img tags for an existing website running the interspire shopping cart platform, I have gotten pretty close I bleieve, but i cannot seem to get it right. Any help would be grealty appreciated.
// Is there a thumbnail image we can show?
$thumb = $GLOBALS['ISC_CLASS_PRODUCT']->GetThumb();
$alttext = $GLOBALS['ISC_CLASS_PRODUCT']->GetProductName();
if ($thumb == '' && GetConfig('DefaultProductImage') != '') {
if (GetConfig('DefaultProductImage') == 'template') {
$thumb = GetConfig('ShopPath').'/templates/'.GetConfig('template').'/images/ProductDefault.gif';
} else {
$thumb = GetConfig('ShopPath').'/'.GetConfig('DefaultProductImage');
}
$thumbImage = '<img src="'.$thumb.'" alt="->GetProductName" />';
} else if ($thumb != '') {
$thumbImage = '<img src="'.GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/'.$thumb.'" alt=""'.$alttext.'" />';
}
I have tried posting the code but it says new users cannot post image tags for some reason
It looks to me like you have two double quotes after you open the alt attribute, then the text, then another closing quote.
This line won't work
$thumbImage = '<img src="'.$thumb.'" alt="->GetProductName" />';
You probably want something like this
$thumbImage = '<img src="'.$thumb.'" alt="'.$GLOBALS['ISC_CLASS_PRODUCT']->GetProductName().'" />';
//or as you have already set $alttext:
$thumbImage = '<img src="'.$thumb.'" alt="' . $alttext . '" />';
ylebre meant: (scroll the code box to the right)
} else if($thumb != '') {
$thumbImage = '<img src="'.GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/'.$thumb.'" alt=""'.$alttext.'" />';
}
^
|
there is one extra " at the end!
} else if($thumb != '') {
$thumbImage = '<img src="'.GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/'.$thumb.'" alt="' . htmlspecialchars($alttext) . '" />';
}

Categories