<a href ..>Setting a variable before the page calls itself

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • universalbitmapper

    <a href ..>Setting a variable before the page calls itself

    Hi,

    This exercise (From PAM525Web Development) asks me to display a table
    of reviews, then sort the reviews depending on which column heading is
    clicked.

    I can't find how to write this line:

    <th><a
    href=movie_deta ils03.php?movie _id=$movie_id&c oll_sort=\"revi ew_date\">"Date
    of Review"</th>

    in this bit of code:

    $review_table_h eadings=<<<EOD
    <tr>
    <th><a
    href=movie_deta ils03.php?movie _id=$movie_id&c oll_sort=\"revi ew_date\">"Date
    of Review"</th>
    <th>Review Title</th>
    <th>Reviewer Name</th>
    <th>Movie Review Comments</th>
    <th>Ratings</th>
    </tr>
    EOD;

    The coll_sort variable is extracted by a GET

  • Erwin Moller

    #2
    Re: &lt;a href ..&gt;Setting a variable before the page calls itself

    universalbitmap per wrote:
    [color=blue]
    > Hi,
    >
    > This exercise (From PAM525Web Development) asks me to display a table
    > of reviews, then sort the reviews depending on which column heading is
    > clicked.
    >
    > I can't find how to write this line:
    >
    > <th><a
    >[/color]
    href=movie_deta ils03.php?movie _id=$movie_id&c oll_sort=\"revi ew_date\">"Date[color=blue]
    > of Review"</th>
    >
    > in this bit of code:
    >
    > $review_table_h eadings=<<<EOD
    > <tr>
    > <th><a
    >[/color]
    href=movie_deta ils03.php?movie _id=$movie_id&c oll_sort=\"revi ew_date\">"Date[color=blue]
    > of Review"</th>
    > <th>Review Title</th>
    > <th>Reviewer Name</th>
    > <th>Movie Review Comments</th>
    > <th>Ratings</th>
    > </tr>
    > EOD;
    >
    > The coll_sort variable is extracted by a GET[/color]


    Hi,

    You don't want " or ' in your url.
    use urlencode to pass them around safely.
    So try:
    <?
    $myUrl = "movie_detailso 3.php";

    // assuming $movie_id is a number, you do not need urlencoding
    $myUrl .= "?movie_id=".$m ovie_id;

    // assuming coll_sort need encoding:
    $myUrl .= "&amp;coll_sort =".urlencode("r evie_date");
    ?>
    <a href="<?= $myUrl ?>">sort on review date</a>

    Now receive it like:
    $sortorder = $_GET["coll_sort"];

    Also note the &amp; instead of &. It was not your problem, but the &amp; is
    better, according to W3C.

    Regards,
    Erwin Moller

    Comment

    • David Haynes

      #3
      Re: &lt;a href ..&gt;Setting a variable before the page calls itself

      universalbitmap per wrote:[color=blue]
      > Hi,
      >
      > This exercise (From PAM525Web Development) asks me to display a table
      > of reviews, then sort the reviews depending on which column heading is
      > clicked.
      >
      > I can't find how to write this line:
      >
      > <th><a
      > href=movie_deta ils03.php?movie _id=$movie_id&c oll_sort=\"revi ew_date\">"Date
      > of Review"</th>
      >
      > in this bit of code:
      >
      > $review_table_h eadings=<<<EOD
      > <tr>
      > <th><a
      > href=movie_deta ils03.php?movie _id=$movie_id&c oll_sort=\"revi ew_date\">"Date
      > of Review"</th>
      > <th>Review Title</th>
      > <th>Reviewer Name</th>
      > <th>Movie Review Comments</th>
      > <th>Ratings</th>
      > </tr>
      > EOD;
      >
      > The coll_sort variable is extracted by a GET
      >[/color]

      I may have this screwed up, but isn't this just a case of setting a
      default sort order and 'href'fing the headers?

      If so, the form needs some top processing...

      <?php
      // use the specified column for sorting or sort by ratings by default
      $coll_sort = isset($_GET['coll_sort']) ? $_GET['coll_sort'] : 'rating';

      // select and order based on coll_sort
      $sql = <<<SQL
      select
      movie_id,
      title,
      name,
      comment,
      rating
      from
      reviews
      order by
      $coll_sort;

      $result = ....

      I've assumed a database-backed form here. If this is just an array, then
      you would use array sorts to do the same thing.

      Then you would need to change the references to 'Review Title',
      'Reviewer Name', etc. to be href links that set the coll_sort.

      For example:
      <th><a
      href="movie_det ails03.php?movi e_id=$movie_id& coll_sort=title ">Review
      Title</th>

      Notes:
      Make the coll_sort value match the column name in the database unless
      you are worried about exposing that level of implementation detail to
      the user. If so, make sure to map the coll_sort name into a database
      column name before you do the query.

      There is no need to use the double quotes around the argument to
      coll_sort in your href. The entire href= argument is already quoted.

      Hope this helps.
      -david-

      Comment

      • universalbitmapper

        #4
        Re: &lt;a href ..&gt;Setting a variable before the page calls itself

        I have tested two little scripts with &amp; in the href links and got
        them to work
        Many thanks for your replies

        Comment

        Working...