The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
How to ger $id in foreach command
Discuss How to ger $id in foreach command in the PHP Development forum on Dev Shed. How to ger $id in foreach command PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 25th, 2012, 11:39 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
Time spent in forums: 15 m 38 sec
Reputation 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
|

November 25th, 2012, 11:56 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 295
  
Time spent in forums: 3 Days 8 h 35 m 37 sec
Reputation Power: 5
|
|
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'>
|

November 25th, 2012, 12:35 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
Time spent in forums: 15 m 38 sec
Reputation 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
|

November 25th, 2012, 12:45 PM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 295
  
Time spent in forums: 3 Days 8 h 35 m 37 sec
Reputation Power: 5
|
|
|
Your variables are in an HTML area. PHP hasn't been defined...
|

November 25th, 2012, 01:01 PM
|
 |
Contributing User
|
|
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 712
 
Time spent in forums: 4 Days 11 h 4 m 59 sec
Reputation Power: 11
|
|
Quote: | 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
|

November 25th, 2012, 01:16 PM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 295
  
Time spent in forums: 3 Days 8 h 35 m 37 sec
Reputation Power: 5
|
|
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...
|

November 25th, 2012, 04:26 PM
|
 |
Contributing User
|
|
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 712
 
Time spent in forums: 4 Days 11 h 4 m 59 sec
Reputation Power: 11
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|