Jump to content

Recommended Posts

Hello,

I need to echo diferent links depending of an item on a database, but i don't know how to write the code.

I guess i'm almost there and must be just a need of ( ) ' " ; in the right places...

Can anyone help me?

I wrote this:

[code=php:0]<?php
        $tipo = $row_rs_clientes_detail['tipo'];
        if ($tipo='pilotos') echo "<a href='perfil_clientes.php?id=$row_rs_clientes_detail['id'];&tipo=$row_rs_clientes_detail['tipo'];'> perfil</a>";
        if ($tipo='equipas') echo "<a href='apresentacao_clientes.php?id=$row_rs_clientes_detail['id'];&tipo=$row_rs_clientes_detail['tipo'];'> apresentação</a>";
        if ($tipo='organizadores') echo "<a href='apresentacao_clientes.php?id=$row_rs_clientes_detail['id'];&tipo=$row_rs_clientes_detail['tipo'];'> apresentação</a>";
        ?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/32167-need-help-with-if-and-else/
Share on other sites

It'll be easier and better if you used a switch rather than if statements. Like so:
[code]$tipo = $row_rs_clientes_detail['tipo'];

switch($tipo)
{
    case 'pilotos':
        echo '<a href="perfil_clientes.php?id=' . $row_rs_clientes_detail['id'] . '&tipo=' . $tipo .'">perfil</a>';
    break;

    case 'equipas':
        echo '<a href="apresentacao_clientes.php?id=' . $row_rs_clientes_detail['id'] . '&tipo=' . $tipo . '">apresentação</a>';
    break;

    case 'organizadores':
        echo '<a href="apresentacao_clientes.php?id=' . $row_rs_clientes_detail['id'] . '&tipo=' . $tipo . '">apresentação</a>';
    break;
}[/code]
You need to use the "==", not "=" in the "if" statement. The "==" is the comparison operator, the "=" is the assignment operator. There is a difference. You also have single quotes within strings delimited by single quotes. Another way to write this would be to use a switch statement:
[code]<?php
$href= '';
switch ($tipo) {
    case 'pilotos':
        $href= '<a href="perfil_clientes.php?id=' . $row_rs_clientes_detail['id'] . '&tipo=' . $row_rs_clientes_detail['tipo'] . "> perfil</a>"
        break;
    case 'equipas':
        $href = '<a href="apresentacao_clientes.php?id=' . $row_rs_clientes_detail['id'] . '&tipo=' . $row_rs_clientes_detail['tipo'] . '"> apresentação</a>';
        break;
    case 'organizadores':
        $href = '<a href="apresentacao_clientes.php?id=' . $row_rs_clientes_detail['id'] '&tipo=' . $row_rs_clientes_detail['tipo'] . '"> apresentação</a>';
          break;
}
echo $href;
?>[/code]

Wildteen beat me to it...

Ken
[code]
<?php
$tipo = $row_rs_clientes_detail['tipo'];

switch ($tipo) {
   case "pilotos":
      echo "<a href='perfil_clientes.php?id=" . $row_rs_clientes_detail['id'] . "&tipo=" . $row_rs_clientes_detail['tipo'] . "'>Perfil</a>";
       break;

   case "equipas":
      echo "<a href='apresentacao_clientes.php?id=" . $row_rs_clientes_detail['id'] . "&tipo=" . $row_res_clientes_detail['tipo'] . "'>Apresentação</a>";
       break;

   case "organizadores":
       echo "<a href='apresentacao_clientes.php?id=" . $row_rs_clientes_detail['id'] . "&tipo=" . $row_res_clientes_detail['tipo'] . "'>Apresentação</a>";
       break;
}
[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.