PHP echoing <img>/<a> with attributes - php

I need some help with echoing a <img> tag with attributes like title, rel, and class.
I have made it this far, when I'm echoing a filename from a db to search in a catalogue to find it. But I'm not sure how to write some attributes to it since I'm going to display it with Pirobox.
This is what i got working:
echo '<a href="uploads/'.$row['bildnamn'].'">';
echo '<img src="uploads/'.$row['thumb_bildnamn'].'">';
echo '</a>';
But I also need these attributes for the <A> tag which makes the image large.
rel="gallery" class="pirobox_gall" title="$row['uploaded']" . " " . "$row['user']";
What I don't get to work is how to get that line together with:
echo '<a href="uploads/'.$row['bildnamn'].'">';

You should be able to concatenate everything in the <a> tag like so:
echo '<a href="uploads/' . $row['bildnamn'] . '" rel="gallery" class="pirobox_gall" title="' . $row['uploaded'] . ' ' . $row['user'] . '">';
echo '<img src="uploads/' . $row['thumb_bildnamn'] . '">';
echo '</a>';
I inserted spaces to help emphasize where PHP does concatenation. In your case, a single quote starts/ends the string for PHP; a double quote is ignored and goes into the HTML. So this part:
title="' . $row['uploaded'] . ' ' . $row['user'] . '"
will make the title be the value of the uploaded column, then a space, then the value of the user column. Then just end the a tag with a >.

you could continue to concatenate the string
echo '<a href="uploads/'.$row['bildnamn'].'"'. 'rel="gallery" class="pirobox_gall" title="'.$row['uploaded'].' '.$row['user'].'">';

Try this:
echo '<a href="uploads/' . $row['bildnamn'] . '" rel="gallery" class="pirobox_gall" title="' . $row['uploaded'] . ' ' . $row['user'] . '">';
echo '<img src="uploads/' . $row['thumb_bildnamn'] . '">';
echo '</a>';

You can do this using string concatenation, like this:
$anchor = '<a href="uploads/'.$row['bildnamn'].'"';
$anchor .= 'rel="gallery" class="pirobox_gall" title="' . $row['uploaded'] . ' ' . $row['user'] . '">';
$anchor .= '<img src="uploads/'.$row['thumb_bildnamn'].'"></a>';
echo $anchor;

Related

Dynamic link with absolute URI gets changed to localhost

