Jump to content

Recommended Posts

Well,

 

I have a database with guides on it, sorted by their ID, which is auto increment and the primary key.

 

What I'm trying to do is have a page called guides.php, this will show a table containing the guide names with links to the guides.

 

The links will be guides.php?id=1

ID number according to the ID in the database.

 

The guide page will display the information from the database.

 

 

Example:

 

If a guide is called PHP - Beginner and has an ID on the MySQL database of 5, then I would go to guides.php and click "PHP - Beginner" on the list, this would then take me to guides.php?id=5, this page would show me the guide information from the other fields in the database, instead of the table.

 

Look at this page to understand what I mean more:

http://www.zybez.net/quests.php

 

I just have no idea where to begin as I've never tried to do it before.

 

Any help is greatly appreciated.

Assuming you already have the database setup... here's the php query.

 

<?php
if(isset($_GET['id'])){
  $id = mysql_real_escape_string($_GET['id']);
  $query = "SELECT * FROM `table` WHERE `id` = '$id'";
  $run = mysql_query($query);
  if($run){
    $arr = mysql_fetch_assoc($run);
  }else{
     die("Unable to connect to database.");
  }
}
?>

The code I provided above would allow the url with the $_GET variable to work.  To generate the page that displays the urls with the $_GET vars contained would be something like this

 

<?php

// Collect guides
$query = "SELECT `id`,`guide` FROM `guides`";
$run = mysql_query($query);
if($run){
	while($arr = mysql_fetch_assoc($run)){
		echo <<<HTML
		<a href="guides.php?id={$arr['id']}">{$arr['guide']}</a><br />
HTML;
	}
}else{
	die("Unable to connect to database.");
}
?>

That would be explained in my first post... I'll reiterate in further detail below.

 

<?php

// assuming guide.php?id=2
if(isset($_GET['id'])){

// first grab the id
$id = mysql_real_escape_string($_GET['id']);

// second query for that id
$query = "SELECT `guide` FROM `table` WHERE `id` = '$id'";
$run = mysql_query($query);
if($run){

	// third display the guide
	$arr = mysql_fetch_assoc($run);
	echo $arr['guide'];

}else{
	die("Unable to connect to database.");
}
}
?>

 

Does this answer your question?

Yeah, right now I haven't written a fail-safe in case an id wasn't provided.  I'd probably redirect the user back to the listing (sample below).

 

<?php

// assuming guide.php?id=2
if(isset($_GET['id'])){
   
   // first grab the id
   $id = mysql_real_escape_string($_GET['id']);
   
   // second query for that id
   $query = "SELECT `guide` FROM `table` WHERE `id` = '$id'";
   $run = mysql_query($query);
   if($run){
      
      // third display the guide
      $arr = mysql_fetch_assoc($run);
      echo $arr['guide'];
      
   }else{
      die("Unable to connect to database.");
   }
}else{
// if the headers haven't been set yet you can use
header("Location: listpage.php");

// otherwise use js
echo <<<SCRIPT
<script type="text/javascript">
window.location = 'listpage.php';
</script>
SCRIPT;
}
?>

Ahh I see what you're saying.  If a guide id isn't specified, you want to list them all.  The script below should do the trick.

 

<?php

// assuming guide.php?id=2
if(isset($_GET['id'])){
   
   // first grab the id
   $id = mysql_real_escape_string($_GET['id']);
   
   // second query for that id
   $query = "SELECT `guide` FROM `table` WHERE `id` = '$id'";
   $run = mysql_query($query);
   if($run){
      
      // third display the guide
      $arr = mysql_fetch_assoc($run);
      echo $arr['guide'];
      
   }else{
      die("Unable to connect to database.");
   }
}else{
// Collect guides
   $query = "SELECT `id`,`guide` FROM `guides`";
   $run = mysql_query($query);
   if($run){
      while($arr = mysql_fetch_assoc($run)){
         echo <<<HTML
         <a href="guides.php?id={$arr['id']}">{$arr['guide']}</a><br />
HTML;
      }
   }else{
      die("Unable to connect to database.");
   }
}
?>

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.