TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Storing multiple file paths inside a database (http://www.talkphp.com/general/2241-storing-multiple-file-paths-inside-database.html)

Orc 02-13-2008 04:10 AM

Storing multiple file paths inside a database
 
How do I store multiple file paths or files inside the database, for them to be in all in one id?

Say if I wanted to get the id of a file, but there were multiple files, how would I put them all together to one id? :S

Or say I had two tables, one for title, description, and one for the id and files. How would I combine these?

Andrew 02-13-2008 04:51 AM

Instead of just using the ID field, you can create a row in the table named 'files', and you could list them off, separating them by a semicolon and storing them that way. (You can then use a function like explode() to get the into an array for any manipulation.)

Orc 02-13-2008 04:53 AM

Quote:

Originally Posted by Andrew (Post 10674)
Instead of just using the ID field, you can create a row in the table named 'files', and you could list them off, separating them by a semicolon and storing them that way. (You can then use a function like explode() to get the into an array for any manipulation.)

list them off? What do you mean?

xenon 02-13-2008 09:55 PM

He means serialization. Something like:

Code:

path1|path2|path3
And then explode by the |.

Orc 02-13-2008 10:00 PM

Quote:

Originally Posted by xenon (Post 10693)
He means serialization. Something like:

Code:

path1|path2|path3
And then explode by the |.

But I want to output those files paths. Then I would have to do a pattern match, cause it's a path.

Alan @ CIT 02-14-2008 01:02 AM

Using the examples above, you'd end up with an array of the file paths that you could pattern match / output as you wished.

For example, say your files field in your database contained:

Code:

/home/www/images/file1.png|/home/www/images/file2.jpg|/home/www/images/file3.gif
You would then select that field from your database and use something like:

PHP Code:

$filePaths explode('|'$result['filepaths']); 

The $filePaths variable would then contain an array that looked something like:

PHP Code:

// [0] => '/home/www/images/file1.png'
// [1] => '/home/www/images/file2.jpg'
// [2] => '/home/www/images/file3.gif' 

Which you could then manipulate as you wished.

To re-serialize the data, just use the implode() function.

Alan

Orc 02-14-2008 01:05 AM

Quote:

Originally Posted by Alan @ CIT (Post 10720)
Using the examples above, you'd end up with an array of the file paths that you could pattern match / output as you wished.

For example, say your files field in your database contained:

Code:

/home/www/images/file1.png|/home/www/images/file2.jpg|/home/www/images/file3.gif
You would then select that field from your database and use something like:

PHP Code:

$filePaths explode('|'$result['filepaths']); 

The $filePaths variable would then contain an array that looked something like:

PHP Code:

// [0] => '/home/www/images/file1.png'
// [1] => '/home/www/images/file2.jpg'
// [2] => '/home/www/images/file3.gif' 

Which you could then manipulate as you wished.

To re-serialize the data, just use the implode() function.

Alan

Oh thanks! I've never really tried explode, so I wouldn't know if that happened or not. I just thought when it exploded it did this:
PHP Code:

[0] => '/home/www/images/file1.png/home/www/images/file2.jpg/home/www/images/file3.gif' 

Thanks for clearing that up :D

EyeDentify 02-14-2008 10:00 AM

Do you at all look in the one place where info are to be found:

PHP: Hypertext Preprocessor

?

If not you should really start.
Itīs a great source of information about various functions and so on about PHP.

Good luck.
/EyeDentify

Village Idiot 02-14-2008 03:05 PM

Better to use a relational table or three fields on the table then explode it like that. It's never a good idea to store data like that. It's also bad for this purpose because | can go into a filename.In the unlikely event that someone sticks that in there, you have a problem on your hands.

Nor 02-14-2008 04:19 PM

Quote:

Originally Posted by EyeDentify (Post 10743)
Do you at all look in the one place where info are to be found:

PHP: Hypertext Preprocessor

?

If not you should really start.
Itīs a great source of information about various functions and so on about PHP.

Good luck.
/EyeDentify


Thats just ignorant...

anyways search up:

PHP: serialize - Manual could be helpful in your case.

sarmenhb 02-17-2008 04:29 AM

try trowing all the paths into a variable like

Code:


$variable = $path[1]."|".$path[2]."|".$path[3];

$query = "INSERT INTO somename(id,path) VALUES(null,'$path')";
$save = mysql_query($query);

your table structure should be

Code:

create table somename(id NOT NULL ,path mediumblob)
ENGiNE = myisam (or whatever u choose);


then you can do like

Code:

$query = "SELECT path FROM somename";
$output = mysql_query($query);

if(mysql_num_rows($output)) {


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

$data = $row['path'];
}

for($i=0;$i<count(data);$i++) {

echo $data[$i]."<br>";
}
}

let me know if this is what you needed, i got cared away :p


All times are GMT. The time now is 05:47 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0