SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Array Help

Hybrid View

  1. #1
    SitePoint Wizard Crowe's Avatar
    Join Date
    Nov 2001
    Location
    Huntsville
    Posts
    1,117
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Array Help

    I am buidling a little script for my dad to speed up some html building he does. The form I've designed works like this:

    1. Input number of products and Number of Rows
    2. Use number of products to build input form consisting of tn (thumbnail), image, and description.
    3. Process the form and build columns and rows

    I can get 1 and 2 working just fine. I'm having trouble with processing the array from step 2 to step 3. Here is the code I wrote:

    PHP Code:
    <?


    if ($action == "buildform"
        {
            echo 
    "<form action=\"store.php\" method=\"post\">";
            for (
    $i 1$i <= $products$i++) 
                {
                
    // display form
                
    echo "
                    Thumbnail <input type=\"text\" name=\"items[]\"><br>
                    Full Size Image <input type=\"text\" name=\"items[]\"><br>
                    Descritpion<br> <textarea cols=\"50\" rows=\"5\" name=\"items[]\"></textarea><br><hr size=\"1\"><br>
                "
    ;            
                }
                echo 
    "
                    <input type=\"hidden\" name=\"action\" value=\"buildhtml\">
                    <input type=\"hidden\" name=\"products\" value=\"
    $products\">
                    <input type=\"hidden\" name=\"rows\" value=\"
    $rows\">
                    <input type=\"submit\" name=\"submit\" value=\"submit\">
                    </form>    
                "
    ;
        }
        

    elseif (
    $action == "buildhtml")
        {
            
    $drows 0;
            echo 
    "<table width=\"100%\"><tr>";
            foreach(
    $items as $item => $value
                {
                
    // build html
                    
    if( $drows $rows
                        { echo 
    "</tr>\n<tr>"$drows 0; }
                echo 
    "<td><a align=\"left\" href=\"$value\"><img src=\"$value\" border=\"1\"></a><font size=1>$value</font></td>\n";
                
                }
            echo 
    "</tr></table>";
        }
        
    else 
        {
        
    // Default - display build form input
        
    echo "
            <form action=\"store.php\" method=\"post\">
            How many products for this batch? <input type=\"text\" name=\"products\"><br>
            How many columns (not rows) for this batch? <input type=\"text\" name=\"rows\"><br>        
            <input type=\"hidden\" name=\"action\" value=\"buildform\">
            <input type=\"submit\" name=\"submit\" value=\"submit\">
            </form>    
        "
    ;
        }    



    ?>
    I'm terrible at arrays.. any help is appreciated.

    -Chris
    Chrispian H. Burks
    Nothing To Say

  2. #2
    SitePoint Addict shad0w's Avatar
    Join Date
    Aug 2003
    Location
    PA
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I can see you've got loads of problems with your form input names. You cant have the same form have to input tags with the same name or php will get confused. You also have to use the $_GET or $_POST super globals to access the variables containing the different input values.

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    ...
    <
    form ... action='...' />
    ...
    ...
    <
    input type='hidden' name='action' ... /></form
    Is perfectly legal

  4. #4
    SitePoint Wizard Crowe's Avatar
    Join Date
    Nov 2001
    Location
    Huntsville
    Posts
    1,117
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Dr. Livingston is correct. form ... action is not a namespace element while name=action is. action= is a value/pair and name= is an action pair, they are not the same.

    Anyone have any idea on the array issue? I've been looking through the php cookbook, which usually solves my problems and so far I haven't seen anything that get me straight. I'm close, just not quite
    Chrispian H. Burks
    Nothing To Say

  5. #5
    SitePoint Wizard Crowe's Avatar
    Join Date
    Nov 2001
    Location
    Huntsville
    Posts
    1,117
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Okay, I think I cheated and got it working. I still think there is a more elegant way to do this, but this works perfectly. Here is the code I used.

    PHP Code:
    <?


    if ($action == "buildform"
        {
            echo 
    "<form action=\"store.php\" method=\"post\">";
            for (
    $i 1$i <= $products$i++) 
                {
                
    // display form
                
    echo "
                    Thumbnail <input type=\"text\" name=\"tn[
    $i]\"><br>
                    Full Size Image <input type=\"text\" name=\"image[
    $i]\"><br>
                    Descritpion<br> <textarea cols=\"50\" rows=\"5\" name=\"description[
    $i]\"></textarea><br><hr size=\"1\"><br>
                "
    ;            
                }
                echo 
    "
                    <input type=\"hidden\" name=\"action\" value=\"buildhtml\">
                    <input type=\"hidden\" name=\"products\" value=\"
    $products\">
                    <input type=\"hidden\" name=\"rows\" value=\"
    $rows\">
                    <input type=\"submit\" name=\"submit\" value=\"submit\">
                    </form>    
                "
    ;
        }
        

    elseif (
    $action == "buildhtml")
        {
            
    $drows 0;
            echo 
    "<table width=\"100%\"><tr>\n";
            for (
    $i 1$i <= $products$i++) 
                {
                
    // build html
                    
    if( $drows >= $rows
                        { echo 
    "</tr>\n<tr>"$drows 0; }
                echo 
    "<td><a align=\"left\" href=\"$tn[$i]\"><img src=\"$image[$i]\" border=\"1\"></a><font size=1>$description[$i]</font></td>\n";
                
    $drows++;
                }
            echo 
    "</tr></table>\n";
        }
        
    else 
        {
        
    // Default - display build form input
        
    echo "
            <form action=\"store.php\" method=\"post\">
            How many products for this batch? <input type=\"text\" name=\"products\"><br>
            How many columns (not rows) for this batch? <input type=\"text\" name=\"rows\"><br>        
            <input type=\"hidden\" name=\"action\" value=\"buildform\">
            <input type=\"submit\" name=\"submit\" value=\"submit\">
            </form>    
        "
    ;
        }    



    ?>
    Chrispian H. Burks
    Nothing To Say


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •