/    Sign up×
Community /Pin to ProfileBookmark

Passing "stuff" between pages

Ok heres wot i’m trying to do starting from the top – I have an article upload feature on my site which i wish to add images to. Basically when you add an article using the form you have a button that you click if you have images relating to the article to upload. This button then opens a small ‘pop-up’ window which has an image add form. With this form you browse the loacl system for the image you want to add, give a description, then upload. You then go back to the main form to finsh it off. As not all articles will have their own pictures i came up with the following idea (all articles are store in a datbase by the way):

First you have the articles table in the database which has article_id (set to auto_increment), article_title, article_content, and then a images field which can be set to either 0 (no images for article) or 1 (has images). This is so that when an article is being loaded the script will check the images field to see if the article has images or not, and if it does opens the article in a template which has a image thumbnail on the right which can be clicked to open a gallery of all images related to the article.

Second their is the images table. This has image_id (set to auto_increment), image_path (the actual path to the image to be used to display it), image_desription, and article_id (which article it relates to). So when the image template is opened it querys the database for images with article_id’s that match the article that is being loaded and places a cutdown version (probably just set image height, width to thumbnail size) of the first image in the set place. Then obviously the image can be clicked for full size and this allows browsing of the other related images.

Right heres my question….how do i do all this?

Only kidding….my real question is that i’m using a button with “onClick open the add image pop up window” – how do i pass the article_id to the new pop up window to be added to the images table? Just to clarify my question, When you’re creating the article you don’t know the article_id as it is set by the database (athough it is always going to be article_id of the last entered +1) so how do you get this number and send it to the new window (when the add image button is pressed) with out submiting the form that the buttom is in? I’ve got the image adding sorted all i need to do is complete the updating of the database but this little problem is standing in my way :mad:.

Also what do you think of my system in general? Is their a better way?

I’ve got confussed just writing all this (and i know what i’m on about) so god knows how much sense it’ll make to you guys that don’t know whats going on in my head? ?

Cheers,
Durbs

to post a comment
PHP

28 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 24.2004 — Simplest way to pass info would be to add a key=value pair to the end of the URL being called for the image upload page:
[code=php]"path/some_page.php?article_id=$article_id"[/code]
$article_id would then be available in the some_page.php file.

A more elegant solution would be to use [url=http://us2.php.net/manual/en/ref.session.php]sessions and session variables[/url].
Copy linkTweet thisAlerts:
@DurbsauthorAug 24.2004 — A more elegant solution would be to use sessions and session variables.[/QUOTE] You couldn't clarify that a little - i've had a brief look at that link but there's hundreds of pages worth info to take in...

Do you mean start a session when the article submit page is loaded that stores (in a cookie) all the infomation about the article (i.e. the article_id) that can be accessed by any page as long as the session is open, then close the session when the final submit button is pressed (deleting the cookie)?

How do i get the next auto number for the mySQL database - in phpmyadmin it tells me, is it a simple case of taking the last one entered and adding 1 to it?

Cheers,

Durbs
Copy linkTweet thisAlerts:
@theuedimasterAug 24.2004 — well if you are confused about sessions, at the first page you do this(before html tag)

session start();

$SESSION['variable'] = $dude;

//Do this however many times, for however many variables you need


Then on the pop up window put:

session start();

$dude2 = $SESSION['variable'];

Hope that helps
Copy linkTweet thisAlerts:
@solavarAug 24.2004 — [i]Originally posted by theuedimaster [/i]

well if you are confused about sessions, at the first page you do this(before html tag)

session start();

$SESSION['variable'] = $dude;

//Do this however many times, for however many variables you need


Then on the pop up window put:

session start();

$dude2 = $SESSION['variable'];

Hope that helps [/QUOTE]




NOTE:

[code=php]

// This is WRONG:
$SESSION['dude'] = $dude;


// This is RIGHT:
$_SESSION['dude'] = $dude;

[/code]
Copy linkTweet thisAlerts:
@NogDogAug 24.2004 — Just thinking, wouldn't it be simpler to use a [b]<input type=file name=image>[/b] tag in your form, as opposed to creating a separate screen?
Copy linkTweet thisAlerts:
@DurbsauthorAug 24.2004 — Cheers for the responses guys. I don;t think i'm gonna use sessions as it seams a little over complecated for what i want to do. The reason that i wanted to have a pop-up window is.....well....i don't really know. Guess it's just the way i saw it happening in my head. Also wot i want to do once i get this working is to expand it and make it a bit more like when you attach files in hotmail. When your writing the article and you decide you wish to add an image to it you click the add image button then you go to the add image page where you add some images, not just one per click of the add image button on the main form, but several before heaing back to it. When you add these i want the image information to be shown in a window (like in hotmail) so you can see what you are about to add to the article. I would also (eventually) like to be able to select the image information and choose to delete the image if required. Any ideas on this?

Cheers,

Durbs

edit:

One other quick question (i know it's on mySQL but i didn't see the point of starting a in thread for this little question) If you have 6 items in a table, each being assigned a ID number by auto_increment (so the id's will be 1-6) and you delete item number 4, does the auto_increment give the next new entry 4 (to fill in the gap) or 7 (to continue the trend? Cheers again
Copy linkTweet thisAlerts:
@MstrBobAug 24.2004 — I don't know, if you want to go with the pop-up (If I'm visualizing this correctly) it can very easily be done with sessions. You have your form page for the article and on that page, a button that calls a pop-up. This pop-up starts off like this:

[code=php]
<?PHP
session_start();
if(isset($_FILES['userfile']['name']))
{
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . $_FILES['userfile']['name']
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)
$_SESSION['img_name']==$_FILES['userfile']['name'];
} else {
?>
... html here...
<form enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<fieldset><lable for="img">Image:</label><input type="file" name="img">
<button type="submit">Add Image</button>
</form>
<? } ?>
[/code]


