View Full Version : Method or way to use a database instead of a directory?
student101
12-06-2009, 11:04 PM
Looking for a method or way to code this to use a database instead of a directory?
function
returnimages($dirname="uploads") {
$pattern="\.(jpg|jpeg|png|gif|bmp)$";
$files = array();
$i = 0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){
if($i>0){
echo ",";
}
echo("'$dirname/$file'\n");
$i++;
}
}
closedir($handle);
}
return($files);
}
returnimages();
Something like this??
$file = $row['image'];
$dirname="uploads";
$i = 0;
while ($i <= 10) {
if($i>0){
echo ",";
}
// how to loop next image here?
echo("'$dirname/$file'\n");
$i++;
}
bluewalrus
12-07-2009, 12:52 AM
What kind of database are you using? Can you tell us what the column names are you're using?
student101
12-07-2009, 06:17 AM
What kind of database are you using? Can you tell us what the column names are you're using?
Sorry totally forgot about that, it's MySQL.
Album:
CREATE TABLE `album_alb` (
`id_alb` INT(11) NOT NULL AUTO_INCREMENT,
`title_alb` VARCHAR(100) NOT NULL,
`description_alb` VARCHAR(255) NOT NULL,
`thumb` VARCHAR(255) NULL DEFAULT NULL,
`imagename` VARCHAR(255) NULL DEFAULT NULL,
`date_alb` VARCHAR(30) NULL DEFAULT NULL,
`showhome` VARCHAR(1) NULL DEFAULT NULL,
PRIMARY KEY (`id_alb`),
INDEX `title_alb` (`title_alb`)
)
COLLATE=utf8_general_ci
ENGINE=InnoDB
ROW_FORMAT=COMPACT
AUTO_INCREMENT=41
AVG_ROW_LENGTH=2048
The albums images:
CREATE TABLE `album_img` (
`id_img` INT(11) NOT NULL AUTO_INCREMENT,
`idalb_img` INT(11) NOT NULL DEFAULT '0',
`thmimage` VARCHAR(100) NULL DEFAULT NULL,
`lrgimage` VARCHAR(100) NULL DEFAULT NULL,
`description_img` VARCHAR(50) NOT NULL,
`date_img` VARCHAR(30) NULL DEFAULT NULL,
`showhome` VARCHAR(1) NULL DEFAULT NULL,
PRIMARY KEY (`id_img`),
INDEX `idalb_img` (`idalb_img`),
CONSTRAINT `album_img_ibfk_1` FOREIGN KEY (`idalb_img`) REFERENCES `album_alb` (`id_alb`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE=utf8_general_ci
ENGINE=InnoDB
ROW_FORMAT=COMPACT
AUTO_INCREMENT=40
AVG_ROW_LENGTH=2048
bluewalrus
12-08-2009, 03:07 AM
Something like this but I don't use mysql I use mssql so will need some altering, also not sure the relation between the two tables you've posted the code for. This code will pull names, locations, captions, the id, and display all of them as images if you want to limit it to a select number you can use top 10 or something. There's a lot of ways you could go...
<?php
$tsql = "Select ID, PHOTO, CAPTION, LOCATION From photo_info";
include 'DBConnection.php';
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false) {
echo "Error in statement preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
$increase = "0";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<body>
<?php
while (sqlsrv_fetch( $stmt )) {
$id[] = sqlsrv_get_field( $stmt, 0);
$photo[] = sqlsrv_get_field( $stmt, 1, SQLSRV_PHPTYPE_STRING( SQLSRV_ENC_CHAR));
$caption[] = sqlsrv_get_field( $stmt, 2, SQLSRV_PHPTYPE_STRING( SQLSRV_ENC_CHAR));
$location[] = sqlsrv_get_field( $stmt, 3, SQLSRV_PHPTYPE_STRING( SQLSRV_ENC_CHAR));
echo "<img src=\"$location[$increase]/$photo[$increase]\" alt=\"$caption[$increase]\" /> Figure # $id[$increase] <br />\n";
$increase++;
}
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
</body>
</html>
student101
12-08-2009, 05:44 AM
Thank you.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.