Jump to content

Find the Integer!


MinusMyThoughts

Recommended Posts

i'm having an issue with a photo upload/rename. the photos get a sequential name (AlbumOne01.jpg, AlbumOne02.jpg, etc.) when the user uploads them. this is determined by a script that counts the number of rows and adds one to the returned value......when i delete a photo, that leaves me with the last photo having a value (say, AlbumOne13.jpg) while my MySQL row query will only return 12 rows, meaning the next photo will upload as "AlbumOne13.jpg" as well. and this is, of course, bad......what i'm thinking is going to be the solution is to parse the name of the last uploaded photo for its integer value and adding one to THAT, keeping my numbers running smoothly......my problem is, i don't know how to grab that integer out of the name......which function will get "13" from "AlbumOne13.jpg"?...thanks for any help!love,jason

Link to comment
Share on other sites

Your easiest solution would be to create a column named "id" (or something like that) and make it: key/main column, the make it an auto_increment. That way you can just search for the largest id out of the database and add one to that, then it will do what you want.

Link to comment
Share on other sites

Your easiest solution would be to create a column named "id" (or something like that) and make it: key/main column, the make it an auto_increment. That way you can just search for the largest id out of the database and add one to that, then it will do what you want.
...i would, except ALL photos are stored in this folder (a user can create multiple albums), and i'd like to index my photos sequentially by album whenever possible......i'm going to use the substr() function to grab the two numbers (since my file extensions are always four characters - .jpg, .gif - i'm just going to use "$int = substr($photo, -6, 2);" and pull that out. will my script treat $int as a number?love,jason
Link to comment
Share on other sites

here's a solution i've come to. if someone's got a smarter way to do this, i'm all ears...

	  $query = 'SELECT * FROM aKa_photo WHERE albumID="'		. $_POST['albumID']		. '" ORDER BY photoID DESC LIMIT 1';	  $result = mysql_query($query);	  $row = mysql_fetch_array($result);	  $i = substr($row['photoName'],-6,2) + 1;	  if($i <= 9)		{		$i = '0' . $i;		}

love,jason

Link to comment
Share on other sites

I would suggest regular expressions.This regular expression would match the numbers in your filename:/[A-Za-z]+(\d+)\.jpg/I don't know how to do this in PHP, but in javascript, it'd look like this:

var filename = "AlbumOne15.jpg";var number = filename.match(/[A-Za-z]+(\d+)\.jpg/)[1];alert(number); // alerts 15;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...