SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    HTML Code:
    <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:
    Code:
    <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
    Code:
    <meta property="og:image" content="http://mysite.com/upload/../upload/067d15bf9df056d7dc67f82bb573edf9.jpg" />
    Thank's in advance!

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    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.

  3. #3
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,387
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by slsv View Post
    So now I have title like this
    HTML Code:
    <title><?php echo $caption;?></title>
    But if there is no $caption in database I get the URL of page instead. How to check if there is no title to show something static 'No title' for example?
    You can use the ternary operator to check if $caption is empty and output a default title if so.

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

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    You can use the ternary operator to check if $caption is empty and output a default title if so.

    PHP Code:
    <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'...

  5. #5
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,805
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Try this instead:

    Code:
    <title><?php echo empty($caption) ? 'No $caption ' : $caption; ?></title>
    
    Off Topic:

    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.

  6. #6
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,387
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by slsv View Post
    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'...
    Oops, sorry, I got the order the wrong way round, it should be:

    PHP Code:
    <title><?php echo (empty($caption)) ? 'No title' $caption ?></title>
    Edit: John beat me to it

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, now is ok. Working good.
    Thank's!
    For my second question I still can't understand from where are this two dots..

  8. #8
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    How is $file_path derived? Where does it come from?
    Ian Anderson
    www.siteguru.co.uk

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru View Post
    How is $file_path derived? Where does it come from?
    from database ->
    Code:
    $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
    Code:
    ../upload/e7c6957b6d957fe50e8a52617c27d9ff.jpg
    In upload path to folder is like this
    Code:
    $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
    Code:
    <meta property="og:image" content="http://localhost<?php echo substr($file_path, 2);?>" />


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •