0

This is my line of code in php which shows the page numbers, which are automatically generated based on the content in the table. Since all the page numbers have the same color I want to display the current active page, I wanted to do it with a condition in the class but due to the anchor tag already being in the "" of echo, and the class starting with ', once i put the ' for condition, the class is ended sooner and the condition is not made.
Is there a way to bypass this or use something else for inside of the ' '? Or is there another way to show the current active link?

   echo "<a class='{% if page.url. =='?page=$x' %}active{% endif %}' href='?page=$x'> $x </a>";

And if anyone needs some more code, this is the whole php code for printing all the pages based on the number of items in the table


for($x = 1; $x <= $totalPages + 1; $x++){
    ?>
        <div class='page'>
        <?php
        echo "<a class='{% if page.url. =='?page=$x' %}{% endif %}' href='?page=$x'> $x </a>";
        ?>
        </div>

    <?php
}
Stephen P
  • 14,422
  • 2
  • 43
  • 67
  • This looks like a typo: `page.url. ==` -- however, you can escape the quote, like `echo " $x ";` – Kinglish Dec 01 '21 at 19:20
  • 1
    Please research your inquiry before posting here in accordance with [ask]. This is a duplicate of [Escaping quotation marks in PHP](https://stackoverflow.com/questions/7999148/escaping-quotation-marks-in-php) – esqew Dec 01 '21 at 19:31
  • Another thing, what is `page.url`? If you modify your code to ``, you get the error: "Use of undefined constant page - assumed 'page'". Is `page` a JS variable? If so, you can't use that in this code, since PHP processes long before JS variables are available. This works if `$page = ['url' => '?page=1'];`, and you change it to `$page['url'] == '?page=' . $x`, (or similar). – Tim Lewis Dec 01 '21 at 19:33
  • • The proper quotes to use in HTML is the _double quote_ `"`. • PHP can use single or double quotes for strings. It is therefore easiest to use single quotes in PHP to simply use double quotes in the HTML parts, e.g. `echo 'etc` so you don't have to keep escaping quotes. (The same is true for Javascript; I always use single quotes for my javascript strings to make it easy to use double quotes in the string w/o escaping.) – Stephen P Dec 01 '21 at 19:35

1 Answers1

0

Use a backslash inside echo quotes.

echo "<a class=\"myclass\">";
zxcp
  • 11
  • 2