show image in Smarty

Discussion in 'PHP' started by piropeator, Sep 3, 2009.

  1. #1
    In mi PHP file I have:

    <?php
    define('APP_BASEDIR', dirname(__FILE__));
    define('HTTP_SERVER', 'http://localhost');

    define('DIR_IMAGES', 'imagen/logos/');

    // echo "<img src='" . DIR_IMAGES . "logo.jpg' />"; this works but now I want to use Smarty template

    ?>

    But now I want to show my image using my Smarty template using my DIR_IMAGES variable.

    show.tpl.php
    =========
    <table width="100%" border="1" cellspacing="0" bordercolor="#000066">
    <tr>
    <td width="50">

    echo "<img src='" . DIR_IMAGES . "logo.jpg' />"; <<--- Here is my problem.

    </td>
    </tr>
    </table>

    What is my mistake??
    Thanks.
     
    Last edited: Sep 3, 2009
    piropeator, Sep 3, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Doesn't look like a mistake to me, what HTML output do you get?
     
    premiumscripts, Sep 3, 2009 IP
  3. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #3
    Does not show my imagen :confused:
     
    piropeator, Sep 3, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Show the generated HTML source code. And check to make sure that the image is at that location.
     
    premiumscripts, Sep 3, 2009 IP
  5. Dollar

    Dollar Active Member

    Messages:
    2,598
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    90
    #5
    Your sending your images as
    "<img src='"/home/user/public_html/images/something.jpg"/>
    That not not going to work!
    It has to be "http://example.com/images/something.jpg"


    If your looking for hardlinking images in an automated fashion put in your whatever.php file.
    
    define('DIR_IMAGES',$_SERVER['HTTP_HOST'] . '/imagen/logos/');
    $smarty->assign('DIR_IMAGES', DIR_IMAGES);
    
    
    PHP:
    Then in the whatever.tpl use

    <img src="{$DIR_IMAGE}something.jpg" />
    HTML:
    Or better yet just use relative urls starting with /
    <img src="/imagen/someting.jpg" />
    HTML:
     
    Last edited: Sep 4, 2009
    Dollar, Sep 4, 2009 IP
  6. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #6
    What mean

    $_SERVER['HTTP_HOST'] ??

    I defined this ?
     
    piropeator, Sep 4, 2009 IP
  7. Dollar

    Dollar Active Member

    Messages:
    2,598
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    90
    #7
    $_SERVER['HTTP_HOST'] returns the value in Host: from the header of the current request. So if your site is http://example.com.
    $_SERVER['HTTP_HOST'] will be "http://example.com".


    So
    define('DIR_IMAGES',$_SERVER['HTTP_HOST'] . '/imagen/logos/');
    =
    define('DIR_IMAGES','http://example.com' . '/imagen/logos/');

    $_SERVER PHP.net Manual :)
     
    Dollar, Sep 4, 2009 IP
  8. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #8
    This is all my program.

    conf.php
    ======
    <?php
    define("APP_BASEDIR", dirname(__FILE__));
    define('SMARTY_BASEFILE', APP_BASEDIR . '/lib/Smarty-2.6.14/Smarty.class.php');
    define('ADODB_BASEFILE', APP_BASEDIR . '/lib/adodb-492/adodb.inc.php');
    define('DIR_IMAGES', '/imagen/logos/');

    ini_set('session.gc_maxlifetime','1800'); // 30 minutes
    ini_set('error_reporting', 'E_ALL');

    require_once APP_BASEDIR . "/clases/util/Plantilla.class.php";

    session_start();
    ?>

    index.php
    =======
    <?php
    require_once "./conf.php";

    $tpl = new Plantilla();
    $tpl->assign('DIR_IMAGES', DIR_IMAGES);
    $tpl->display('portada.tpl.php');
    ?>

    portada.tpl
    ========
    {include file="header.tpl.php"}
    <table width="100%" border="1" cellspacing="1">
    <tr>
    <td colspan="2"><div align="center">MAIN MENU</div></td>
    </tr>
    <tr>
    <td width="50%"><a href="admin/proceso1.php">Process 1 </a></td>
    <td width="50%">&nbsp;</td>
    </tr>
    <tr>
    <td width="50%"><a href="admin/proceso2.php">Process 2 </a></td>
    <td>&nbsp;</td>
    </tr>
    </table>
    {include file="footer.tpl.php"}

    header.tpl.php
    ===========
    <table width="100%" border="1" cellspacing="0" bordercolor="#000066">
    <tr>
    <td width="50">
    <img src="{$DIR_IMAGES}logo.jpg"/>
    </td>
    <td width="52%">Something</td>
    </tr>
    </table>

    proceso1.tpl.php
    =============
    {include file="header.tpl.php"}
    <table width="100%" border="1" cellspacing="1">
    <tr>
    <td colspan="2"><div align="center">PROCESS 1 </div></td>
    </tr>
    <tr>
    <td width="50%">&nbsp;</td>
    <td width="50%">&nbsp;</td>
    </tr>
    <tr>
    <td colspan="2"><div align="center"><a href="../index.php">Return</a></div></td>
    </tr>
    </table>
    {include file="footer.tpl.php"}

    Root
    ====
    conf.php
    index.php
    \imagen\logos\logo.jpg
    \plantillas\templates\header.tpl.php
    \plantillas\templates\portada.tpl.php
    \plantillas\templates\footer.tpl.php
    \plantillas\templates\proceso1\proceso1.tpl.php
    \plantillas\templates\proceso2\proceso2.tpl.php

    But de image does not show. :confused:
     
    piropeator, Sep 7, 2009 IP
  9. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #9
    You still haven't shown what I asked you 4 days ago, the generated source code. (When you go in your browser > view source..)

    You should also read the sticky in this forum and learn how to paste code correctly.
     
    premiumscripts, Sep 7, 2009 IP
  10. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #10
    OOPSS!!
    I'm sorry.... this is the generated source code in IE, when run Index.php.

    <table width="100%" border="1" cellspacing="0" bordercolor="#000066">
      <tr>
    	<td width="50">
    	<img src="/imagen/logos/logo.jpg"/>
    	</td>
        <td width="52%">Something</td>
      </tr>
    </table><table width="100%" border="1" cellspacing="1">
      <tr>
        <td colspan="2"><div align="center">MAIN MENU</div></td>
      </tr>
      <tr>
        <td width="50%"><a href="admin/proceso1.php">Process 1 </a></td>
        <td width="50%">&nbsp;</td>
      </tr>
      <tr>
        <td width="50%"><a href="admin/proceso2.php">Process 2 </a></td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <table width="100%" border="1" cellspacing="0" bordercolor="#999999">
      <tr>
        <td bordercolor="#333333"><p align="center">My business.com</p>
        <p align="center">2009</p></td>
      </tr>
    </table>
    PHP:
    ..... and...... this is the generated source code in IE, when I choose Proccess 1 option:

    <table width="100%" border="1" cellspacing="0" bordercolor="#000066">
      <tr>
    	<td width="50">
    	<img src="logo.jpg"/>
    	</td>
        <td width="52%">Something</td>
      </tr>
    .... etc.
    PHP:
     
    piropeator, Sep 7, 2009 IP
  11. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #11
    When I invoke my image with this path from a sub folder does not show the image.
    :confused:
     
    piropeator, Sep 10, 2009 IP
  12. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Just do:

    <img src="/imagen/logos/logo.jpg"/>
    HTML:
     
    premiumscripts, Sep 10, 2009 IP
  13. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #13
    hamidof, Sep 10, 2009 IP
  14. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #14
    This works when invoked from any sub folder?
    :confused:
     
    piropeator, Sep 11, 2009 IP