Displaying (if) link in a cell - php

The table cell in the below code is being used as part of a messaging system and it returns the url as a link to the attached file to the message.
echo '<td width="25%">'.wp_get_attachment_url($row->file_attached).'</td>';
This is fine and works well, my problem is that I would like it to either only show the title of the file ( not the full url)
or even better would be to have it print "file attached" as a link, but if there is no file to get could it say "no file attached"
I am a complete novice at this and I am trying to stuble my way through. It would be great if somebody could push me in the right direction.
Thanks

If you just mean you want the text of the link to be different, this is really basic HTML (not PHP):
some text for the link
In your case, you have the URL repeated as the contents of the <a> tag, but you can put whatever you want there.
Edit: To only show the link if there is a link to show, you need an if statement, probably the simplest piece of programming logic there is:
if ( $row->file_attached ) {
// echo a link
}
else {
// echo something other than a link
}

Try following. when attachement is found it displays your link, when its not found it just shows a message. It is not a good idea to have a link when no attachement is found, that will link to nowhere...
if(wp_get_attachment_url($row->file_attached))
echo '<td width="25%">'.sometext.'</td>';
else
echo '<td width="25%">no file attached</td>';

Related

Check wordpress post for specific HTML tag . Else show post thumbnail

I guess this question must have been asked here before, but i couldn't find the topic so bear with me.
I'm trying to achieve an if/else statement in PHP to find a specific HTML tag with a certain classname. If this HTML tag is found i want it to display the HTML tag otherwise if not found, i want it to show the post thumbnail.
All this needs to be done in a wordpress template (single.php).
So i have this:
if (strpos($post->post_content, ('img[.ngg_displayed_gallery]') === false)){
$gallery = false;
the_post_thumbnail('full');
echo do_shortcode('[shareaholic app="share_buttons" id="390155"]');
}
else{
echo ('img[.ngg_displayed_gallery]');
echo do_shortcode('[shareaholic app="share_buttons" id="390155"]');
}
I'm pretty sure i screwed this code up so please help me. I'm very bad with PHP ;)
Thanks for the help!
Edit:
Ok.. i'll try to be more clear:
When someone edits a wordpress post and adds a Nextgen Gallery in the content editor there is this HTML string:
<img class="ngg_displayed_gallery mceItem" src=“../nextgen-attach_to_post/preview/id--1443" alt="" data-mce-placeholder="1" />
So basically i want to code something like this: When this HTML IMG string appears in the content field, show that INSTEAD of the post thumbnail (featured image).. If that HTML IMG string is NOT in the content field, show the post thumbnail (featured image).
From what I understand from the question, you need to find a html tag in the contents of string, you are using strpos(). I want to let you know this is bad practice and alternative should be used instead, however, here is an example.
// First we must get the HTML formated content.
$content = apply_filters ("the_content", $post->post_content);
// We then must indentifie the needle in the heystack (content)
$needle = 'ngg_displayed_gallery';
if (strpos($content, $needle) === false)){
$gallery = false;
// No, there's no gallery present
echo "NO, gallery not found";
} else {
// Yes there is an gallery present
echo "YES, gallery found";
}
Q1. Why is this bad practice?
We can find the id of gallery/image reliability, as each <img class='ngg_displayed_gallery'..> is different in src, id and possible class

Display an img inside a php echo string

