0

I am new to PHP frameworks and am stuck on a problem where I am trying to put the values of an array into a site url in Codeigniter. I am querying a database and getting the results back no problem. I am trying to use each result to create links using site urls.

Here is what I have at the so far

  if (is_array($results))
   {

    if( !empty($results) ) 
    {  
      foreach($results as $row) 
         {
          $venue_name = $row->venue_name;
          $venue_town = $row->venue_town;
          $venue_county = $row->venue_county;

          echo '<div class="col-sm-12 col-md-6">'; 

          echo '<tr>';

          echo '<h4>';
          echo '<td>'.$row->fixture_round.'</td>'."</br></br>";

          echo '<td >'.'<img src="'."$row->team_logo".'">   '.$row->fixture_text.'    <img src="'."$row->team_logo_2".'"> '.'</td>'."</br></br>";
             echo '<td>'.$row->fixture_comp.'</td>'."</br>";
             echo '</h4>';
             echo '<td>'.$row->fixture_time.' '.$row->fixture_date.'</td>'."</br></br>";
             echo '<td>'.$row->venue_name.',  '.$row->venue_town.'</td>'."</br></br>";

             echo '</tr>';
         }
       }

      else echo "Not Array";

    }

I am trying to create a link that uses values from the array and concatenates them together to create a link - I have enabled query strings and am trying to use $_GET to do something like this but I cant get it to work

<a href="<?php echo site_url('user/reviews?venue_name=$venue_name&venue_town=$venue_town&venue_county=$venue_county') ?>Link to item</a>

This goes to a page but it displays the variable names instead of the value contained within them.

Welcome to $venue_name, $venue_town, $venue_county
0

2 Answers 2

1

This works really slick. You will need to load the url helper first:

$this->load->helper('url');

Then you can do this:

<?php echo anchor('user/reviews?venue_name='.$venue_name.'&venue_town='.$venue_town.'&venue_county='.$venue_county, 'Link to item', 'title="Link to item"'); ?>

Or the traditional way (you do not need to load the helper in this case):

<a href="<?php echo site_url('user/reviews?venue_name='.$venue_name.'&venue_town='.$venue_town.'&venue_county='.$venue_county) ?>Link to item</a>

CI URL Helper

Ok I think we need to take a step back a bit. Believe it or not, but I think you are trying to do too much yourself. Let the framework help you out a little more...

In your project, you should have a view, a controller and a model. It sounds like you have each, and your model is returning results.

Now, in your controller it should look something like this:

<?php 
// load venue model
// load review model
// load user model
// load any other helpers

public function index(){
    $venues = [];
    $venues = $this->venue_model->get_all_venues();

    $data[ 'venues' ] = ( $venues ) ? $venues : NULL;
    $this->load->view( 'path-to-your-view', $data );
}


<p>Venues</p>
<?php
if ( $venues )
{
?>
    <table>
    <?php
    foreach($venues as $venue)
    {
    ?>      
        <tr><td><?php echo anchor( 'user/reviews/'.$venue->venue_id, $venue->venue_name); ?></td></tr>
    <?php  
    }
    ?>
    </table>
<?php
}
?>
... rest of your markup here ....

Then you will have a route something like (controller names are example):

$route['user-reviews/(:num)'] = 'your_controller/get_user_review/$1';

Then in the controller, you will have a matching method:

<?php
public function get_user_review($userID) {
    $user_reviews = $this->your_model->get_user_reviews('$user_id);
    $data[ 'user_reviews' ] = ( $user_reviews ) ? $user_reviews : NULL;
    $this->load->view( 'path-to-your-view', $user_reviews );
}

And finally, your view:

<h1>Reviews</h1>
<?php
if($user_reviews) {
    // loop through reviews.
}
else {
?>      
   <p>No reviews found for user.</p>

<?php
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your response. I have used your answer and it goes to the page I need which is perfect. This is what I did to it <a href="<?php echo site_url("user/reviews? venue_name='$venue_name'.venue_town='$venue_town'.venue_county='$venue_county'") ?>Link</a> I need to take it a step further. I need to somehow use the $_GET method to assign the values because on the page where the link goes I have the following which displays the values in the variables echo "Welcome to ".$_GET['venue_name'].", ". $_GET['venue_town'].", ".$_GET['venue_county'] ;
I updated my response - hopefully it will get you rolling in the right direction. It takes a bit to get used to, but once the light bulb goes on it's pretty neat.
I went back to my code and used your original suggestions and they both work. I particularly like the URL helpers one which is nice and clean. The second one is similar to what I was trying to use but mine was full of quotation marks in the wrong places. Thank you very much for your help.
0

Some things I noticed in your question example code:

The reason you are getting the variable names is because you have the variable (eg: $venue_name) enclosed inside single quotes.

When using variables inside single quotes, you have to break the variables outside of them.

Another thing I noticed in your example is that the <a> tag is not closed properly. (You might have this fixed in your code?)

You have:

...$venue_county) ?>Link to item</a>

It should be:

                  Notice
                    VV
...$venue_county) ?>">Link to item</a>

Try This:

<a href="<?php echo site_url('user/reviews?venue_name='.$venue_name.'&venue_town='.$venue_town.'&venue_county='.$venue_county) ?>">Link to item</a>

Creating a proper link will allow you to pass those variables to the other page where you can use $_GET to retrieve those values.

1 Comment

Thank you for your response to my question - I was missing the closing bracket from the anchor tag I posted - the joys of copy and paste :)

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.