1

I am trying to add a variable to the end of an link <a href="foo.com/$ID">LINK</a> I am using Laravel and am passing an array of data to the view were its looped through and then displayed.

View

<form action="{{ URL::route('Search')}}" method="get">
Search<input type="text" name="query" /><br>
<input type="submit" value="Search" />
  {{Form::token()}}
  </form>
  <br>

<?php if (count($Results) > 0): ?>
<table style="border-collapse: separate; border-spacing: 25px 5px;">
  <thead>
    <tr>
      <p></p><th><?php echo implode('</th><th>', array_keys(current($Results))); ?></th>
    </tr>
  </thead>
  <tbody>
<?php foreach ($Results as $row): array_map('htmlentities', $row); ?>
    <tr>
      <td><?php echo implode('</td><td>', $row); ?></td>
    </tr>
<?php endforeach; ?>
  <tbody>
</table>
<?php endif; ?>

this displays.

title               place_film_country  wab_id
4th of February     Sri Lanka           266

So I am trying to turn the wab_id and pass it into a variable and stick it on the end of the link.

Controller

public function Search(){
        $query = Input::get('query'); 
            $raw_results =  DB::Table('films')->select('title', 'place_film_country', 'wab_id')
                              ->where('title', 'LIKE', "%$query%")
                              ->orwhere('place_film_country', 'LIKE', "%$query%")
                              ->orwhere('genre', 'LIKE', "%$query%")
                              ->orwhere('wab_id', 'LIKE', "%$query%")
                              ->get();
             $array = json_decode(json_encode($raw_results), true);

            return View::make('Index')->with('Results', $array);
        } 
0

2 Answers 2

1

Replace this:

<td><?php echo implode('</td><td>', $row); ?></td>

With something like this:

<td><?= $row['title'] ?></td>
<td><?= $row['place_film_country'] ?></td>
<td><a href="http://foo.com/<?= $row['wab_id'] ?>"><?= $row['wab_id'] ?></a></td>
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you but if you don't mind me asking why was <?= $row['wab_id'] ?> added a second time to the end i removed it and got the same results, just wondering if it was meant to display the id along with "Link".
'just wondering if it was meant to display the id along with "Link".' -- yes exactly that. Display the ID and have it a link.
Ah I see, I missed the extra ">" incantation there so that the ID should have appeared between <a href=".."> and </a> -- the first > was what I missed out.
@delatbabel how we can add hardcoded url in href instead to use built in function by laravel to do that?
1

Your question is a bit confusing, but if you have a variable $ID and you want to append it to the end of your URL, you first need to wrap it in php tags, such as:

<a href="<?=URL("route/{$ID}")?>">LINK</a>

Or if you are using blade:

<a href="{{URL("route/{$ID}")}}">LINK</a>

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.