Log in

View Full Version : tables in php with mysql



davidjmorin
12-31-2007, 06:46 PM
i have this code and cannot for the life of me figure out how to make it so that i to only displays 4 pics per row. can someone please help. thanks



<?
if(isset($_GET['id']))
{
include 'config.php';
include 'opendb.php';

$id = $_GET['id'];
$query = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);

header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;

include 'closedb.php';
exit;
}

?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
include 'config.php';
include 'opendb.php';

$query = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
}
else
{
?>
<table border="1">
<?
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<td>
<a href="download.php?id=<?=$id;?>">
<img src="download.php?id=<?=$id;?>" height="150" width="150"><?=$name;?></a>
</td>
<?
}
?>
</table>
<?
}
include 'closedb.php';
?>
</body>
</html>

GhettoT
01-02-2008, 08:26 AM
Without reposting your code, this is what I would do.

At the begging of the script add.


$rowCount = 0;

Then, every time you echo a picture say,


$rowCount ++;
Then in the same place you echo a row say,


if($rowCount >= 4){
</tr></tr> //closing the first row, starting a new one
}


Quick fix, but it should work, good luck!

Leafy
01-02-2008, 08:53 AM
First, an explanation of my changes.

1. Add a doctype
You need a doctype in your documents to tell the web browser what type of document you are giving them. This document looks like HTML 4.0 Transitional, so you must include this doctype. You may find a list of doctypes here (http://www.w3.org/QA/2002/04/valid-dtd-list.html). They must be before the <html> tag.
2. escape your database inputs.
Any user-derived data must go through mysql_real_escape_string to avoid SQL injections (http://en.wikipedia.org/wiki/SQL_injection).


<?
if(isset($_GET['id']))
{
include 'config.php';
include 'opendb.php';

$id = mysql_real_escape_string($_GET['id']);
$query = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);

header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;

include 'closedb.php';
exit;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<div id="container">
<?
include 'config.php';
include 'opendb.php';

$query = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "<p>Database is empty</p>";
}
else
{
?>
<table border="1">
<tr><td>Name</td><td>Preview</td><td>Download</td></tr>
<tr>
<?
$rows = 0;
while(list($id, $name) = mysql_fetch_array($result))
{
list($id,$name) = $arr;
$rows++;
echo (($rows % 4) == 0)?"</tr>":"";
printf("<tr><td>%s</td><td><img src=\"%s\" height=\"150\" width=\"150\" /></td><td><a href=\"download.php?id=%s\">Download</a></td>\n",
$name,
"download.php?id=".$id,
$id
);
}
?>
</tr>
</table>
</div>
<?
}
include 'closedb.php';
?>
</body>
</html>