echo image problems?

Discussion in 'PHP' started by izlik, Oct 20, 2013.

  1. #1
    Hello

    I want to echo an img with a href in php but i am having problem, anyone that can help me with the problem ? :/

    echo '<a href="mypage.com/index.php?dog=' . $id . '" target="_blank"><img src="/thumb/' . $id . '.jpg /></a>';
     
    izlik, Oct 20, 2013 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Well you forgot a double quote in the src attribute. Don't see anything else so far, but then again I'm not even sure what your problem is...
     
    nico_swd, Oct 20, 2013 IP
  3. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #3
    I am getting Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' on whatever i attept when using it :/
     
    izlik, Oct 20, 2013 IP
  4. monkeye

    monkeye Active Member

    Messages:
    307
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    68
    #4
    Does this work?

    echo '<a href="mypage.com/index.php?dog=' . $id . '" target="_blank"><img src="/thumb/' . $id . '.jpg" /></a>';

    Are you sure $id contains a number?
     
    monkeye, Oct 20, 2013 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    That line doesn't have any syntax errors (at least not PHP's syntax). Can you post some more code? Perhaps a few lines above, and a few lines below?
     
    nico_swd, Oct 20, 2013 IP
  6. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #6
    Thank you that worked like a charm!!! :D i have this in a loop now like the code bellow and i was wondering, can i do a pagenation somehow in this loopt? i have almost 1000 entries in my database and i just limit it to 5 for testing right now but would like tho pagenate it all if i remove the "limit" so to say :/

    <?php
    $getquery=mysql_query("SELECT * FROM dogs ORDER by ID LIMIT 5");
    while($rows=mysql_fetch_assoc($getquery))
    
    {
        $id=$rows['id'];
    
        echo'br';
        echo 'mypage.com/index.php?dog=' . $id . '" target="_blank"><img src="/thumb/' . $id . '.jpg" /></a>';
        echo '';
    }
    ?>
    PHP:
    [/quote]
     
    izlik, Oct 20, 2013 IP
  7. Gangsta

    Gangsta Active Member

    Messages:
    145
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #7
    <?php
    $page = (int)$_GET['page'];
    $perpage = 5;
    
    if ($page<1) $page = 0;
    
    $getquery=mysql_query("SELECT * FROM dogs ORDER by ID LIMIT ".($perpage*$page).",".$perpage);
    
    while($rows=mysql_fetch_assoc($getquery))
    
    {
        $id=$rows['id'];
        echo'br';
        echo 'mypage.com/index.php?dog=' . $id . '" target="_blank"><img src="/thumb/' . $id . '.jpg" /></a>';
        echo '';
    }
    
    $getquery=mysql_query("SELECT count(*) FROM dogs ORDER by ID");
    $total = mysql_result($getquery,0);
    
    echo "<div class='paging'>";
    for ( $n = 0; $n <= $total; $n++ ) {
        echo "<a href=\"?page=".$n."\">".$n."</a>";
    }
    echo "</div>";
    ?>
    Code (markup):
    something like this... havn't test it, jsut coded. ask questions if you need more help
     
    Gangsta, Oct 22, 2013 IP
  8. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #8
    Hey Gansta and thanks for looking at it, it tried running the code and get it to work but it just ends up like this along with the error "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource"
    https://www.dropbox.com/s/yi9dmbzq7isqrvv/pagen.png
     
    Last edited: Oct 22, 2013
    izlik, Oct 22, 2013 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #9
    Some extra advice:

    First up, STOP using string addition on ECHO when you don't have to. It's a hair slower and uses more memory since it has to be built and sent.

    Try this test to see what I mean. It'll leave you scratching your head until you figure out the difference.

    <?php
    function testWorld() { echo 'world ' };
    echo 'hello ',testWorld(), '<br />';
    echo 'hello ' . testWorld() . '<br />';
    ?>
    Code (markup):
    The output from that ends up:
    hello world
    world hello

    String addition is run before being sent to echo, comma delimits are sent separately one at a time. Splitting the formatting to multiple lines helps too in making it easier to check for errors, and of course this is 2013 not 1998, it's unlikely you're inside a frameset, so why are you using TARGET?

    echo '
    	<a href="mypage.com/index.php?dog=', $id, '">
    		<img src="/thumb/', $id, '.jpg" alt="DESCRIBE THIS!" />
    	</a>';
    Code (markup):
     
    deathshadow, Oct 22, 2013 IP
    bartolay13 likes this.
  10. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #10
    in your original post you never added the last double quotes after .jpg
    therefore your HTML tag was never. and ran straight through with code picking up characters and assuming a unexpected string or ',' etc.

    and monkeyeye accidently forgot them after
    dog=

    which by both accidental oversights is having you looking for ghost gremlins that never existed.
    you must close the double quotes...and add them everywhere they belong without becoming confused when single quotes end the echo line
    when ever double and single quotes end up in stack "'"'''''""" etc
    or when they start stop to fit $vars in is always where things sometimes go wrong.

    Try this one:
    echo '<a href="mypage.com/index.php?dog="' . $id . '" target="_blank"><img src="/thumb/' . $id . '.jpg"></a>';
     
    ezprint2008, Nov 1, 2013 IP