Page 1 of 3

Using assigned IDs

Posted: Tue Feb 13, 2007 2:06 pm
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.

Posted: Tue Feb 13, 2007 4:10 pm
by feyd
Sorry, I'm really confused by your question, can you restate it or explain some more?

Posted: Tue Feb 13, 2007 5:17 pm
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.

Posted: Tue Feb 13, 2007 5:53 pm
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'];
}

Posted: Tue Feb 13, 2007 5:57 pm
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.

Posted: Tue Feb 13, 2007 6:10 pm
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:

Posted: Tue Feb 13, 2007 6:19 pm
by superdezign
:lol: That's me too. EXCEPT what if the only time you care is if its empty? Then what? :-p

Posted: Tue Feb 13, 2007 6:23 pm
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
}
?>

Posted: Tue Feb 13, 2007 10:04 pm
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.

Posted: Wed Feb 14, 2007 11:26 am
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.

Posted: Wed Feb 14, 2007 11:37 am
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
}
?>

Posted: Wed Feb 14, 2007 12:05 pm
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?

Posted: Wed Feb 14, 2007 12:42 pm
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.

Posted: Wed Feb 14, 2007 2:00 pm
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?).

Posted: Thu Feb 15, 2007 1:29 pm
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
}
?>