Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    echoing html code- Help. please

    Ok, so I have the following code

    Code:
    <?php
    $left="http://www.runeescape.net/theaddiction/left.gif";
    $right="http://www.runeescape.net/theaddiction/right.gif";
    $link="/theaddiction/";
    echo "<img src=".$link.$z.".jpg>";
    echo "<a href=index.php?page=".$prev."><img src=".$left." align=left></a>"."<a href=index.php?page=".$next."><img src=".$right." align=right></a>";
    ?>
    Which, for the time being, let's assume that $next=2 and $prev=0

    So the portion
    Code:
    echo "<a href=index.php?page=".$prev."><img src=".$left." align=left></a>"."<a href=index.php?page=".$next."><img src=".$right." align=right></a>";
    outputs
    <a href=index.php?page=0><img src=http://www.runeescape.net/theaddiction/left.gif align=left></a><a href=index.php?page=2><img src=http://www.runeescape.net/theaddiction/right.gif align=right></a>
    however, I want it to say
    <a href="index.php?page=0"><img src="http://www.runeescape.net/theaddiction/left.gif" align=left></a><a href="index.php?page=2"><img src="http://www.runeescape.net/theaddiction/right.gif align=right"></a>
    So, obviously, the problem is that it won't be valid html code without the "" surrounding the url's. How can I enter the " in since they indicate a closing portion of the string?

  • #2
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    \"
    Should work
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #3
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You have 3 main options for mixing HTML and PHP

    Escape slashes
    PHP Code:
    $str "<a href=\"page.php?id=$myvar\">Page</a>"
    Use single quotes and break of the string
    PHP Code:
    $str '<a href="page.php?id=' $myvar '">Page</a>'
    Break in and out of HTML
    PHP Code:
    <a href="page.php?id=<?php echo $myvar?>">page</a>
    If you have a lot of HTML then this is a good option.

  • #4
    New Coder
    Join Date
    Feb 2006
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by degsy
    You have 3 main options for mixing HTML and PHP

    Escape slashes
    PHP Code:
    $str "<a href=\"page.php?id=$myvar\">Page</a>"
    Use single quotes and break of the string
    PHP Code:
    $str '<a href="page.php?id=' $myvar '">Page</a>'
    Break in and out of HTML
    PHP Code:
    <a href="page.php?id=<?php echo $myvar?>">page</a>
    If you have a lot of HTML then this is a good option.
    A cool. I didn't know you could just pop php in the html code like that. I like option 3. THanks.

  • #5
    Regular Coder
    Join Date
    Feb 2006
    Location
    The Netherlands
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can also use the heredoc syntax, meaning that you say
    PHP Code:
    echo <<<SOMENAME
    //Things you want to echo
    SOMENAME; // No whitespace before this 
    Just as with double quotes, variables are converted. However, you do not need to escape the double quotes. Though the third way degsy describes is faster, you probably want to know this as you might come across this in code examples or something
    Yeah that.

  • #6
    New Coder
    Join Date
    Feb 2006
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Vin0rz
    You can also use the heredoc syntax, meaning that you say
    PHP Code:
    echo <<<SOMENAME
    //Things you want to echo
    SOMENAME; // No whitespace before this 
    Just as with double quotes, variables are converted. However, you do not need to escape the double quotes. Though the third way degsy describes is faster, you probably want to know this as you might come across this in code examples or something
    not sure I understand what you are saying, but I have a new question now.

    If I want to do the code

    while (condition)
    {
    do this stuff
    }

    but I want it to be while ((NOT(condition))

    What do I type in?

    the condition in question is file_exists so it will return a boolean

  • #7
    Regular Coder
    Join Date
    Feb 2006
    Location
    The Netherlands
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    More info about heredoc

    You just place an exclamation mark (!) in front of it, so if(!file_exists(...))
    Yeah that.

  • #8
    New Coder
    Join Date
    Feb 2006
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Vin0rz
    More info about heredoc

    You just place an exclamation mark (!) in front of it, so if(!file_exists(...))

    I'm having problems getting file exist to work.. What's wrong with this code?

    assume that $test does, in fact = 0
    $prevchap = 4
    $chap = 5
    $sec =1

    I know the above as I echoed them to the screen. What I can't figure it is why it's not running the while loop...

    Code:
    if ($test==0)
     {
     $prevchap=$chap-1;
     $prevsec=0;
     do
     {
      $prevsec=$prevsec+1;
      echo "prevchap: ".$prevchap." - prevsec: ".$prevsec."<br>";
      echo "http://www.runeescape.net/theaddiction/chapters/".$prevchap."-".$prevsec.".txt<br>";
     }
     while (file_exists("http://www.runeescape.net/theaddiction/chapters/".$prevchap."-".$prevsec.".txt"));
     $prevsec=$prevsec+1;
     }
    else
     {
     $prevsec=$sec-1;
     $prevchap=$chap;
     }
    This is the output on the screen
    prevchap: 4 - prevsec: 1
    http://www.runeescape.net/theaddiction/chapters/4-1.txt
    next file=-2.txt

  • #9
    New Coder
    Join Date
    Feb 2006
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, i've simplified it and still can't figure it out.

    Code:
      $filename="http://www.runeescape.net/theaddiction/chapters/".$prevchap."-".$prevsec.".txt";
      echo $filename."<br>";
      if (file_exists($filename))
       {
       echo "i'm here<br>";
       }
      else
       {
       echo "not there<br>";
       }
    and it's telling me that it's "not there"

    despite the fact that "http://www.runeescape.net/theaddiction/chapters/4-1.txt" is most definetly there (type it into your browser)

  • #10
    Regular Coder
    Join Date
    Feb 2006
    Location
    The Netherlands
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Keyser520
    ok, i've simplified it and still can't figure it out.

    Code:
      $filename="http://www.runeescape.net/theaddiction/chapters/".$prevchap."-".$prevsec.".txt";
      echo $filename."<br>";
      if (file_exists($filename))
       {
       echo "i'm here<br>";
       }
      else
       {
       echo "not there<br>";
       }
    and it's telling me that it's "not there"

    despite the fact that "http://www.runeescape.net/theaddiction/chapters/4-1.txt" is most definetly there (type it into your browser)
    Well, with double-quotes you don't need to escape the string, so either you do
    PHP Code:
    $filename="http://www.runeescape.net/theaddiction/chapters/{$prevchap}-{$prevsec}.txt";
      echo 
    "$filename <br>";
      if (
    file_exists($filename))
       {
       echo 
    "i'm here<br>";
       }
      else
       {
       echo 
    "not there<br>";
       } 
    Or the faster way:
    PHP Code:
    $filename='http://www.runeescape.net/theaddiction/chapters' $prevchap '-' $prevsec '.txt';
      echo 
    $filename '<br>';
      if (
    file_exists($filename))
       {
       echo 
    'i\'m here<br>';
       }
      else
       {
       echo 
    'not there<br>';
       } 
    Don't know if the problem lies there, but try this first.
    Yeah that.

  • #11
    New Coder
    Join Date
    Feb 2006
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, why is this not working?

    Code:
      if (file_exists("http://www.runeescape.net/theaddiction/chapters/4-1.txt"))
       {
       echo "i'm here<br>";
       }
      else
       {
       echo "not there<br>";
       }
    It's returning "not there" but the dang file exists :-(

  • #12
    Regular Coder
    Join Date
    Feb 2006
    Location
    The Netherlands
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have no idea, actually... Sorry that I couldn't help but I wouldn't want you waiting for an answer not to come
    Yeah that.

  • #13
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    file_exists may be local only.

    Try this
    PHP Code:
    <?php
    $url 
    "http://www.runeescape.net/theaddiction/chapters/4-1.txt";

    if (@
    fopen($url"r")) {

     echo 
    "File Exists";

    } else {

     echo 
    "Can't Connect to File";

    }
    ?>


  •  

    Posting Permissions

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