0

I'm relatively new to CodeIgniter and PHP. I'm trying to display table output which includes a reference to a controller function (main/select). Would anyone be able to tell me why this href doesn't work and how I could fix this?

    if($data->num_rows() > 0)
    {
        foreach($data->result() as $row)
        {
            $output .= '
                    <tr>
                        <td><a href="<?php echo base_url(); ?>main/select/<?php $row->PatientID; ?>"> Select </a></td>
                        <td>'.$row->MRN.'</td>
                        <td>'.$row->LastName.'</td>
                        <td>'.$row->FirstName.'</td>
                        <td>'.$row->DateOfBirth.'</td>
                        <td>'.$row->Gender.'</td>
                    </tr>
            ';
        }
nb0yc33
  • 11
  • 1

1 Answers1

1

You can't use echo inside a PHP string (or inside any other PHP command). Even if you could, it wouldn't do what you want in this scenario. And you certainly can't open new PHP tags inside another PHP tag either!

Just concatenate the variables and static parts of the string together in the normal way with the . operator - exactly the same as you're doing for all the other variables on subsequent lines of the same string, in fact.

<td><a href="'.base_url().'main/select/'.$row->PatientID.'"> Select </a></td>
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • there should be a declaration of variable `$output=''`before the for each loop, too – Vickel Aug 20 '20 at 22:35
  • @Vickel since this is only a snippet we don't know if that already exists or not. And it's not relevant to the question, which was specifically about the hyperlink(s) - they aren't complaining that the rest of the table isn't being displayed, so chances are the overall structure is ok – ADyson Aug 20 '20 at 22:42
  • just a comment to point out a very common error of beginner's code – Vickel Aug 20 '20 at 22:45
  • @Vickel indeed. But it's not relevant to this question. – ADyson Aug 20 '20 at 22:47
  • Okay, thank you very much – nb0yc33 Aug 20 '20 at 23:38