And then your main form handler starts off with session_start() and can access the image name.
Copy linkTweet thisAlerts:
@DurbsauthorAug 24.2004 — Thats a bit back to front from what i want to do but has cleared up how to use seessions and session variables a great deal. I only need to carry one $variable accross to the pop-up in your masterful opion would you suggest using sessions or the URL method?

Cheers MstrBob, you're like my very own Master of php named Robert.

Durbs
Copy linkTweet thisAlerts:
@MstrBobAug 25.2004 — It depends on the variable you're trying to pass. If this is a number variable, then I see no problem. But text gets dicey, and I'd say that you'd be best off using sessions. So if you have $var=3 and want to pass it to popup, then:

<?PHP $var=3; ?>

<a href="popup.php?var=<?=$var;?>" onclick="window.open('popup.php?var=<?=$var;?>');return false">Add Image</a>

And in terms of auto_incrament, if you have 7 id's (starting with 1), and you delete 4, then the next one you add will be 8. In terms of if you delete number 7, what the next number will be, I believe it will be 7, but I'm not positive on that.
Copy linkTweet thisAlerts:
@DurbsauthorAug 25.2004 — Cheers, thats what i thought. I wanted to predict the next entry before it was entered by simply adding 1 to the last article_id and didn't want to be caught out when deleting articles.

I might use sessions after all as i think i might want to pass something back to the orginal page where the pop up was created after the pop up's form is submitted. Will the original page be able to read session variables passed back without being reloaded? I only ask because, firstly - i want that variable written to the table along with the aricle itself when it's finally submitted, and secondly - i'm a novice and don't know the answer...

Cheers,

Durbs
Copy linkTweet thisAlerts:
@MstrBobAug 25.2004 — The form page won't be able to access the Session variable, when when the form is submitted, that Session variable is avaible.
Copy linkTweet thisAlerts:
@DurbsauthorAug 25.2004 — So thats not possible then, bugger. Oh well i've got another idea as back up.

Right a different question now, When i was testing what i've created i decided to use a pic of my m8s pet camel. When i uploaded the image (which was pretty big check it here:[URL=http://www.ugmfc.com/article_images/img412bde6d25405.jpg]The Camel[/URL] (have i spelt camel right?)) the form just sat there after i pressed the upload button (it was uploading the maHUsive file) with no real indication that it was doing nething, which may, ok WILL, make my dim-witted users start clicking buttons adnclosing things. Is there any way of getting an indication of when it's uploading and when it's finished so that i could send the users to a page saying "Uploadin' please wait" and when a finished signal is sent redirect them to add another?

Durbs.
Copy linkTweet thisAlerts:
@MstrBobAug 25.2004 — Well, not with PHP. Maybe with an onclick event by Javascript, but I'm not sure. But there's something else you should know. The PHP ini file has a set limit to the filesize of an image. If you go over that limit, it won't work. But you'd be best to create a limit of your own using this predefined variable:

$_FILES['filename']['size']
Copy linkTweet thisAlerts:
@theuedimasterAug 25.2004 — sry bout' the wrong info ?
Copy linkTweet thisAlerts:
@DurbsauthorAug 25.2004 — Yeah am going to set that in a mo. Is the php.ini set by my host? It doesn't seem to mind the camel and thats about....oh it's only 1.1MB. I was just going to set it so that you couldn't upload images over a certain size, would i use getimagesize() to ascertain the height and width and then say "if height and width are < (or >, which ever one is greater than - i always forget and have to look it up in a book. The crocodile eats the bigger one doesn't it?) than x, dont upload and let the user know". By doing this the file would have to be uploaded first before finding this out, can you find out before it's uploaded (saving bandwith and time) and let then let the user know?

Cheers,

Durbs
Copy linkTweet thisAlerts:
@MstrBobAug 25.2004 — I think you'd best read this page:

http://us3.php.net/features.file-upload

It answers many questions...
Copy linkTweet thisAlerts:
@DurbsauthorAug 25.2004 — hmm...I seem to have a slight problem deleting the file i uploaded - i don't have the rights to do NEthing with it except open it. Bugger Bugger.....Bugger. I take i must create a php file for deleting it, yes?

