#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    How to ger $id in foreach command


    Hello,
    I have a piece of code:

    <?php foreach($movies as $movie): ?>
    <tr>
    <td><a href='$_SERVER[PHP_SELF]?id=$id'><?=$movie->moviename ?></a></td>
    <td><?=$movie->categoryname ?></td>
    <td><?=$movie->description ?></td>
    </tr>
    <?php endforeach; ?>

    I would like to turn moviename into link on webpage and when i click that link i get that movie id from my database. At the moment i get in addressbar: My functions are in different file and i also want to sort and search by categories so that situation confuses me. I can sort and sear at the moment.

    Thanks very much for help
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    I'm not too familiar with the colon type layout of loops, but from what you have, it seems you're already doing what you need, just not for the $id. I'm assuming moviename, categoryname, and description are all columns just like id would be. Why not call/reference it the same as the rest?

    Code:
    <a href='$_SERVER[PHP_SELF]?id=$id'>
    
    into
    
    <a href='$_SERVER[PHP_SELF]?id=$movie->id'>
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    The problem is when i hover mouse on that link instead of .../catalog?id=2
    I get on addressbar .../catalog/$_SERVER[PHP_SELF]?id=$id
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    Your variables are in an HTML area. PHP hasn't been defined...
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    12
    Originally Posted by Triple_Nothing
    Your variables are in an HTML area. PHP hasn't been defined...
    TN is right, instead of this:

    PHP Code:
    <a href='$_SERVER[PHP_SELF]?id=$id'
    You need to do:

    PHP Code:
    <a href='<?= $_SERVER[PHP_SELF] . '?id=' $id ?>'>
    Please remember to wrap your code with PHP tags
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    667
    Rep Power
    6
    And if you are going to be using variable throughout your HTML, you can avoid opening and closing PHP all the time by writing this within an echo.

    PHP Code:
    <?php
      
    foreach($movies as $movie):
        echo 
    "<tr>
            <td><a href='" 
    $_SERVER[PHP_SELF] . "?id=" $id "'>" $movie->moviename "</a></td>
            <td>" 
    $movie->categoryname "</td>
            .
            .
            .
    Sorry if I kinda got the single/double quotes backwards...
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    12
    I've certainly done it the way that TN showed, but I think if you are sprinkling php into html opening and closing makes it a bit easier to read in most code editors. Plus code editors will be able to match opening and ending tags. Obviously it depends on the context of what you are doing, but if you look at most frameworks, if you are working in the view (where most of the html is), you open and close just what you are echoing out, control structures like your foreach, etc.

IMN logo majestic logo threadwatch logo seochat tools logo