Jump to content

[SOLVED] Making Link from Database Table


jeffmthomas

Recommended Posts

Hello,

Long time reader, first time poster.  I have a little script that I am working on to basically pull download file information from a database and display a link to the download page on the form.

Here is the SQL Table that I am working with:
[code]CREATE TABLE downloads (
  downloads_id int NOT NULL auto_increment,
  downloads_name varchar(64),
  downloads_description text,
  downloads_image varchar(208),
  downloads_url varchar(255) default NULL,
  PRIMARY KEY (downloads_id)
);[/code]

Here is the PHP Code that I am working with:
[code]<?
/* Include Files *********************/
session_start();
include("database.php");
include("login.php");
/*************************************/
?>
<html>
<head>
<title>PG Mobile Site</title>
</head>
<body>
<?php require('header.php'); ?>
<?
if($logged_in){
$result = mysql_query( "SELECT downloads_name FROM downloads" )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
while ($get_info = mysql_fetch_row($result)){
foreach ($get_info as $field)
?>

<center>There are <?php echo $num_rows; ?> downloads.
<table width="250" border="0">
<tr align="center">
<td><font size="4"><?php echo $field; ?></font></td>
</tr>
</table>
</center>
<?
}
}else{
?>
<p align="center">
Bla Bla Bla you need to register and login before you can download.
<? displayLogin(); ?>
</p>
<?
}
?>
<?php require('footer.php'); ?>
</body>
</html>[/code]

When I print the <?php echo $field; ?>, which is the name of the download into the table, I want to make it a link that will link to the corresponding download ID in the same database table ... i.e. http://www.mysite.com/downloadlist.php?downloads_id=561.  How would this be done as I am still in the learning process of PHP.

Thanks in Advance,

Jeff
Link to comment
Share on other sites

[code]
<?php
/* Include Files *********************/
session_start();
include("database.php");
include("login.php");
/*************************************/
?>
<html>
<head>
<title>PG Mobile Site</title>
</head>
<body>
<?php require('header.php');
if($logged_in){
$result = mysql_query( "SELECT downloads_name FROM downloads" )
or die("SELECT Error: ".mysql_error()); 
?>
<center>There are <?php echo mysql_num_rows($result) ?> downloads.
<table width="250" border="0">
<?php
while ($get_info = mysql_fetch_array($result)){
echo '<tr align="center">'
echo '<td><font size="4">';
echo '<a href="page.php?id=' . $get_info['downloads_id'] . '">';
echo $get_info['downloads_name'];
echo '</a></font></td></tr>';
}
?>
</table>
</center>
<?php
}else{
?>
<p align="center">
Bla Bla Bla you need to register and login before you can download.
<?php displayLogin(); ?>
</p>
<?php
}
require('footer.php');
?>
</body>
</html>[/code]

Basicly mysql_fetch_array will make an array of the current row, with the column name as the key. Each time it steps through the while loop, it gets the next row.

Look through the code a couple of times. I cleaned it up in several spots.


[size=7pt]Yes, I know I didn't need those echos. But I wanted to make easy to understand code.[/size]
Link to comment
Share on other sites

Hello,

Thank you very much for that.  I now understand it much better, and I do understand that the echo was not necessary.  I also know that I do not need to break in and out of the PHP as much.  Thank you very much once again for helping me understand PHP better.  YOu can only learn by making mistakes.

I do have one last question though since I really dont feel like spending hours searching through my books and google to figure it out.

Taking the above mentioned code.  Say for example I have a downloads_cat option in the database table.  How would I go about having it show only the rows that are in cat 1, or cat 5 for example.

Thank a Million,

Jeff
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.