Durbs
Copy linkTweet thisAlerts:
@MstrBobAug 25.2004 — You can't delete the file? That's no good. Try CHMODing it and then delete:

[code=php]
<?PHP
chmod("/path/to/dir/cotaining/img/", 0770);
?>
[/code]
Copy linkTweet thisAlerts:
@DurbsauthorAug 25.2004 — The server is a windows server (2003 i think) can you chmod on it?

cheers,

Durbs

Edit:

Oh and it's just the image file - i've got full right on the directoy itself. How do stop the upload feature from 'not giving me access to the images' when they are created?
Copy linkTweet thisAlerts:
@MstrBobAug 25.2004 — Oh, well then, that's odd. You can CHMOD files as well, though.

And it's odd that it won't let you delete the file, very odd. If the CHMOD works, than you can try CHMODing the file upond upload. Never had that issue before, though...
Copy linkTweet thisAlerts:
@DurbsauthorAug 25.2004 — i used my ftp client to do it for me and it just came back with "command not recognised". Also when i used XP's built in FTP client and went to the properties for the file it said "This server does not support changing file permissions.:eek: !? Any sugestions?

Cheers,

Durbs
Copy linkTweet thisAlerts:
@Paul_JrAug 25.2004 — [i]Originally posted by Durbs [/i]

[B]edit:

One other quick question (i know it's on mySQL but i didn't see the point of starting a in thread for this little question) If you have 6 items in a table, each being assigned a ID number by auto_increment (so the id's will be 1-6) and you delete item number 4, does the auto_increment give the next new entry 4 (to fill in the gap) or 7 (to continue the trend? Cheers again [/B]
[/QUOTE]

No. The auto_increment will not adjust itself when you delete rows. If you have 7 rows, and you delete number 7, the next row added will have an id of 8. If you delete row 4, and add a row, the next row number will be 9, and the ids will be 1, 2, 3, 5, 6, 8, 9.
Copy linkTweet thisAlerts:
@rch10007Aug 25.2004 — I'm new to all this so my input may not help at all, but in any case here is my $.02.

If you want to assign an article number to a post along with the picture (if there are any) that hasn't been entered into the database yet, would it be easier to code if you just assign the new article a database number when the user clicks the link to go the page where they will post the article. That way you will have the number reserved before they even start to post anything, whether it has a picture or not. If they don't complete their posting, then the article_id won't be used and recycled for the next user.

Does this make sense at all!

If they have a picture to load, then the article_id number is being reserved while they linking to the upload page. Once they post the article and picture, the database stores all the info in the fields provided along with the article_id number that was reserved.

I hope this helps! It kinda makes sense to me, but then again, I wouldn't know how to code it, it's just my idea of a solution. Please let me know what you peeps think!
Copy linkTweet thisAlerts:
@MstrBobAug 25.2004 — Well, that's along the ideas of what he's thinking. If I understand him correctly, he's going to find the last database entry ID, increase it by one, to get the current article ID for his pics. But he isn't going to actually create the Database field until the article is written. Or am I misunderstanding him?
Copy linkTweet thisAlerts:
@MstrBobAug 25.2004 — Actually, that method is flawed. If two users create articles, and they submit them successively, it won't work since the id will be in use. Here's how I would do it:

You have a form page. Simple form where users write the article, and a link to a pop-up to download images. This pop-up has a session_start() and users enter there image and their image description. If you want the ability for multiple images for an article, here's what you do:

Using the standard methods for file uploads, check the validity of images (ie: make sure they are actually image files, check filesize) and move them to your image directory. you have two session variables:

$_SESSION['img_paths']

$_
SESSION['img_descs']

Each variable is an array. You save the image path an description to these arrays. This allows you to save multiple images to an article. Now, upon submitting the form, these session variables are available. You use a

foreach()

loop to create database rows for each image, and you use the article's new ID (the article is already entered before this) to get the proper ID. Wherever you want to display an article's images, simply run through the image table, grabbing the image path/descriptions of images with the article's ID. Shouldn't that work?
Copy linkTweet thisAlerts:
@rch10007Aug 25.2004 — I agree that using +1 to do it won't work for the same reason. Multiple users, LOL. Who can imagine, 2 people accessing the database at the same time, LOL.

I think using the method of "reserving" the article_id in the database BEFORE the user enters anything (the article or images) would be the best way to go.

Like, I said though, I can't write the code to do it but it just seems logical to assign the number first then update the database with the info when the user clicks submit.

If they don't submit, the ID number reserved in the database can be recycled.
Copy linkTweet thisAlerts:
@MstrBobAug 25.2004 — That is possible, but it would require extra code, and there's no real need to do it that way, IMO. The form page itself does not need access to the image files, and the best way to be dealing with the images is to have a directory for these images, and then store there path in the database. And since you're using sessions, the submit script will have access to the image info.
Copy linkTweet thisAlerts:
@rch10007Aug 25.2004 — Cool! ?
×

Success!

Help @Durbs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...