Motive
Motive

Reputation: 3111

Cleanest way to write a URL in Codeigniter

I have the following code in my view, but it's pretty hard to read. I have read about the Codeigniter url helper and anchor(), but I can't seem to make it much cleaner in there either.

<a href="<?php echo base_url() ?>games/<?php echo $row['slug']; ?>" title="<?php echo $row['title']; ?>"> <?php echo $row['title']; ?></a>

How can I clean this up?

Upvotes: 0

Views: 435

Answers (2)

cchana
cchana

Reputation: 5000

You can pass the segments you require to the base_url() method, like this:

<a href="<?php echo base_url('games/'.$row['slug']) ?>" title="<?php echo $row['title']; ?>"> <?php echo $row['title']; ?></a>

This is cleaner and would give you the same result, however this might be a better option:

<?php echo anchor('games/'.$row['slug'], $row['title'], 'title="'.$row['title'].'"'); ?>

One tag, no need to declare PHP tags for each part that is dynamic and it takes care of the HTML output for you.

Upvotes: 2

alex
alex

Reputation: 490413

You should use reverse routing.

This allows you to have one place to define routes which will propagate through all your existing website's links.

Upvotes: 0

Related Questions