PDA

View Full Version : referencing array items if exist on different pages one at a time


deafboyzaudio
01-04-2010, 08:24 PM
1) I have a upload form that uploads pics... up to 5 files are uploaded at once

2)The filenames of thumb and fullsize are put into the DB

3)each thumb filename goes into the same array.

4) This array is then put into a session called "uploaded_thumbs_session"... so that the array items can be referenced on subsequent pages.

5)This is where I need some help....

I want a thumbnail to appear of each one, each on a separate form(separate page)... users can enter data about the pic and it gets put into the DB... I can do this all hardcoded, but cant figure it out dynamically.

Basically, i need to check for the existence of a array item and if it exists,
A) use it to provide the location of the pic for the thumb preview,
B) the name of the pic so that the DB can be updated,
C)and make the link at the bottom either refresh the page with the next pic in the array or go to a confirmation screen for all files.

This was the only way I could think of doing this, although I up for suggestions and all help is appreciated.

tux
01-05-2010, 05:24 PM
Could you not just have a table in your DB for images, which could have imageID, filename or path to file and any other info it needs like title etc..

Then a user table with userID, imageID and comment.

This should be enough to make the queries you need to get your application to work.

You can select the image from the imageID and also the comment that a user made from userID and imageID or all comments on an image using just imageID.

It will need some work to get it to do want you want but something along these lines should point you in the right direction.

Whats your PHP skills like?

deafboyzaudio
01-05-2010, 11:31 PM
Hey Tux,
thanks for the reply.
Could you not just have a table in your DB for images, which could have imageID, filename or path to file and any other info it needs like title etc..

I already have a pretty in depth DB set up with this stuff(id, caption, filename, etc.) Username, filename, id, and upload date are added on upload, then it goes to this form, one for each image to add title caption info about vehicle etc...

I believe that all routes eventually lead to having an array with the items recently uploaded by this user... whether its an array of the id's or the filenames, I need a way to differentate this 'group' of uploads from others....

I can picture how the logic might work in my head but not in code... the filenames go into an array so they can be referenced

//query session to get array items....
$_SESSION['uploaded_thumbs_session']

//if items exist, use to display thumb,one at a time each page
if (array_key_exists($uploaded_thumbs_array[0]){
echo img src= "uploads/thumbs/$uploaded_thumbs_array[0]"
}
obviously not correct syntax... thats where you guys come in:mrgreen:
Whats your PHP skills like?They are beginner to moderate... I have 2 books I reference and that with google and here, I can manage to limp through... this is my most advanced page project yet, this one right here.

tux
01-06-2010, 06:18 AM
So am I right in thinking that the user will upload a new image and then add a caption, title to it. Is it only the new image(s) that need the captions etc..

If so, once the user uploads an image and it is added to the DB it will have a unique ID from an auto-increment (imageID). You could then make a query for that image using the imageID which would be unique to that image. Then display it on your form page where the user can add the info which can then be inserted into the DB where needed, again using the imageID. You could also do this at the upload stage in one go.

If other users will be adding info, again just use the unique imageID to select or insert.

Am I on the right lines to what you need?

Regards, Paul

deafboyzaudio
01-06-2010, 07:32 PM
So am I right in thinking that the user will upload a new image and then add a caption, title to it. Is it only the new image(s) that need the captions etc..Yes
You could then make a query for that image using the imageID which would be unique to that image.I need a way to do this dynamically... new id's are always being added. how can i tell my application which image id's to display???? thats my issue...
Then display it on your form page where the user can add the info which can then be inserted into the DB where needed, again using the imageID. You could also do this at the upload stage in one go.I have done this by hardcoding it... but the image id that needs to be displayed and updated is always changing... I need a way for my application to decide which image id's to display/update... dynamically

the only way i know how to do this is by putting an array of the current upload ID's in a session... that way the id's can be referenced by the application enabling the correct images to be displayed/updated one form at a time... does that make sense?

tux
01-06-2010, 10:44 PM
I need a way to do this dynamically... new id's are always being added. how can i tell my application which image id's to display???? thats my issue...

Still not grasping what you are after here. So, a user will upload a new image and caption it. Then another user will visit the site and view the new image(s) that the last user just uploaded, yes?

I have done this by hardcoding it... but the image id that needs to be displayed and updated is always changing... I need a way for my application to decide which image id's to display/update... dynamically

If I am correct in what I have said above you will then query your DB and sort it using.....

SELECT * FROM yourtable ORDER BY imageID DESC

This will will return the images newest first.

Not sure if this is what your after though!!

tux
01-06-2010, 10:46 PM
If this isn't what you are after post your code and a link to your site so I can see what you mean.

deafboyzaudio
01-06-2010, 11:47 PM
SELECT * FROM yourtable ORDER BY imageID DESC...............This will will return the images newest first.


Well I have considered that but I have that 'what if' hanging in the back of my head....

what if 2 users upload photos simultaneously... with a small form for each pic, by the time the user gets to the second pic it might not be the 2nd most recent in the db.

There has to be a more sure fire way to do this... guaranteed with no 'what if's'

I dont have that part on the net yet but you have the idea correct about how its supposed to work

Here it is for anyone thats curious though...
1)The filenames of successful uploads are put into 2 arrays... one for fullsize, one for thumbs.
//loop through fullsize array
foreach ($uploaded_files_array as $item1){

//split filename.extension into "filename" and ".extension"
$item1_chop_block = explode(".",$item1);
$item1_ext = ".$item1_chop_block[1]";
$item1_filename = "$item1_chop_block[0]";
//insert SQL
$query = "INSERT INTO pics (pic_filename, pic_user_name, pic_filename_ext) VALUES('$item1_filename', '$pic_user_name','$item1_ext')";
mysql_select_db($database_db294582132, $db294582132);
mysql_query($query, $db294582132) or die(mysql_error());
}
//loop through thumbs array
foreach ($uploaded_thumbs_array as $item2){
//split t_filename.extension into ".extension" and "t_filename" and "filename"
$item2_chop_block = explode(".",$item2);
$item2_ext = ".$item2_chop_block[1]";
$item2_filename = "$item2_chop_block[0]";
$item2_t_stripped = str_replace('t_','',$item2_filename);
//insert SQL
$query = "UPDATE pics SET thumb_filename = '$item2_filename', thumb_filename_ext = '$item2_ext' WHERE pic_filename = '$item2_t_stripped'";
mysql_select_db($database_db294582132, $db294582132);
mysql_query($query, $db294582132) or die(mysql_error());
}
//make session out of uploaded_thumbs_array
session_start();
$_SESSION['uploaded_thumbs_session'] = $uploaded_thumbs_array;
//redirect

As you can see first a row is made in the DB for the fullsize pic, then that same row is updated with the filename for the thumb....
the thumbs array containing the filenames is put into a session and then taken to the next page.... This is where im stuck, I need a way to call those array items one at a time, page by page. hope that makes it more clear

tux
01-07-2010, 05:35 AM
what if 2 users upload photos simultaneously... with a small form for each pic, by the time the user gets to the second pic it might not be the 2nd most recent in the db.

It would never happen exactly simultaneously. One would be always be entered first and always get its unique imageID.. It doesn't matter if another user uploads an image before the 1st uploads his 2nd as each image will have its own imageID and also its userID.

You would call the users specific images by using userID. So....

SELECT * FROM yourimagestable WHERE userID = '$userID' ORDER BY imageID DESC

.... would only return the images from a specific user. The $userID variable you would get from the session you create when the user logs in.

Is this any more help?

deafboyzaudio
01-07-2010, 06:37 AM
yes that does help...
Thanks for the tip tux, I was thinking too complicated :mrgreen: i didnt think about ordering by username...

I can use the array to count the number of pics uploaded though so I know how many images to go back

just one question though... how would i step through the results one at a time page by page?

tux
01-07-2010, 06:08 PM
You will need some sort of paging built into your page. ie. Next, Previous etc. but in your instance that may be overkill.

But if you just want the user to select their next image then maybe just select the next imageID that is greater than the current imageID. As the user is already looking at the current image you will have the current imageID so....

SELECT * FROM yourimagetable WHERE userID = '$userID' AND imageID > '$imageID' ORDER BY imageID DESC

This is only an idea and I have not tested it but something along those lines should work.

deafboyzaudio
01-07-2010, 08:07 PM
ok i wasnt sure if you can use logic like that in SQL... I will experiment and get back on here with a solution or ?'s I may have. thanks a ton:mrgreen:

deafboyzaudio
01-08-2010, 06:39 AM
ok I seem to be stuck....

here is what i have after the basic image data is inserted into DB

######################
########UPLOAD PAGE#####
######################
/////count the # of uploads completed
$thumb_count = count($uploaded_thumbs_array);

//////define a number to start counting at
$thumb_number = 0;

/////put these values into an assoc. array
$vital_thumb_data = array('thumb_count' => "$thumb_count", 'thumb_number' => "$thumb_number");

/////then put that array into a session for use on other pages.
$_SESSION['uploaded_thumbs_session'] = $vital_thumb_data;

####################################
##Pic Upload Primer(page redirected to after uploads)##
####################################
/////define variables from session for display of dynamic image and dynamic SQL
$username = $_SESSION['MM_Username'];
$thumb_number = $_SESSION['uploaded_thumbs_session']['thumb_number'];
$thumb_count = $_SESSION['uploaded_thumbs_session']['thumb_count'];

//////Run SQL to select one row...
/////NOTICE the $thumb_number, this is used to skip down first 0
////rows then 1, and so on to step down the DB list...
$query = "SELECT * FROM pics WHERE pic_user_name ='$username' ORDER BY pic_id DESC LIMIT $thumb_number, 1";
mysql_select_db($database_db294582132, $db294582132);
$result2 = mysql_query($query, $db294582132) or die(mysql_error());
$row = mysql_fetch_assoc($result2);

///////This is where my thumbnail is to display the pic using $row
////// var. from above
<img src="images/uploads/thumbs/<?php echo
$row['thumb_filename'];?>.jpg" />

All that previous code runs when the page first loads
This is where im having problems at....

I know that I need to add 1 to the count and then update the session...

####all this code is only ran when form submitted....####
///do I have to redefine these variables for use when the form is
///// submitted???
$thumb_filename = $row['thumb_filename'];
$thumb_number = $_SESSION['uploaded_thumbs_session']['thumb_number'];
$thumb_count = $_SESSION['uploaded_thumbs_session']['thumb_count'];

/////add 1 to the var. so next time around the SQL skips the first
////row and starts me off with a new row to work with
$thumb_number = $thumb_number + 1;

/////then put back into array and then session....
/////also is there a better way to update an array/session or
/////do i have to remake it?
$vital_thumb_data = array('thumb_count' => "$thumb_count",
'thumb_number' => "$thumb_number");
$_SESSION['uploaded_thumbs_session'] = $vital_thumb_data;

/////finally update the image data in DB using
///// $row['thumb_filename'] from earlier

$query = "UPDATE pics SET blah blah blah
WHERE thumb_filename = '$thumb_filename'";


now that last part im not even sure if everything there should be run when the form is submitted or when...

the 1st pic after upload shows up and the insert into DB works... but when the submit button is hit the page refreshes with the same pic... data is entered and then on the third page the 2nd image shows up

Viewing the DB I noticed the first record is good...


Upon inspecting the array on the second page I noticed that the thumb_number wasnt changing to 1 until the 3rd page, so it lags one.... so somethin is wrong but not sure how to fix it.

i can guess that it has something to do with the $thumb_number not adding 1 until after the first refresh... how can i fix this?

tux
01-08-2010, 05:46 PM
I'm still not sure what your overall objective is but it does seem to me that you are over complicating your whole strategy.

A few questions.

What is your site for? Why do you need to know the thumb count? Why do you need the session stuff.

I need to get my head round your objective so I can help better.

deafboyzaudio
01-11-2010, 08:48 PM
What is your site for?
its a community of car audio enthusiasts where users can upload pictures/video of their car audio systems for other users to see/talk about.
Why do you need to know the thumb count? Using your suggestion of sorting the db by user name and then taking the most recent record to update, that requires knowing how many records to go back.

Say for example user X uploads 2 pics on tuesday, and puts info with them via my form.....
Then 2 more on Friday, I only need to update the 2 most recent records b/c the others have already been updated.

The DB is sorted by username DESC... so all records for that user are returned... using the thumb count i can only update the most recent ones as I know how many of those total records are for this upload only. does that make sense?
Why do you need the session stuff.
The session is to hold the thumb count and the number of the current thumb. EACH PAGE needs a thumb and a different update statement.
by putting this changing variable into a session it can be used page to page, incrementing by one each time to skip down the rows one by one. I hope this helps.... '

tux
01-11-2010, 09:31 PM
Right I think I am starting to get it but it does seem you are over complicating your logic.

When a user uploads an image they will be adding their caption/comment to the image as well, yes! So, the job is done. If they upload another the next day, they will add a caption/comment with that image too, job done.

If they just upload an image and want to add a caption later you would just add functionality for them to select their image before updating their info.

They will be able to select any of their images and edit their caption/comments at any time.

Am I on th right track?

deafboyzaudio
01-12-2010, 02:19 AM
When a user uploads an image they will be adding their caption/comment to the image as well, yes! So, the job is done.


