0

What I'm trying to do is that any form with the $id that is less than 50 to use a certain A href else use another A HREF. This is what I have so far but I have no idea how to include it since it has php with in the HREF. How can I make this work. Thanks in advance.

</td>        
<td>    
<?php if($id <= 50) { 
    <a href="<?=$row[form_type]?>.php?id=<?=$row[id]?>&form=<?=$row[form_type]?>&***template***=true"><?=$row[software]?></a> 
}
else  { 
    <a href="<?=$row[form_type]?>.php?id=<?=$row[id]?>&form=<?=$row[form_type]?>&***modify***=true"><?=$row[software]?></a> 
} 
?>
</td>
Jatin
  • 3,065
  • 6
  • 28
  • 42
Deleted
  • 91
  • 1
  • 13
  • For some reason is not reading or not liking the code, I tested all examples and it will link to the first href even if the $id is greater than 50, I even switch the <= to >= and it linked only to the second href even if less or greater. I think there is something else making do this. But really appreciate the quick response. – Deleted Mar 10 '14 at 20:36

6 Answers6

2
<td>    
<?php if($id <= 50): ?>
    <a href="<?=$row[form_type]?>.php?id=<?=$row[id]?>&form=<?=$row[form_type]?>&***template***=true"><?=$row[software]?></a> 
<?php else: ?> 
    <a href="<?=$row[form_type]?>.php?id=<?=$row[id]?>&form=<?=$row[form_type]?>&***modify***=true"><?=$row[software]?></a> 
<?php endif; ?>
</td>
2

A cleaner way to do this would be to just use an if statement on the part of the anchor tag that changes (the modify/template bit):

<a href="<?=$row[form_type]?>.php?id=<?=$row[id]?>&form=<?=$row[form_type]?>&<?=$id <= 50 ? 'template' : 'modify'?>=true"><?=$row[software]?></a>

You can put this anywhere in a page that is parsed by PHP (doesn't matter that there's inline PHP in your HTML.

Exit PHP like this (easy enough - in your case you don't need to edit really, you just wrap the top example here in your <td> tags):

?>
<a href="<?=$row[form_type]?>.php?id=<?=$row[id]?>&form=<?=$row[form_type]?>&<?=$id <= 50 ? 'template' : 'modify'?>=true"><?=$row[software]?></a>
<?php

... or:

$action = $id <= 50 ? 'template' : 'modify';
echo '<a href="' . $row['form_type'] . '.php?id=' . $row['id'] . '&form=' . $row['form_tyoe'] . '&' . $action . '=true">' . $row['software'] . '</a>';
scrowler
  • 24,273
  • 9
  • 60
  • 92
1
<?php
if($id <= 50) {
    echo '<a href="' . $row['form_type'] . '.php?id=' . $row['id'] . '&form='. $row['form_type']. '&***template***=true">' . $row['software'] . '</a>';
} else {
 echo '<a href="'. $row['form_type'] . '.php?id='. $row['id'] . '&form='. $row['form_type'] . '&***modify***=true">'. $row['software'] . '</a>';
} 
?>
SuperDJ
  • 7,488
  • 11
  • 40
  • 74
1

printf() is your friend.

<td>
<?php
$frame = "<a href="%s.php?id=%s&form=%s&%s=true">%s</a>";
if($id <= 50) {
  printf($frame, $row[form_type], $row[id], $row[form_type], '***template***', $row[software]);
} else  { 
  printf($frame, $row[form_type], $row[id], $row[form_type], '***modify***', $row[software]);
} 
?>
</td>
Sammitch
  • 30,782
  • 7
  • 50
  • 77
0
</td>
<td>    
<?php 
    if($id <= 50) { 
        echo '<a href="'.$row[form_type].'.php?id='.$row[id].'&form='.$row[form_type].'&***template***=true">'.$row[software].'</a>';
    } else  { 
        echo '<a href="'.$row[form_type].'.php?id='.$row[id].'&form='.$row[form_type].'&***modify***=true">'.$row[software].'</a>';
    } 
?>

Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115
0

First of all, don't use the short tags <?=?>, this is considered bad practice.

Secondly, you may use this:

</td>        
<td>    
  <?php if($id <= 50) { ?> 
        <a href="<?php echo $row[form_type]?>.php?id=<?php echo $row[id]?>&form=<?php echo $row[form_type]?>&***template***=true"><?php echo $row[software]?></a> 
       <?php } else  { ?>
        <a href="<?php echo $row[form_type]?>.php?id=<?php echo $row[id]?>&form=<?php echo $row[form_type]?>&***modify***=true"><?php echo $row[software]?></a> 
  <?php } ?>
</td>
Onion
  • 1,714
  • 1
  • 23
  • 42
  • Short tags are enabled by default in PHP >= 5.4, so I don't think it's bad practice at all. http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use – scrowler Mar 10 '14 at 19:44
  • Nevertheless, they are the source of all problems if you decide to move your program to a server with PHP < 5.4. Also, see this answer for further explanation: http://programmers.stackexchange.com/a/151694 – Onion Mar 10 '14 at 19:46