$refPointer = $site_to_be_extracted . $a->href;
echo $refPointer . '<br>';
generates in output (Chrome browser):
http://www.hackingwithphp.com/1/1/0/is-this-book-for-you
You would expect the following line to generate a correct <a> tag. That is a correct link
print '<a href=' . $refPointer . '>' . $a . '</a>';
Instead it generates in output:
localhost/1/1/0/is-this-book-for-you
So both the protocol (HTTP) and the $site_to_be_extracted (www.hackingwithphp.com) are lost. And replaced by "localhost"
Why? What could be the problem
This is the function where it's all happening. Now it works fine
include('simple_html_dom.php');
.
.
function createChapterContent ($site_to_be_extracted, $chapter_to_be_extracted) {
$html = file_get_html($chapter_to_be_extracted);
$refPointer;
foreach($html->find('ol') as $ol) {
foreach($ol->find('li') as $li) {
// echo '<br>' . $li->innertext ;
foreach($li->find('a') as $a) {
// Original. Causing the reported problem
// $refPointer = $site_to_be_extracted . $a->href;
// echo $refPointer . '<br>';
// changed to (see below). Now the output is correct
// Why?
$a->href = $site_to_be_extracted . $a->href;
$refPointer = $a->href;
print '<a href=' . $refPointer . '>' . $li->innertext . '</a>' . '<br>';
break;
};
}
}

Proximity search, Sending Email to a few receiver

I have emails and street addresses stored in a MySQL database. I want to use PHP to run a proximity/geospatial search and send an email to all the results within a given radius.
I can get the Email/Addresses in a foreach loop, but now I don't know how to send the email to all the results within the query result.
My code is like this:
foreach ($Ergebnis as $Eintrag) {
echo '<li><strong>' . htmlentities($Eintrag->Name) . '</strong><br />' . "\n";
echo htmlentities($Eintrag->Email) . '<br />' . "\n";
echo htmlentities($Eintrag->Strasse) . '<br />' . "\n";
echo htmlentities($Eintrag->PLZ) . ' ' . htmlentities($Eintrag->Ort) . '<br />' . "\n";
if (!empty($Eintrag->Website)) {
echo 'Website: ' . htmlentities($Eintrag->Website) . '<br />' . "\n";
}
Thank you very much!

limit results in foreach loop

The code below I have works fine but I wanted to check with you, the experts, to make sure I was using best practices.
I want to limit the loop results to 16. Does the code below seem like the best method?
Thanks,
Jeffrey
foreach ($flickr_set['items'] as $id => $photos) {
$ctr=0;
foreach ($photos as $photo) {
if($ctr>=16) break; else $ctr++; /* limits results to 16 */
echo '<a href="' . $photo['large'] . '" title="' . $photo['title'] . '" rel="flickr-set" ><img src="' . $photo['thumb'] . '" /></a>';
}
}
Your solution is fine, if you want a more structured solution which might be also more understandable, you can use array_slice:
foreach ($flickr_set['items'] as $id => $photos) {
foreach (array_slice($photos, 0, 16) as $photo) {
echo '<a href="' . $photo['large'] . '" title="' . $photo['title'] . '" rel="flickr-set" ><img src="' . $photo['thumb'] . '" /></a>';
}
}
Your code is fine but..
I woudn't check if it is higher or equal i would only check if it equal like this:
if($ctr == 16)

How do I add these php tags to the <ul> tag?

I am modifying a wordpress function. I have the following code:
$output .= $r['link_before'] . '<ul><li class="bbp-forum-info">' . $title . '</li>' . $counts . '<li class="bbp-forum-freshness">' . $subfresh . '</li></ul>' . $r['link_after'];
And I need the ul tag to look like this:
<ul id="bbp-forum-<?php bbp_forum_id(); ?>" <?php bbp_forum_class(); ?>>
However, I don't know how to add those php tags inside of that code. I have tried it in a number of different ways, but haven't been able to get it to work. Any advice?
use the . to concatenate strings:
$c='a'.'b';
echo $c //outputs 'ab'
knowing that, just:
$output .= $r['link_before'] . '<ul id="bbp-forum-'.bbp_forum_id().'" '.bbp_forum_class().'><li class="bbp-forum-info">' . $title . '</li>' . $counts . '<li class="bbp-forum-freshness">' . $subfresh . '</li></ul>' . $r['link_after'];
if you are adding it correctly
$output .= $r['link_before'] . '<ul id="bbp-forum-<?php bbp_forum_id(); ?>" <?php bbp_forum_class(); ?>><li class="bbp-forum-info">' . $title . '</li>' . $counts . '<li class="bbp-forum-freshness">' . $subfresh . '</li></ul>' . $r['link_after'];
This is hard to answer without further knowlege of the system.
ensure bbp_forum_class(); echoes something along the lines of class="foo"
if that isnt your issue, your style may be overridden by the li class. ensure you dont have conflicting css!

If $alt variable empty then fall back to $title in Wordpress custom function

I have constructed a custom gallery type as a new shortcode in Wordpress.
However I want to make it a little more SEO friendly so am trying to call the alt tag, and if the alt tag is not found replace it with the title.
So far I have the following (and it works fine) but I am stuck at trying to have $alt fallback to title if $alt is empty? Any ideas/help appreciated!
$bigimageUrl = wp_get_attachment_image_src($attachment_id,$size='full-size');
$littleimageUrl = wp_get_attachment_image_src($attachment_id,$size='thumbnail');
$title_raw = $attachment->post_title;
$title = preg_replace("/[^a-zA-Z0-9\s]/", " ", $title_raw);
$alt_raw = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
$alt = preg_replace("/[^a-zA-Z0-9\s]/", " ", $alt_raw);
echo '<img class="gallerytype" title="'; echo $title; echo '" alt="'; echo $alt; echo '" data-rsTmb="'; echo $littleimageUrl[0]; echo '" src="'; echo $bigimageUrl[0]; echo '"/> ';
Instead of using one echo after the other:
echo '<img class="gallerytype" title="';
echo $title;
echo '" alt="';
echo $alt; echo '" data-rsTmb="';
echo $littleimageUrl[0];
echo '" src="';
echo $bigimageUrl[0]; echo '"/> ';
You can use a comma (,) to separate expressions to be output:
echo '<img class="gallerytype" title="', $title, '" alt="', ..., '"/> ';
That should already help you to make the code a little mit more readable.
Then you want to set $alt to $title if it is empty. So do that before the output:
empty($alt) && $alt = $title;
Read as: $alt is empty and $alt is $title.
You could also write that with an if clause:
if (empty($alt)) $alt = $title;
That should do it for you.

Categories