0

Been struggling with this problem for a while now and I just can't het my brain to understand it.

I have a very simple website where I can add items in a database. There is a list on index.php where the list is displayed and each item needs a url that directs to a "more information" page.

The "more information" page has to be a dynamic one as there are a lot of items and these items can be added or deletend.

What my code for this section looks like at the moment:

$result_set = mysql_query("SELECT id, name FROM items WHERE id = $item");
while ($item = mysql_fetch_row($result_set)) {
$name = $item['name'];
echo "<a href=\"/items/".$item['1'].".html\">$name</a>";

This results in a link if item 1 = wrench ../items/wrench.html.

But this page obviously doesn't excist. How can I get this to work?

4
  • i guess you need this echo "<a href=\"/items/".$item['name'].".html\">$name</a>"; Commented Nov 14, 2013 at 9:09
  • 2
    I feel like I have to warn you about using mysql_* even if that itself is not the problem here. Commented Nov 14, 2013 at 9:10
  • 2
    You should be using prepared statements, it helps mitigate the risk of SQL injection attacks. It's just as easy, it just seems as though most tutorials online use mysql_x which is depricated. Seriously, do yourself this favor it's no good learning bad habbits early on. See: php.net/pdo and php.net/mysqli Commented Nov 14, 2013 at 9:16
  • Could you EDIT your question to show valid URLs Commented Nov 14, 2013 at 9:32

2 Answers 2

1

You can't have one html page for each item if the items are dynamically added But you can do it this way

 echo '<a href="/items/more_information.php?item_id='.$item['1'].' ">$name</a>';

This way you have only one page who receive as a GET parameter the id of the item you want the description. In the page of more_information.php you just display a text corresponding to the id you received.

Sign up to request clarification or add additional context in comments.

Comments

0

use .htaccess for this. you can add this code

RewriteEngine On
RewriteRule  ^items/(.+)$ items.php?page=$1

In your items.php use $items=$_GET[page]; so you can read what is in url after items/

This is a link where you can find more about RewriteRule's http://httpd.apache.org/docs/current/rewrite/intro.html

8 Comments

I think OP will like this, but I assume it may be over his head. I think the question was geared more towards 'how do i create a dynamic page based on GET parameters' -- The question is worded poorly.
if you wote down than be more specific about question so we can help. did not write to you David Houde :)
I'd like this, but I can't figure out how this works. What is the $1.
$1 is not important so much, David Houde is explain that. on your items.php use $items=$_GET[page]; so you can read what is in url after items/
$1 represents the 'item' -- So a request for /items/wrench.php will really be seen by /items.php?page=wrench -- You will still need to create item.php which will use $_GET['page'] to query the database and pull the additional information.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.