I am fairly new to the code world and am trying to display an image inside of an echo string.
Here is the code
<?php
echo String::insert(
__("Are you owner of this listing? :claim_link"),
array('claim_link'=>$html->link(
__("CLAIM IT NOW"),
array("controller"=>"listing_claims",
"action"=>"claim",$listingData['Listing']['id']
)
)
))
?></div>
The code works fine, however, I want to replace ... (__("CLAIM IT NOW") ... with an image instead of just text...something like 'img src='my-image.png'. The goal is to get a button to appear instead of text.
so far, when I make the change, I see whatever text I have typed in.
Any help is appreciated.
Replace:
"CLAIM IT NOW"
with:
"<img src='yourimage.png' alt='Image'>"
If you are using CakePHP you should do something like:
$html->link($html->image('yourimage.png', ...

Getting information from ID's

First off, I'm brand new to PHP so I'm sorry if this is a stupid question, second of all sorry if this title is incorrect.
Now, what I'm trying to do is create an overlay for a game that I play. My code for the overlay works perfectly, and now I'm working on my HTML file which gets its information from a website and outputs it. The code on the website looks like this:
<span id="example1">Information I want</span>
<span id="example2">More Info I want</span>
...
<span id="example3">And some more</span>
Now what I want to do is create a PHP script which goes in and finds elements by their names and gives me the information in those span tags. Here's what I've tried so far, it's not working however (no surprise):
//Some HTML here
<?php
$doc = new DomDocument;
$doc->validateOnParse = true;
$doc->Load('www.website.com');
echo "Example1: " . $doc->getElementById('example1') . "\n";
?>
//More HTML
To be honest, I have no clue what I'm doing. If anyone could show me an example of how to do this properly, or to point me in the right direction I would appreciate it.
The text between open and close tags is a Text Node.
Just write $doc->getElementById('example1')->nodeValue
Your code seems along the right lines, but you're missing a few things.
First of all, your load call is literally looking for a file named "www.website.com". If it's a remote file, you must include the http:// prefix.
Then, you are attempting to echo out the node itself, whereas you want its value (ie. its contents).
Try $doc->getElementById("example1")->nodeValue instead.
That should do it. You may want to add libxml_use_internal_errors(true); so that any errors in the source file won't destroy your page with PHP errors. Also, I would suggest using loadHTMLFile instead of load, as this will be more lenient towards malformed documents.
you can use getElementById:
$a = $doc->getElementById("example1");
var_dump($a); so you will see what you want to echo or put, or something.
You can also make all the names i HTML as example[] end then foreach the example array, so you can get element by id from example array with just one row of code

Insert a div + link + image inside an echo (Joomla)

I'm having a little problem here and I would much appreciate your help.
I'm trying to create a sort of menu which will be displayed after a user logs in in Joomla (the fact that is joomla doesn't really matter though). I have two different outputs for two different users. If the user is User A, s/he will see output A, if it's user B, s/he will see output B.
I have already figured out how to do the "Look who's the user---give output" part. And it's working perfectly. The problem is the output itself. I'm trying to echo a div which contains an image and a text, both as a link. However, the only output I'm getting is the text, whith no format whatsoever.
Here it's the code:
<?php
$user =& JFactory::getUser();
if ($user->id == 291) {
echo <<<EOS
<div class="mitribu"><img class="mitribuimg" src="images/banners/books.png" alt="books"/>Mi tribu</div>
EOS;
}
?>
As you see this should render an image and the text Mi tribu, and that should be a link. But the only thing I'm getting is Mi tribu in plain text.
What Am I doing wrong here?
Thank you very much in advance!
Hernan.
EDIT: I have found the answer. It was the plugin used to render PHP inside articles and modules in Joomla, Sourcerer, which was stripping the code. Just in case someone else has the same problem, here it's the solution:
You must use {source 0} {/source} to add the code. Adding the 0 tells the plugin to do not strip the HTML code.
Have you tried opening it in Google Chrome? Then rightclick where the image should be (so on the text) and choose "inspect element" from the menu.
This will bring up the debugging tools from Google Chrome, and will allow you to see what's been output to the browser, and might show you why your image is not visible (wrong path or something alike).
You can execute HTML inside of php code easily like this below. Instead of using { } just use the colon and output the html below.
$user =& JFactory::getUser();
if ($user->id == 291):
?>
<div class="mitribu"><img class="mitribuimg" src="images/banners/books.png" alt="books"/>Mi tribu</div>
<?php endif;?>
or do this, make sure to use the ' because you are using " in your HTML.
<?php
$user =& JFactory::getUser();
if ($user->id == 291) {
echo '<div class="mitribu"><img class="mitribuimg" src="images/banners/books.png" alt="books"/>Mi tribu</div>'
}
?>

link with same url in php

I have some thumbnail images with its larger version.I placed the thumbnail images in a page.Now for link I just gave a link
<img src="thumbnail1.jpg>
but for this I have to make different pages for showing larger one.I want to give a link to show them in a single page.means whenever I will click the thumbnail it will open the larger one in a page with the same url but with its name like
imagegallery.php?news=images/largerimage1/13.jpg
imagegallery.php?news=images/largerimage1/14.jpg
so how to do that?
Pretty basic stuff, I suggest you get to read some PHP tutorials on the internet to get some knowledge on one thing and another.
The ?news= part in your URL is a parameter that can be read by PHP. This type is known as $_GET. To get this part you would need $_GET['news'] so if we'd use your first link and place this inside a script: echo $_GET['news']; the page would say images/largerimages1/13.jpg.
In order to get the image loaded on your website we need some simple steps, I'm changing the news parameter into image, that suits better for your script since it ain't news items:
<?php
// Define the path (used to see if an image exists)
$path = 'your/absolute/path/to/public_html/'; # or wwwroot or www folder
// First check if the parameter is not empty
if($_GET['image'] != "") {
// Then check if the file is valid
if(file_exists($path . $_GET['image'])) {
// If an image exists then display image
echo '<img src="'. $_GET['image'] . '" />;
}
}
?>
Below this script you can put all your thumbnails the way you want. Ofcourse, also for these thumbnails there are some automated options. But I strongly suggest you get a good look at the script above and some beginner PHP tutorials so you completely understand the example given. This still isn't the best method, but it's kicking you in the right direction.
if your imagegallery.php is in root of your domain, you can just add slash as a first char to links like this:
<img src="thumbnail1.jpg>
else you will have to write some php function which it returns BaseUrl of your web. Then it should looks like this:
<img src="thumbnail1.jpg>
maybe you can something like this,
Techincally, there is no thumbnail image, just a stretch version of the regular image
I don't understand which part you don't know how to do:
- the link part?
it should look like
<img src="thumbnail1.jpg>
- or the PHP part (the file called imagegallery.php)?

Categories