Creative Communities of the World Forums

The peer to peer support community for media production professionals.

  • Posted by Phil Incorvia on August 19, 2005 at 1:47 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 5×7 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…

    https://www.seebigbelly.com/gallerybeta.php

    Thanks!

    Phil

    Curtis Thompson replied 20 years, 8 months ago 2 Members · 5 Replies
  • 5 Replies
  • Phil Incorvia

    August 19, 2005 at 1:50 am
  • Curtis Thompson

    August 19, 2005 at 3:49 pm

    hello…

    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>”;
        tally++;
    }
    

    or

    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>”;
        tally++;
    }
    

    does that do what you need?

    sitruc

  • Curtis Thompson

    August 19, 2005 at 3:50 pm

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

  • Phil Incorvia

    August 20, 2005 at 1:55 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 5×7, 8×10, 10×15 and 15×18 sized prints.

    I would like the returned value for the first image’s (image 1) 5×7 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!

    Phil

  • Curtis Thompson

    August 20, 2005 at 3:38 pm

    hello…

    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 go…so 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… 🙂

    sitruc

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy