Jump to content

need help with if and else


alapimba

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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.