How can i display image from a link instead of link?

Discussion in 'PHP' started by baris22, Apr 9, 2008.

  1. #1
    Hello,

    I have got links field in the database. The entries are like this:

    
    
    http://webpicshot.com/images/0vhoxwbxxoaicir13la.jpg;http://webpicshot.com/images/i2qf04wgmwstdukqpdt.jpg;http://rapidshare.com/files/106224452/TenaciousDFhergently.avi ;
    
    
    PHP:
    I have got image link urls and normal urls. Every url is seperated with ";"

    What can I do to show the image for the image url instead of the url?

    I want to display image instead of the url.

    I just use
    $row['links']
    PHP:
    to show the links.


    Thanks
     
    baris22, Apr 9, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You need to split up the url's and then display the images.

    Something like:

    
    
    $imageArray = array('jpg','gif','png');
    
    $urls  = explode(';',$row['links']);
    
    foreach($urls as $url)
    {
    
    if(in_array(substr($url,3),$imageArray))
    {
    
    echo '<img src="'.$url.'" />';
    
    }
    
    }
    
    
    PHP:
     
    jestep, Apr 9, 2008 IP
    baris22 likes this.
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thank you for your reply. I tried your code but it did not work. It is just saying Array instead of image or links.


    Thanks

     
    baris22, Apr 9, 2008 IP
  4. kalisthegreat

    kalisthegreat Peon

    Messages:
    132
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    if(in_array(substr($url,3),$imageArray))

    for the line above replace the number 3 with minus 3 (-3)
    tell me what happens
     
    kalisthegreat, Apr 9, 2008 IP
  5. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    
    
    $imageArray = array('jpg','gif','png');
    $urls  = explode(';',$row['links']);
    foreach($urls as $url)
    {
    if(in_array(substr($url,(-3)),$imageArray)){
    echo '<img src="'.$url.'" />';
    }
    }
    
    
    PHP:
    This worked kind of. The problem is if there is normal link other than image in the field , it does not display.

    Any idea how can i show normal links as well as image links?


    Thanks

     
    baris22, Apr 10, 2008 IP
  6. kalisthegreat

    kalisthegreat Peon

    Messages:
    132
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ok add the following lines to the code

    if(in_array(substr($url,(-3)),$imageArray)){
    echo '<img src="'.$url.'" />';
    }
    else
    {

    echo '<a href='.$url.'>'.$url.'</a>';

    }

    tell me what happens
     
    kalisthegreat, Apr 10, 2008 IP
    baris22 likes this.
  7. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Thank you, it worked.

     
    baris22, Apr 10, 2008 IP
  8. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #8
    Last one more question. What about if i want to show the first image and rest of the urls as links. Say there are 4 urls. First 2 are images and last 2 are links.

    I want to show first image url as image and other 3 urls as url.

    this is what i have got now.

    
    $imageArray = array('jpg', 'gif', 'png'); 
    
    $urls  = explode(';', $row['links']); 
    
    foreach ($urls AS $url) 
    { 
        if (in_array(substr($url, -3), $imageArray)) 
        { 
            echo "<img src=\"$url\" /><br /><br />"; 
        } 
    else
    {
    
    echo '<a href='.$url.'>'.$url.'</a><br />';
    
    }
    }
    
    PHP:
     
    baris22, Apr 11, 2008 IP
  9. kalisthegreat

    kalisthegreat Peon

    Messages:
    132
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    ok try this code

    $counter = 0;

    foreach($urls as $url)
    {

    if(in_array(substr($url,-3),$imageArray))
    {
    if($counter==0)
    {
    echo "<img src=\"$url\" /><br /><br />";
    $counter++;

    }
    else
    {

    echo '<a href='.$url.'>'.$url.'</a><br/><br/>';

    }
    }
     
    kalisthegreat, Apr 11, 2008 IP
  10. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #10
    Thank you very much. it worked, this code does not make my site run slow, does it?

    Thanks



    
    $imageArray = array('jpg', 'gif', 'png'); 
    $urls  = explode(';', $row['links']); 
    $counter = 0;
    foreach ($urls AS $url) 
     {     
     if (in_array(substr($url, -3), $imageArray))     
     {
     if($counter==0)
     {
     echo "<img src=\"$url\" /><br /><br />";     
     }
     $counter++;
     }
     else
     {
     echo '<a href='.$url.'>'.$url.'</a><br />';
     }
     }
    
    PHP:


     
    baris22, Apr 11, 2008 IP