1

In PHP I am writing:

$link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs='. $cat .'>5.000€</a>";

But the value "$cat" is not showing anything. Any suggestion?

Consider that if I write in HTML:

<a href="<?php echo '/?s=cqs3&importo_desiderato=5000&categoria_cqs=<?php echo $cat; ?>">5.000€</a>

It works...

Probably it is something I am missing in the sintax. Thank you!

3 Answers3

1

I solved like this:

<?php

$cat = $_GET['categoria_cqs'];//(to get the value from the session)
$link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs=$cat'>5.000€</a>";

?>

I still cannot understand why I had to get it again (in HTML it worked without it) but that is fine. Thank you for your help!

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
0

The error is in the quotes, your line should look like this:

$link = '<a href="/?s=cqs3&importo_desiderato=5000&categoria_cqs='. $cat .'">5.000€</a>';

Remember that the string must be closed with the same quotation mark as it was opened.

Additionally, remember that the character " " and ' ' can "escape" themselves, as MoarCodePlz said.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
AKVero
  • 13
  • 3
0

I don't know what your real purpose is, if it is to create this directly in PHP or not, but maybe the code below will help you, I created a variable that will store the website URL that will pass the $cat variable as a parameter, to see the result I used the <a> tag in html

The code would look like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
    $cat = 'Pathname';
    $link = "https://yourwebsite.com/?s=cqs3&importo_desiderato=5000&categoria_cqs=' . $cat . '";
    ?>

    <a href="<?php echo $link; ?>">5.000€</a>
</body>
</html>

If you want to get the result only in PHP, use the following code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
    $cat = 'Pathname';

    $link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs=" . $cat . "'>5.000€</a>";

    echo $link;
    ?>
</body>
</html>

Edit

You say that your code works only using HTML and not in PHP. Well, I've been analyzing it here and I saw that this was happening due to the fact that the file is in a directory, right? PHP was not understanding that the URL was from this directory, at least for me the error was this one in addition to the quotes

Please try the code below, I believe it will work in both PHP and HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
    $cat = $_GET['categoria_cqs'];

    $link = '<a href="/?s=cqs3&importo_desiderato=5000&categoria_cqs=' . $cat . '">5.000€</a>';
    echo $link;
    ?>

    <a href="/?s=cqs3&importo_desiderato=5000&categoria_cqs=<?php echo $cat; ?>">5.000€</a>

</body>
</html>
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98