Home » Php » Creating Dynamic Links – PHP

Creating Dynamic Links – PHP

Posted by: admin February 23, 2020 Leave a comment

Q(Question):

OK heres a newb question and I’m sorry for it, but im getting back into web design after like 5 years off and im stuck even though i should know the problem i cant seem to correct it atm.

What I’m trying to do is create a page that pulls up a list of items in a DB page, lists the items and creates a dynamic link using the item_id making the item_name a link to a more detailed page.

So that when they click the Item_name it uses the item_id to bring up more details just on that one item instead of a complete list of all the items.

Where im stuck is, for some reason and i know this is gonna be an easy fix, that when you view the list of items and mouse over the item_name to see the link the id shows the href, but the id is blank on it and when you click on the link it has a fault im sure which is connected to the id missing.

I’m posting the link to the pages im playing with to get a handle back on php and the source code so that someone can give me a quick hand with this, so i can move on with my life. Appreciate any help given. Thanks..

(note that on this page the links should have a number at the end of the link on the names)

http://www.elitegamers.us/Database_Test/list.php

And this is the details page which has the fault:

http://www.elitegamers.us/Database_Test/view.php?id=

Source Code as follows for list.php


<?php
$con = mysql_connect("localhost","Username","Password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("DB NAME HERE", $con);
$result = mysql_query("SELECT * FROM items");
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Location</th>
</tr>";while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><a href=\"view.php?id=".$myrow[id]."\"> ". $row['item_name'] ." </a></td>";
echo "<td>" . $row['item_loc'] . "</td>";
echo "<TD><a href=\"view.php?id=".$myrow["id"]."\">View</a>";
echo "</tr>";
}
echo "</table>";mysql_close($con);
?>

Source Code for Dynamic page:


<HTML>
<?php
$db = mysql_connect("localhost", "User name", "Password");
mysql_select_db("DB Name Here",$db);
$result = mysql_query("SELECT * FROM items WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
echo "First Name: ".$myrow["item_name"];
echo "<br>Last Name: ".$myrow["item_location"];
?>
</HTML>

Thanks for the help…. Lazoris

A(Answer):

Heya, Lazoris. Welcome to TSDN!

Please use CODE tags when posting source code:

[CODE=php] PHP code goes here.
[/CODE]

[CODE=html] HTML code goes here.
[/CODE]

A(Answer):

oh btw line 21 in the list view.php is redundant was trying the original code over and forgot to erase it before i posted it. so ignore line 21. My question is for line 19 since thats the link that im using for the new page details im trying to do.

A(Answer):

Heya, Lazoris.

Try using $row instead of $myrow.

A(Answer):

ok that fixed the missing id on that page thanks very much got most of the other issues worked out the correct code for line 6 on the view page gets @ in the beginning so it looks like


@$row = mysql_fetch_array($result);

A(Answer):

Funny thing… managed to clear all my errors, but… No info prints out on the view page could i have my echo wrong?


echo "First Name: ".$row["item_name"];
echo "<br>Location Found: ".$row["item_loc"];

Either .$row or what follows. trying to research it, but i noticed most websites references are about other things with no real examples, and others are spam sites lol..

A(Answer):

Heya, Lazoris.

On the view page, you use $myrow instead of $row.

On this line:


$myrow = mysql_fetch_array($result);

Compare with this line in list.php:


while($row = mysql_fetch_array($result))

A(Answer):

Yeah i forgot to mention i fixed the $myrow to $row when i did it to the list.php. Anyways i tried your way if I got it right ….


while($row = mysql_fetch_array($result))
echo "First Name: ".$row[item_name];
echo "<br>Location Found: ".$row[item_loc];

got the same error as before:

mysql_fetch_array(): supplied argument is not a valid

I do not however get that error code when i do:


while(@$row = mysql_fetch_array($result))

or


@$row = mysql_fetch_array($result);

but, when I do not get the error message, the page just echos "first name" & "last name" but no actual db info is put out. Been trying different ways to print out the echo but nothing seems to work..

I tried experimenting with line 5 on the view page


$result = mysql_query("SELECT * FROM items WHERE id=$id",$db);

changing $id to other words, just to see what happens and get the same affect so I assume i did something wrong that the code isnt catching the id field from the db.

I’ve been assuming i got line 5 correct where "items" = the table and "$id" = the id field in the table, and that id = $id is telling the page that the id sent from the previous page " <a href=\"view.php?id=".$row[id]."\">" = $id on the view page.

is it possible that other than making the id the page name that the id isnt being sent to this page for it to query the db for the information?

A(Answer):

Heya, Lazoris.

Prepending a ‘@’ does not ‘fix’ an error; it merely suppresses the reporting of the error message.

Try this:


$result = mysql_query("SELECT * FROM items WHERE id=$id",$db);
echo "SELECT * FROM items WHERE id=$id<br /><br />", mysql_error();

You’ll want to add this code near the top of your code on view.php:


$id = (int) $_GET['id'];

A(Answer):

yeah funny i was just reading on how to pass the get method over to this new page. however when i added your 2 lines of new code it printed out the "SELECT * FROM items WHERE id="

here is the new code: for the details page or view.php:


<?php
$db = mysql_connect("localhost", "", "");
mysql_select_db("",$db);
$result = mysql_query("SELECT * FROM items WHERE id=$id",$db);
echo "SELECT * FROM items WHERE id=$id<br /><br />", mysql_error();
$row = mysql_fetch_array($result);
echo "First Name: ".$row["item_name"];
echo "<br>Last Name: ".$row["item_location"];
?>

and i assumed you meant list.php page for the other piece of code you gave me.
didnt change anything but on the details page (the new pages im trying to make) still got that same error message and a few new ones. here you can see em @

http://www.elitegamers.us/Database_Test/view.php?id=7

gonna leave the page as is for the night and figure it out in the morning lol. 6 hours of this and my head hurts lol. im getting closer i think and i know its just a stupid mistake im probably making somewhere. Gotta love coding lol.

A(Answer):

Heya, Lazoris.

When you put a variable in the URL, PHP auto-assigns it to $_GET.

So you’ll want to put this code at the top of view.php:


$id = (int) $_GET['id'];

A(Answer):

OK. shoot…. now I have to admit it… im a moron and you my friend are a god.. works fine now. guess i was just at it too long and couldnt think straight.

heres the final code to make this dynamic page of tables link to a dynamic page created by using a link in the db to link to more detailed pages.

the main table:


<?php
// change the user_name and password
$con = mysql_connect("localhost","user_name","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// change the database_name
mysql_select_db("db_name", $con);
// change the table_name
$result = mysql_query("SELECT * FROM table_name");
// change the display text in the table below
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Location</th>
</tr>";while($row = mysql_fetch_array($result))
{
echo "<tr>";
// change the view.php to whatever page you will be creating dynamically
// through the link below as well as the row_name that you want the link to be
// from
echo "<td><a href=\"view.php?id=".$row[id]."\"> ". $row['row_name'] ." </a></td>";
// add more rows and change the name of row_name2 to whatever row/rows you
// want to show in the main page
echo "<td>" . $row['row_name2'] . "</td>";
echo "</tr>";
}
echo "</table>";mysql_close($con);
?>

the page that the table links go to, in order to create the new pages of info:


<?php
$id = (int) $_GET['id'];
// change the user_name and password
$db = mysql_connect("localhost", "user_name", "password");
// change the database_name
mysql_select_db("db_name",$db);
$result = mysql_query("SELECT * FROM items WHERE id=$id",$db);
$row = mysql_fetch_array($result);
// make these any row in the table that you want to show for more detail on the
// items in the main tables page
echo "First Name: ".$row["row_name"];
echo "<br>Last Name: ".$row["row_name2"];
?>

Thanks for the help pbmods

A(Answer):

Heya, Lazoris.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime 🙂