Jump to content

[SOLVED] Dynamic links


porta325

Recommended Posts

Hey, i'm sure it looks childish for you but i'm in the beginning and i couldn't find something i could understand on this topic anywhere.The ideea is that i want to make some kind of directory. I have the code that lists entries from a database but i don't know how to make all those entries links that point to other pages.

Basicaly, i need every entry(category) to point to a new page that will contain data about that category.

 

<?php

//connect

mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("directory") or die(mysql_error());

 

//select table

$result = mysql_query ("SELECT * FROM countries");

 

//get entries

$row = mysql_fetch_array ($result);

 

//print result

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

echo $row ['Country']." <br>";

}

?>

 

 

This is where i am and where i am stuck.All this part works just fine.I have an Id assigned to every entry so if you can help me move on i would apreciate. I just need to know what other files i need to create and how the href tag should look like.

Link to comment
Share on other sites

<?php
//connect
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("directory") or die(mysql_error());

//select table
$result = mysql_query ("SELECT * FROM countries");

//get entries
$row = mysql_fetch_array ($result);

//print result
while ($row = mysql_fetch_array ($result)){
echo "<a href=\"showlinks.php?id={$row["id"]}\">{$row["name"]}</a><br />";
";
}
?>

 

Assuming you have a "name" field. Then to show them in showlinks.php, use $_GET["id"] in your query :)

Link to comment
Share on other sites

Assuming you have a "name" field. Then to show them in showlinks.php, use $_GET["id"] in your query Smiley

 

The name field is "Country". I'm not sure though how to use $_GET["id"] in my query and i'm not sure what showlinks.php should contain.

Link to comment
Share on other sites

showlinks.php demo:

 

<?php

if(is_numeric($_GET["id"]) {
$id = $_GET["id"];
}

if(!isset($id)) {
die("Invalid ID");
}

$query = mysql_query("SELECT * FROM table WHERE categoryid = '{$id}'") or die(mysql_error());

while($row = mysql_fetch_assoc($query)) {
// Output...
}

?>

Link to comment
Share on other sites

<?php

 

//connect

mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("directory") or die(mysql_error());

 

if(is_numeric($_GET["id"]) {

$id = $_GET["id"];

}

 

if(!isset($id)) {

die("Invalid ID");

}

 

$query = mysql_query("SELECT * FROM countries WHERE Country = '{$id}'") or die(mysql_error());

 

while($row = mysql_fetch_assoc($query)) {

// Output...

}

 

?>

 

Index works great, i still have a problem with

if(is_numeric($_GET["id"]) {

$id = $_GET["id"];

}

 

if(!isset($id)) {

die("Invalid ID");

}

I get an error and i don't know why, i'm not familiar with that function.

Link to comment
Share on other sites

Index.php

<?php

//connect

mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("directory") or die(mysql_error());

 

//select table

$result = mysql_query ("SELECT * FROM countries");

 

//get entries

$row = mysql_fetch_array ($result);

 

//print result

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

$id=$row["Id"];

$status=$row["Status"];

$country=$row["Country"];

 

echo "<a href=\"showlinks.php?id={$id}\">{$country}</a><br />";

 

}

?>

 

 

Showlinks.php

 

 

<?php

 

//connect

mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("directory") or die(mysql_error());

 

 

if(!isset($id)) {

die("Invalid ID");

}

 

$id = $_GET["id"];

 

$query = mysql_query("SELECT * FROM links WHERE Id = '{$id}'") or die(mysql_error());

 

while($row = mysql_fetch_assoc($query)) {

// Output...

$id1=$row["id"];

echo "$id1";

}

 

?>

 

I'm not sure why it says INVALID ID, as i see it the id's from both tables are connected right. Any ideeas ?

Link to comment
Share on other sites

Index.php

<?php
//connect
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("directory") or die(mysql_error());

//select table
$result = mysql_query ("SELECT * FROM countries");

//get entries
//$row = mysql_fetch_array ($result); <--- remove this line

//print result
while ($row = mysql_fetch_array ($result)){
$id=$row["Id"];
$status=$row["Status"];
$country=$row["Country"];

echo "<a href=\"showlinks.php?id={$id}\">{$country}[/url]
";

}
?>

Showlinks.php

<?php

//connect
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("directory") or die(mysql_error());
$id = $_GET['id']; // <---- add this line

if(!isset($id)) {
die("Invalid ID");
}

$id = $_GET["id"];

$query = mysql_query("SELECT * FROM links WHERE Id = '{$id}'") or die(mysql_error());

while($row = mysql_fetch_assoc($query)) {
// Output...
$id1=$row["id"];
echo "$id1";
}

?>

Link to comment
Share on other sites

Thx alot, solved this one, now i'm fighting with another.I can't asociate a column from first table with one from the second.Meaning that every category in first column has an id associated, id that i have to associate with entries from the second table that corespond to the categories from the first table but witch are positioned random.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.