FORUMS: list search recent posts

Need PHP help

COW Forums : Web Design

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Phil-lipNeed PHP help
by on Aug 19, 2005 at 1:47:46 am

Hello Everyone,

I'm new to PHP and am stuck on a problem. Might be my lack of knowledge of PHP, or might be a logic problem I'm not solving...

I am creating a gallery for people to order prints of images from. Each image has pull-down selector to enable visitors to order from 0-10 of 4 different sizes of print. So there would be 4 variables which need to be passed for each image.

This works just fine when I type everything but I'm using my newfound PHP knowledge to use a "for" loop to create the layout for each image - the thumbnail and 4 pulldowns for each size. I have the html which creates the layout loop up to the number of images in the gallery.

This part is great - I can instantly have the thumbnails and pull-downs set for lots of images... however, since the "select" tags are buried in the for loop, the variable name is exactly the same for each image - say a 5x7 has the variable name "$5x7_1" - it now has the same name for pictures 2 and up as well.

Is there a way to increment the _1 part of the name within the for loop? So that the 2nd image's variable would be "$5x7_2" and so on? I've tried writing the name as "$5x7_$rownumber but this doesn't work within the for loop.

Or is there some better way I'm missing? I can't figure out how to do this with an array either.

here's a url if it helps - please ignore the disasterous HTML, I'm just trying to get the script working before I redo the whole thing w/CSS...



Return to posts index

Phil-lipSORRY - here's the right url
by on Aug 19, 2005 at 1:50:15 am

Return to posts index

Curtis ThompsonRe: Need PHP help
by on Aug 19, 2005 at 3:49:18 pm


i sorta follow how you have your code but without seeing it i can't say for sure (the generated html didn't help much, unfortunately)...but there are 2 basic ways to do a tally in a loop.

example 1

you have an array of elements like this:

$foo = array("apple", "orange", "strawberry");

and you want to have your pulldown increment situation - so you could do either of these:

$tally = 1;
foreach ($foo as $f) {
echo "<OPTION VALUE="{$f}_{$tally}">{$f}</OPTION>";

for ($i=0; $i < count($foo); $i++) {
echo "<OPTION VALUE="{$foo[$i]}_".($i + 1)."">{$foo[$i]}</OPTION>";

(btw - the { } chars around the variable in the echo are just to help php and the code reader see the full variable name - they border the variable. it can also sometimes be needed if you want to keep the quote string going, as if (for example) you have a variable called "$bar" - if you reference it in a quoted string as "$bar_text", php will think the var name is "bar_text" - but if you do "{$bar}_text", then it will see the correct var name)

example 2

you have a hash/associative/key value array of elements like this:

$foo = array("fruitApple"=>"Apple", "fruitOrange"=>"Orange", "fruitStrawberry"=>"Strawberry");

then you can make use of the key value like this (very similar to the first example above):

$tally = 1;
foreach ($foo as $k=>$v) {
echo "<OPTION VALUE="{$k}_{$tally}">{$v}</OPTION>";

does that do what you need?


Return to posts index

Curtis ThompsonDOH! typo in that answer - Re: Need PHP help
by on Aug 19, 2005 at 3:50:05 pm

should be $tally++; (with the dollar sign)....

Return to posts index

Phil-lipCurtis - I'm still stuck - but closer (?!?)
by on Aug 20, 2005 at 1:55:47 pm

I can't tell you how much I appreciate your time and knowledge!

I think I'm closer, but still not getting something.

I've zipped up the code so you can see it:
Here's the code!

So here's my thinking using the variables in place in this script...

The page creates $imnum number of images, each with pulldowns to select quantities of 5x7, 8x10, 10x15 and 15x18 sized prints.

I would like the returned value for the first image's (image 1) 5x7 quantity to be a variable called $5x7_1, the second image's to be called $5x7_2, etc.

Currently, I'm using a for loop with the $rowmaker variable controlling the loop. This $rowmaker variable can also be used to tack on the appropriate _1, _2, etc end to the print size variables, but I can't get the script to read it as $5x7_1 when I use $5x7_$rowmaker. I think I tried using the {} in all combinations, but it still won't work.

Ideally, the script would just use $_POST['5x7_$rowmaker'] to read these values, but I don't even know if you can get it to read a variable in the $_POST context.

I'm trying to make the page values sticky - when the user updates the cart, the page reloads. It isn't until they checkout that they move on to a new page.

Again - thank you so much for taking any time at all with this!


Return to posts index

Curtis ThompsonRe: Curtis - I'm still stuck - but closer (?!?)
by on Aug 20, 2005 at 3:38:02 pm


ok - took a look and i think i see what you want - also based on what you said here - using a $_POST['5x7_$rowmaker'] variable would be the way to something like this:

echo "<SELECT NAME="q5x7_{$rowmaker}">";
for ($q=1; $q <= 10; $q++) {
echo "<OPTION VALUE="$q" ".(($_POST["q5x7_{$rowmaker}"] == $q) ? "SELECTED" : "")."">$q</OPTION>";
echo "</SELECT>";

assuming your pre-select is the only issue? if not feel free to let me know - it's early on a saturday here and i'm still waking up... :-)


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 All Rights Reserved