PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 25th, 2012, 11:39 AM
sabbath sabbath is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 sabbath User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old November 25th, 2012, 11:56 AM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 294 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 8 h 6 m 9 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'>

Reply With Quote
  #3  
Old November 25th, 2012, 12:35 PM
sabbath sabbath is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 sabbath User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old November 25th, 2012, 12:45 PM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 294 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 8 h 6 m 9 sec
Reputation Power: 5
Your variables are in an HTML area. PHP hasn't been defined...

Reply With Quote
  #5  
Old November 25th, 2012, 01:01 PM
msteudel's Avatar
msteudel msteudel is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 712 msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level) 
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

Reply With Quote
  #6  
Old November 25th, 2012, 01:16 PM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 294 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 8 h 6 m 9 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...

Reply With Quote
  #7  
Old November 25th, 2012, 04:26 PM
msteudel's Avatar
msteudel msteudel is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 712 msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > How to ger $id in foreach command

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap