Using assigned IDs
Moderator: General Moderators
-
phpflixnewbie
- Forum Contributor
- Posts: 132
- Joined: Fri Nov 17, 2006 11:46 am
Using assigned IDs
After getting the htmlentities working and having the URL id automatically assigned to each entry which is pulled from the database, how do i actually use the assigned id.
ie. if the assigned URL/id is http://www.domainname/detail.php?id=49
What filename do i give to the php file which has the details for id 49?
Its obviously a common problem, but as a beginner I need a hand please.
ie. if the assigned URL/id is http://www.domainname/detail.php?id=49
What filename do i give to the php file which has the details for id 49?
Its obviously a common problem, but as a beginner I need a hand please.
- speedy33417
- Forum Contributor
- Posts: 128
- Joined: Sun Jul 23, 2006 1:14 pm
I'm not sure if I understand your question either. I'm guessing you need help passing the id to another page???
On the receiving page have this code:
If this is not the answer you're looking for please try explain your problem in more detail.
On the receiving page have this code:
Code: Select all
if(empty($_GET['id'])) {
//if not set do something
} else {
$userId= $_GET['id'];
}- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Seems kinda odd logic, doesn't it? I mean, check to see if it is empty so you can do nothing, else do something? Why not just check to see if it is not empty?speedy33417 wrote:I'm not sure if I understand your question either. I'm guessing you need help passing the id to another page???
On the receiving page have this code:
If this is not the answer you're looking for please try explain your problem in more detail.Code: Select all
if(empty($_GET['id'])) { //if not set do something } else { $userId= $_GET['id']; }
Code: Select all
if(!empty($_GET['id'])) {
$userId= $_GET['id'];
}- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
If the only time you care is if it is empty, then you check
Code: Select all
<?php
if(empty($var)) {
// Do what I need if the thing is empty
}
?>- speedy33417
- Forum Contributor
- Posts: 128
- Joined: Sun Jul 23, 2006 1:14 pm
Wow! I didn't realize that my 5 lines of code would bring this much attention.
Being new to php myself, I've had plenty of help on this forum. Finally I felt I maybe of help...
Way to put no pressure on me for any future urge to try and help...
The //if not set do something line was supposed to be an opportunity for him to avoid any bad input or lack thereof.
Anyhow. Nuff said.
Being new to php myself, I've had plenty of help on this forum. Finally I felt I maybe of help...
Way to put no pressure on me for any future urge to try and help...
The //if not set do something line was supposed to be an opportunity for him to avoid any bad input or lack thereof.
Anyhow. Nuff said.
-
phpflixnewbie
- Forum Contributor
- Posts: 132
- Joined: Fri Nov 17, 2006 11:46 am
Thanks for the replies. Basically i have a table of DVD titles, when the script prints out the results of the table it also assigns a unique ID number to that DVD title, as shown below.
Now obviously i cant make hundreds of pages called detail.php because they wont be able to sit on the webserver in the same directory. So how do I distinguish each detail page from each other using the unique IDs?
I have since been told, the best way to do this would be to also store the details information/image urls etc in the database as well. This would involve either changing the main sql query to also retrieve the details information or adding something at the beginning of the detail.php page. Probably something along the lines of the code you guys mentioned already, but im not sure.
Hope this explains my problem better, and any further help is much appreciated.
Code: Select all
// Printing results in HTML
echo '<table>';
echo "<tr><th>Title</th><th>Rating</th><th>DVD Release Date</th></tr>";
while( $row=mysql_fetch_array($result, MYSQL_ASSOC) ) {
echo '<tr>',
'<td><a href="detail.php?id=', $row['dvd_id'], '">', htmlentities($row['dvd_title']), '</a></td>',
'<td>', htmlentities($row['rounded_rating']), '</td>',
'<td>', htmlentities($row['rlsdate']), '</td>',
'</tr>';
}
echo '</table>';
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($Link_ID);Now obviously i cant make hundreds of pages called detail.php because they wont be able to sit on the webserver in the same directory. So how do I distinguish each detail page from each other using the unique IDs?
I have since been told, the best way to do this would be to also store the details information/image urls etc in the database as well. This would involve either changing the main sql query to also retrieve the details information or adding something at the beginning of the detail.php page. Probably something along the lines of the code you guys mentioned already, but im not sure.
Hope this explains my problem better, and any further help is much appreciated.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
You are making what's called a master/detail form. Basically you output a list of data that is not very specific but has enough information for a user to make a choice on. A user clicks a link and, voila!, they are at a page with a lot more detail about what they just clicked.
What you want to do is something along the lines of...
What you want to do is something along the lines of...
Code: Select all
<?php
// Trigger to tell us if we show the master or the detail
$show_detail = false;
if (isset($_GET['id']))
{
$dvd_id = mysql_real_escape_string($_GET['id']);
// Run a query of the DVD's based on this ID, so a SELECT * FROM `table` WHERE `idfield` = $dvd_id
// IF THERE IS A RESULT save that result into a array var like $dvd_detail = mysql_fetch_array($result)
// and set the $show_detail flag to true like $show_detail = true;
// If there is no result, you can either echo a warning or just go back to the master
}
// After getting the detail information, we check for the show_detail flag
if ($show_detail)
{
// Use the $dvd_detail array to show the information the way you want
}
else
{
// Run the code that gets and shows the master
}
?>-
phpflixnewbie
- Forum Contributor
- Posts: 132
- Joined: Fri Nov 17, 2006 11:46 am
- speedy33417
- Forum Contributor
- Posts: 128
- Joined: Sun Jul 23, 2006 1:14 pm
Basically you need two pages for this. The first is a form where the user selects the information he/she's looking for. The form passes on the id of the requested item in a form as you first posted:
http://www.domainname/detail.php?id=49
Then use this code in detail.php to receive it.
Now that you have the itemId, use that to pull all the information that you need to display.
Database is recommended, but arrays would do I guess.
Hope this helps.
http://www.domainname/detail.php?id=49
Then use this code in detail.php to receive it.
Code: Select all
if(!empty($_GET['id']))
{
$itemId= $_GET['id'];
}Database is recommended, but arrays would do I guess.
Hope this helps.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
This can all be done in one page. It doesn't have to be, but it can be.phpflixnewbie wrote:Would this code need to be on the detail.php page, and do I need to have the detail information in the database?
It is always helpful to have the requested information in the database (else what is the use of offering a link to the item to begin with?).
-
phpflixnewbie
- Forum Contributor
- Posts: 132
- Joined: Fri Nov 17, 2006 11:46 am
Hi guys, ive made a start on the code, its obviously not right yet, was hoping someone could ammend it a little to get a working script.
Appreciate all the help guys!
Appreciate all the help guys!
Code: Select all
<?php
$show_detail = false;
if (isset($_GET['id']))
{
$dvd_id = mysql_real_escape_string($_GET['id']);
$query = select dvd_id, dvd_plot from dvd_details where dvd_id = $dvd_id;
$dvd_detail = mysql_fetch_array($result);
}
if ($show_detail)
{
$dvd_detail = mysql_fetch_array($result)
echo $dvd_detail;
}
else
{
// Will add code for master/main table here
}
?>