Get partial filename from server

Discussion in 'PHP' started by MrConn, Jun 15, 2012.

  1. #1
    Hi!
    I currently have this script:
    
    <?php
    
    $sti = "/home/t/ts/xxx/xxx.xx/html/xxx/produkt_bilder/";
    $url = "http://www.xxx.xx/xxx/produkt_bilder/";
    ?>
    
    
    <div class="box">
    
    <?php 
    
    $prodid = $this->getRequest()->getParam('id');
    $catid = $this->getRequest()->getParam('category');
    
    if($catid == "") {
    $catfil = "c_$prodid.jpg";
    $prodfil = "";
    
    $catfil2 = "c2_$prodid.jpg";
    $prodfil2 = "";
    
    }else{
    $prodfil = "p_$prodid.jpg";
    $catfil = "c_$catid.jpg";
    
    $prodfil2 = "p2_$prodid.jpg";
    $catfil2 = "c2_$catid.jpg";
    
    }
    
    if ((file_exists("$sti$prodfil") && ($prodfil))) {
    print "<img src=\"$url$prodfil\" alt\"teaser\">";
    }elseif((file_exists("$sti$catfil") && ($catfil))) {
    print "<img src=\"$url$catfil\" alt\"teaser\">";
    }else{
    }
    
    if ((file_exists("$sti$prodfil2") && ($prodfil2))) {
        print "<br><img src=\"$url$prodfil2\" alt\"teaser\">";
    }elseif((file_exists("$sti$catfil2") && ($catfil2))) {
        print "<br><img src=\"$url$catfil2\" alt\"teaser\">";
    }else{
    }
    ?>
    
    
    </div>
    
    PHP:
    Is it possible to add a extra variable to the filename, and exctract that in some way?
    What I want to do is to have a imagefile called ie: c_1_info.jpg, and then use the part "info" to make a link like this:

    
    <?php
    
    $sti = "/home/t/ts/xxx/xxx.xx/html/xxx/produkt_bilder/";
    $url = "http://www.xxx.xx/xxx/produkt_bilder/";
    ?>
    
    
    <div class="box">
    
    <?php 
    
    $prodid = $this->getRequest()->getParam('id');
    $catid = $this->getRequest()->getParam('category');
    
    if($catid == "") {
    $catfil = "c_$prodid_$link.jpg";
    $prodfil = "";
    
    $catfil2 = "c2_$prodid_$link.jpg";
    $prodfil2 = "";
    
    }else{
    $prodfil = "p_$prodid_$link.jpg";
    $catfil = "c_$catid_$link.jpg";
    
    $prodfil2 = "p2_$prodid_$link.jpg";
    $catfil2 = "c2_$catid_$link.jpg";
    
    }
    
    if ((file_exists("$sti$prodfil") && ($prodfil))) {
    print "<a href=\"$link.html\"><img src=\"$url$prodfil\" alt\"teaser\"></a>";
    }elseif((file_exists("$sti$catfil") && ($catfil))) {
    print "<a href=\"$link.html\"><img src=\"$url$catfil\" alt\"teaser\"></a>";
    }else{
    }
    
    if ((file_exists("$sti$prodfil2") && ($prodfil2))) {
        print "<br><a href=\"$link.html\"><img src=\"$url$prodfil2\" alt\"teaser\"></a>";
    }elseif((file_exists("$sti$catfil2") && ($catfil2))) {
        print "<br><a href=\"$link.html\"><img src=\"$url$catfil2\" alt\"teaser\"></a>";
    }else{
    }
    ?>
    </div>
    
    PHP:
    Is this possible in any way?
     
    MrConn, Jun 15, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    Maybe. Let's see some code. You don't have anything resembling "info" in the code you show.
     
    Rukbat, Jun 15, 2012 IP
  3. MrConn

    MrConn Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thats the point! I have no idea how to do it! :-S
    At the monment a imagefiles is called c_1.jpg. This file will be shown if category = 1.
    What I want to do is call the file c_1_info.jpg, let the file be shown if category = 1, and link to info.html!
    And ofcourse if I call the file c_1_asdfgh.jpg the link will be to asdfgh.html
     
    MrConn, Jun 15, 2012 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    And we have no idea what you want to do.
    I still really have no idea what you want to do. I can write code to do what I want it to do, I can't write code to do something I can't understand.

    If all you want is to link to a different image file depending on the category, use a switch statement on the category to set a variable with the filename, then use that variable to create the link.
     
    Rukbat, Jun 15, 2012 IP
  5. MrConn

    MrConn Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok...
    In my imagefolder I upload a imagefile called "c_1.jpg". When I navigate on my website (webshop) to the category with id "1" this image will be shown. (If I call the image "c_2.jpg" the image will be shown on a page with category 2.)
    What I want to do is to call my files c_1_info.jpg. Then, when I navigate to a page with category id "1" the image will be shown with a link to info.html.
     
    MrConn, Jun 15, 2012 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    You want the link on the image (click on the image itself) to change depending on the category? Or you want the link on the page (the <a href=...>) to change?
     
    Rukbat, Jun 16, 2012 IP
  7. MrConn

    MrConn Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No, I dont want it to change depending on category. I want it to change depending on filename.
    If the filename is c_1_info.jpg the image should be shown on pages where category id = 1, and link to the page info.html. If the filename is c_1_about.jpg the image should be shown on pages where category id = 1, and link to the page about.html.
     
    MrConn, Jun 16, 2012 IP
  8. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #8
    Then on each page:
    
    if($filename == 'c_1_info.jpg')
    {
        $link = 'info';
    } else {
        $link = 'about';
    }
    echo '<a href="'.$link.'.html"';
    
    PHP:
    That'll get you the beginning of the link. Add the rest of it in the echo statement.
     
    Rukbat, Jun 16, 2012 IP