Item page PHP

waveparticle

Active Member
Joined
Dec 4, 2013
Messages
96
Reaction score
0
Hi people,

New question for today. So I want to make a page which shows my item description but I don't exactly know how to work around this.

I was thinking of using a GET method and then grab the details of a specific item and send it to a link such as:
www.site.com/item.php?id=34546546

But the problem is I don't know how to go about this in case someone goes directly to the item page, let's say in case of Bookmarks. So I was thinking the page should definitely find the id in my DB and output the array of information for the row.

That's my problem. I'm not sure how to go around this. Please just point me to the right direction and I'll try to make it. If I encounter a problem I'll get back here to ask :)

Thanks
 
How will the page know what to display? Where is the information grabbed for that specific URL?

Oh you mean if they go to www.site.com/item.php ?

In the above case, you could have this:

Code:
if(!isset($_GET['id']))
{ header("location: index.php"); }

Which means that if the id parameter isn't specified then it will redirect you to index.php.
 
How will the page know what to display? Where is the information grabbed for that specific URL?

It knows what to display because you've coded the page (in your example item.php) to display something. The only thing that changes on the page is the item data.

If this not what you mean then I'm lost :)
 
Oh you mean if they go to www.site.com/item.php ?

In the above case, you could have this:

Code:
if(!isset($_GET['id']))
{ header("location: index.php"); }

Which means that if the id parameter isn't specified then it will redirect you to index.php.

But what happens in the case of item.php?id=23234324 ?

In that case how is the GET method assigned to this id?

What I'm thinking is this needs to be assigned, then I go in my DB, SELECT * FROM table WHERE id=23234323 and use mysqli_fetch_assoc and loop through it, then display whatever information I need to. Is that a good way of doing it?
 
But what happens in the case of item.php?id=23234324 ?

In that case how is the GET method assigned to this id?

What I'm thinking is this needs to be assigned, then I go in my DB, SELECT * FROM table WHERE id=23234323 and use mysqli_fetch_assoc and loop through it, then display whatever information I need to. Is that a good way of doing it?

In the case of id=12345 then id is specified, therefore the redirect is skipped.
Yes, that query will work fine. Just make sure you account for SQL injection.
 
It knows what to display because you've coded the page (in your example item.php) to display something. The only thing that changes on the page is the item data.

If this not what you mean then I'm lost :)

Yup that's it. I'm working on it now. Will post my progress :D
Thanks

In the case of id=12345 then id is specified, therefore the redirect is skipped.
Yes, that query will work fine. Just make sure you account for SQL injection.
Great, I'm on the right track. I'll use prepared statements to account for the SQL Injection.4
 
Oh you mean if they go to www.site.com/item.php ?

In the above case, you could have this:

Code:
if(!isset($_GET['id']))
{ header("location: index.php"); }

Which means that if the id parameter isn't specified then it will redirect you to index.php.

Come here and collect your slap !!!!

That is the worst code in the world. You never ever do a redirect header call without an exit/die afterwards else you will still execute the remaining PHP code. I've seen "professional" development companies do this on form submissions and cannot understand why their code still executes.


Code:
if(!isset($_GET['id']))
{ 
  header("location: index.php"); 
[B]  die(); // or use exit;
[/B]}
 
Come here and collect your slap !!!!

That is the worst code in the world. You never ever do a redirect header call without an exit/die afterwards else you will still execute the remaining PHP code. I've seen "professional" development companies do this on form submissions and cannot understand why their code still executes.


Code:
if(!isset($_GET['id']))
{ 
  header("location: index.php"); 
[B]  die(); // or use exit;
[/B]}

OMG, I would deserve hundreds of slaps but I'm new to PHP :P. I'll have to review all my codes now. Thanks for this. I would never have known about this.
 
It's better to use SEO friendly URLs. I.e. http://site.com/item/####/friendlyname.html where #### is your id and the name is a sanitized title.

Use a rewrite rule to pass it into the php page.

+1 advice

You could also rewrite everything through a front controller, and then utilise a router to do a lot of the tedious uri parameter checks. (such as the if no item id is specified, 404 or redirect check above).

ie:
/item/{slug} or /item/{id}/{slug} will match and route to an appropriate script, or controller/action
but /item wouldn't, so it 404s.
 
You guys are gonna make his brain explode with htaccess if he is still learning PHP lol, but a simple htaccess file will just change /?id=1 to /1
 
+1 advice

You could also rewrite everything through a front controller, and then utilise a router to do a lot of the tedious uri parameter checks. (such as the if no item id is specified, 404 or redirect check above).

ie:
/item/{slug} or /item/{id}/{slug} will match and route to an appropriate script, or controller/action
but /item wouldn't, so it 404s.

A front controller and a router? The guy is attempting to learn basic URL to method mapping and how everything fits together. Great advice.....
 
OMG, I would deserve hundreds of slaps but I'm new to PHP :P. I'll have to review all my codes now. Thanks for this. I would never have known about this.

Its one the things I use to determine the level of experience a developer has. I use to also make that mistake and had many late nights trying to figure out why my code was not working 100%.

In PHP all that the header command is doing is queueing up the header values to be displayed and it has no understanding / filtering of what your passing.

It also only gets passed to the browser once your script has stopped executing.

Another thing with headers is you can only set them before you display something to the page. So you cannot use header after you started displaying to the screen. The first echo / print / html display command processes the header queue and that is why you cannot queue anymore afterwards.

Eg:
PHP:
<?php
  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
  echo "Moo";
^^^ correct

PHP:
<?php
  echo "Moo";
  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
^^^ incorrect as header will not be used as headers queue has already been processed.

Also if your headers do not work, make sure there are no white spaces before your opening PHP tags, happens a lot

eg

PHP:
 <?php
^ single white space
<?php
No white space
 
Last edited:
A front controller and a router? The guy is attempting to learn basic URL to method mapping and how everything fits together. Great advice.....

Better learn the total picture upfront, even if it is more complex :whistle: The rewrite rule is something like this (this will pass through item id, item title and query string):

PHP:
RewriteRule  ^/item/([0-9]+)/(.*)\.html.*  /item/Item.php?ItemId=$1&ItemTitle=$2&%{QUERY_STRING} [L,PT]
 
Must the page redirect if you have not specified a variable in the link ?

When you navigate to page.php?ID=12345

PHP:
<?php
if ( !empty($_GET["ID"]) ){$ID=$_GET["ID"];} else { $ID='NONE';}

if ($ID=='NONE')
{
//Put code here that displays something in the line of No Item Specified
}
else
{
//Put Code Here that returns the data from the database
}
?>


@cbrunsdonza - I Stand to be corrected (I'm not a pro at this)
But most hosting providers servers are set not allow you to use header to redirect to another page when the page has already returned HTML code.
If you try it normally gives you
PHP:
Warning: Cannot modify header information - headers already sent by Bla bla bla

It works on WAMP though.

Maybe its an attempt to save recources
 
Top
Sign up to the MyBroadband newsletter
X