it might be that easy for a single image... my code works for a single image, but not for 2 or more

Most my users will be uploading multiple images at once and I want the entry of captions etc to pushed on them right after upload... Not in a form they can come back to later and pick a pic....

my users are lazy... the pics will be captionsless forever if i dont get the info from them right away.

it might be complicated but its the only way i can see of doing it... and still accomplishing my goal

if you take a look at my post with all the code, the questions I have are at the bottom

tux
01-12-2010, 05:41 AM
Can you not build the the form for the captions into the same form as the image upload and put some conditions in it so they have to add the caption before it will upload.

if($this && $that is true){
then do your stuff here
}else{ echo "you must fill in all forms!"; }

deafboyzaudio
01-12-2010, 03:10 PM
i could but i believe that that would make that page too big to fit on one screen..... I dont want it to seem overwhelming.

besides, I think this way provides a real seamless, pleasant user experience... i like the way it flows and collects info.

I was reading my book last night and i think i may know how to fix my issues with this script now... ill give it a go today and hope for the best...

deafboyzaudio
01-13-2010, 12:37 AM
$username = $_SESSION['MM_Username'];
$n_o_t = $_SESSION['uploaded_thumbs_session']['n_o_t'];
$i = $_SESSION['uploaded_thumbs_session']['i'];
$p = $_SESSION['uploaded_thumbs_session']['p'];
//select users uploads
$query2 = "SELECT * FROM pics WHERE pic_user_name ='$username' ORDER BY pic_id DESC LIMIT $i, 1";
mysql_select_db($database_db294582132, $db294582132);
$result2 = mysql_query($query2, $db294582132) or die(mysql_error());
$row2 = mysql_fetch_assoc($result2);
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
//set post variables and go about with form action
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "formFeedback")) {
//add one to count of pic and
$i++;
$p++;
$vital_thumb_data = array('n_o_t' => "$n_o_t", 'i' => "$i", 'p' => "$p");
//make session out of uploaded_thumbs_array
$_SESSION['uploaded_thumbs_session'] = $vital_thumb_data;
//name form variables
$pic_title = $_POST['title'];
$pic_caption = $_POST['caption'];
$pic_vehicle_year = $_POST['vehicle_year'];
$pic_vehicle_make = $_POST['make'];
$pic_vehicle_model = $_POST['model'];
$pic_vehicle_setup = $_POST['setup'];
$pic_id = $row2['pic_id'];
$n_o_t = $_SESSION['uploaded_thumbs_session']['n_o_t'];
if($pic_vehicle_year == '???'){$pic_vehicle_year = NULL;}
//update SQL for adding extra info to pics
$query1 = "UPDATE pics SET pic_title = '$pic_title', pic_caption = '$pic_caption', pic_vehicle_year = '$pic_vehicle_year', pic_vehicle_make = '$pic_vehicle_make', pic_vehicle_model = '$pic_vehicle_model', pic_vehicle_setup = '$pic_vehicle_setup' WHERE pic_id = '$pic_id'";
mysql_select_db($database_db294582132, $db294582132);
$Result1 = mysql_query($query1, $db294582132) or die(mysql_error());
}
//do SQL for initial pic.
$query3 = "SELECT * FROM pics WHERE pic_user_name ='$username' ORDER BY pic_id DESC LIMIT $p, 1";
mysql_select_db($database_db294582132, $db294582132);
$result3 = mysql_query($query3, $db294582132) or die(mysql_error());
$row3 = mysql_fetch_assoc($result3);
//redirect if $n_o_t has been reached
if($i >= $n_o_t){
$WA_Redirect_URL = "thankYou.php";
$WA_Redirect_KeepQS = false;
if ($WA_Redirect_URL != "") {
if ($WA_Redirect_KeepQS && $WA_Redirect_URL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "") {
$WA_Redirect_URL .= ((strpos($WA_Redirect_URL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
}
header("Location: ".$WA_Redirect_URL);
}
}
?>
FInally got it workin how i want.... took me a lil longer than i wanted it to but at least its done.

Only problem is that if you go to hit the back button say before the 3rd pic it enters the data for the first pic into the third and then skips to the fourth....

need to disable the back button, and make an edit page for stuff like that....thats my next step.

Thanks for your help man and if you want to see it in action its at www.deafboyzaudio.com/upload.php (http://www.deafboyzaudio.com/upload.php)

I still have to incorporate the captions etc into the display of the pics so thats next too...

youll have to sign up and car audio pics are preferred but i guess i can let a few slide by if you dont got any just so you can see how it works. 8)