Using assigned IDs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Using assigned IDs

Post by phpflixnewbie »

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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Sorry, I'm really confused by your question, can you restate it or explain some more?
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post by speedy33417 »

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:

Code: Select all

if(empty($_GET['id'])) {
	//if not set do something
} else {
	$userId= $_GET['id'];
}
If this is not the answer you're looking for please try explain your problem in more detail.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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:

Code: Select all

if(empty($_GET['id'])) {
	//if not set do something
} else {
	$userId= $_GET['id'];
}
If this is not the answer you're looking for please try explain your problem in more detail.
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?

Code: Select all

if(!empty($_GET['id'])) {
	$userId= $_GET['id'];
}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Sometimes using NOT (!) is hard for people to easily grasp. It's a concept that seems simple when you get it, but I remember saying "what??" when I first got the concept.

Then again, maybe they plan to do something specific if it's empty. Who knows.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Typically, in circumstances like this, I would say check to see if it is set and do something with it, else do something else. I would always tell you to never use and an if check against a positive for the sole purpose of not doing anything. But that is just me. :wink:
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

:lol: That's me too. EXCEPT what if the only time you care is if its empty? Then what? :-p
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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
}
?>
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post by speedy33417 »

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... :lol:
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

Post by phpflixnewbie »

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.

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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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...

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

Post by phpflixnewbie »

Would this code need to be on the detail.php page, and do I need to have the detail information in the database?
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post by speedy33417 »

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.

Code: Select all

if(!empty($_GET['id'])) 
{ 
         $itemId= $_GET['id']; 
}
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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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?
This can all be done in one page. It doesn't have to be, but it can be.

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

Post by phpflixnewbie »

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!

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
}
?>
Post Reply