If there is no 'title' show something

Hello,
It’s me again and need help with this one. I’m sure is not something hard but can’t do it alone. So now I have title like this

<title><?php echo $caption;?></title>

But if there is no $caption in database I get the URL of page instead. How to chek if there is no title to show something static ‘No title’ for example?

Also I have something strange in output of facebook meta tags. This is how I get image path:

<meta property="og:image" content="http://mysite.com/upload/<?php echo $file_path;?>" />

And in output I get two dots ‘…’ I don’t know from where

<meta property="og:image" content="http://mysite.com/upload/../upload/067d15bf9df056d7dc67f82bb573edf9.jpg" />

Thank’s in advance!

The second problem: it seems that $file_path contains a relative path.

For both problems: are you using a template system? You’ll have to find the part of the script that actually puts the values in $caption and $file_path and operate on that code.

You can use the ternary operator to check if $caption is empty and output a default title if so.

<title><?php echo (empty($caption)) ? $caption : 'No title' ?></title>

Yes, I was looking for something like this. But this doesn’t work. If there is no title there is url… instead of ‘No title’…

Try this instead:



[COLOR=#000000][FONT=monospace]<title>[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]<?php [/FONT][/COLOR][COLOR=#007700][FONT=monospace]echo empty([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]$caption[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) ? [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]'No $caption ' : [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]$caption; ?>[/FONT][/COLOR][COLOR=#000000][FONT=monospace]</title>
[/FONT][/COLOR]

[ot]I always used isset() but looks as though empty() is better because not only does it return FALSE for a variable not set it also caters for empty variables.
[/ot]

Oops, sorry, I got the order the wrong way round, it should be:

<title><?php echo (empty($caption)) ? 'No title' : $caption ?></title>

Edit: John beat me to it :wink:

Yes, now is ok. Working good.
Thank’s!
For my second question I still can’t understand from where are this two dots…

How is $file_path derived? Where does it come from?

from database ->

$sql = "SELECT name, caption, file_path FROM images order by id desc LIMIT $offset, $rowsperpage";

When I upload image in DB is stored like this

../upload/e7c6957b6d957fe50e8a52617c27d9ff.jpg

In upload path to folder is like this

$path = "../upload/" . $myFile;

So from here is come… but if remove them from upload form it doesn’t work

edit:
I’ve made it like this and seems to me is working

<meta property="og:image" content="http://localhost<?php echo substr($file_path, 2);?>" />