Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 19
  1. #1
    Senior Member
    Join Date
    Aug 2011
    Posts
    226
    Member #
    29153
    Liked
    4 times

    Using array names in html forms

    I have tried everything I can think of to use to get array names in a form name. Either I am using the php code wrong to execute them or how I'm putting them in just isn't right. I'll give an example.


    I pick out the songs each week at church for service. I keep all of the songs we sing in a database and I change the date we sing them. If I pick out 6 songs and change the date, I have to do it one at a time with over 100 forms and submit buttons. I'd like each song to have its own if where I have something like $date[1], $date[2] etc. Then when I submit it, it will change all with one submit.


    I have a different page that has many more pieces of data that I have set to 100 per page. Therefore, each will have a different id attached to it so I use a counter $x. When I try to set this up as name = "$name[$x]" it doesn't work. I am using the escape with the "". I have changed it around so much that I'm probably repeating myself now.


    How do I set this up?


    What I currently have looks something like this.

    echo "Title<input type="text" name="shortDesc[$x]" size="40">\n"; I've tried several ways to get the submitted data.


    Here's one of many.

    $shortDesc = $_POST['shortDesc']['36'];
    echo $shortDesc
    Last edited by Glenn; May 01st, 2019 at 08:17 AM.

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,689
    Member #
    5580
    Liked
    717 times
    Glenn ...

    You have a loop that displays them, and you make the "value" of the input the title, or better yet, the unique ID value of the song (from the database).

    Each one can be a checkbox ...

    I'm not sure how you are doing your query or loop ... you didn't post any scripts.
    You would have a loop that displays all songs and each would have a different value ... but the checkbox "name" is the same for all.

    echo "<input type='checkbox' name='song[]' value='$id[0]'> $shortDesc[0] <br />";
    echo "<input type='checkbox' name='song[]' value='$id[1]'> $shortDesc[1] <br />";
    echo "<input type='checkbox' name='song[]' value='$id[2]'> $shortDesc[2] <br />";
    echo "<input type='checkbox' name='song[]' value='$id[3]'> $shortDesc[3] <br />";

    The last line is the date you want the selected ones to be processed ...

    echo "DATE <input type='textbox' name='date'><br />";

    You should use a JQuery date pop-up box to select date.

    The point is, if you see the name of the checkbox, name='song[]' , you'll see the name is "song" with empty square brackets.
    That is an array. And each checkbox has the very same name .... name='song[]'

    When you process it, the array will be an array of only the checkboxes that you checked ...

    $boxes = $_POST['song'];
    foreach($boxes as $id){
    echo "The song ID you selected is: $id <br />";
    }


  4. #3
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,689
    Member #
    5580
    Liked
    717 times
    If you show me how you query your database, and how your database is structured, I can show you an example script.

    And also, how did you create your database of church songs? Is this a part of a bigger thing that uses WordPress, or some other software?


  5. #4
    Senior Member
    Join Date
    Aug 2011
    Posts
    226
    Member #
    29153
    Liked
    4 times
    Here's what I have done it seems to work. This is just an example.

    <form method="post" action="">

    <?php
    for($x = 0; $x < 4; $x++){ ?>
    <input type="text" name="p[]" value="<?php echo $_POST['p'][$x]; ?>">
    <br>
    <?php
    }

    echo "<input type="submit" name="submitform" value="Submit">";

    echo "<br><br>\n";

    for($i = 0; $i < 4; $i++) {

    $inputed = $_POST['p'][$i];
    echo "$inputed<br>\n";




    Thanks

    }


    ?>


    </form>

  6. #5
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,689
    Member #
    5580
    Liked
    717 times
    Glen,
    Can you tell me more about your "database" of songs?
    How did you create that and can you give me an example?
    Private message me if you wish.

    I have a suspicion that you're making more work for yourself than it needs to be.

    For example, you won't want to use a "for loop" because you won't know how many songs are in the database.
    You would use a "foreach" loop.

    Things like that. Which is why the database is so important.


  7. #6
    Senior Member
    Join Date
    Aug 2011
    Posts
    226
    Member #
    29153
    Liked
    4 times
    With the song database I use the foreach loop.I have another set of records on something different that I paginate because I have about 4,000 records in it. I may use as many as 100 per page so I use the for loop with it.

    I just need to figure out how I'm going to save check boxes with it. I have each record in different categories, some may be in more than one category. I haven't used check boxes much in the past and need to learn how to save to and read them from the database. As well as have the boxes checked if it was done so in a previous session.

    Thanks for the advice.

  8. #7
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,689
    Member #
    5580
    Liked
    717 times
    Again, what is the database? SQL, plain text?


    If you use SQL (MySQLi, PDO), you should normalize the database.

    One table is the whole list of songs. That's it, just the list of songs. (Row ID, titles, composer, dates, description, etc). There are 4000 rows in this table.

    Another table is your categories. Just the categories. A Row ID, the name of category.

    Another table is the "playlist" ...
    Each row in this table is: A date (one date), ID of song, ID of category.

    You build the playlist for each week by adding songs for a given date (that they will be played). You only need to save the ID of the song and pick a category. When you pick songs for a particular date, you'll see the whole song name, but when selected, you only need the ID of the song.
    This playlist table may have 100,000 rows in it, depending on how far back you keep the playlists.

    You query the playlist table by date and you'll get a list of songs for that date only.

    You query the playlist by category and you'll get a list of all songs that you assigned to that category. There will be a lot of different songs and different dates.

    You query the playlist by song and you'll get a list of just that one song, but every date in which it appears.

    With SQL, you will use JOIN, JOIN LEFT, etc. to build queries across all of your tables.

    So let's say now that it's Sunday May 19, 2019. You have previously added songs to that date. Now you query for that date only and you'll see all of the songs you added to that playlist.

    Someone might say ... "hey, when was the last time we sang that song"? So now you do a query on the playlist to find out the last date that each song was used in a playlist. "Oh, we sang "Onward Christian Soldiers" on May 5th, we don't want to sing that again". So you drop that one from the May 19th playlist and add another one.

    Here's the magic part .... for any given date, you could start typing the title of a song and "suggestions" from the song table will appear. They appear in real time as you type them ... just like Google does when you type in a search box. You could even have it show you the last time the song was used in a playlist. If you type the word "church", every song with "church" in the title will appear. If you see the song you want to add, you can click on it and add it to your playlist.

    You will not need to look through 4000 songs to build your playlist.

    You need to use PDO (Which is the proper MySQL - PHP Data Objects) for your database.

    There may already be a PHP script (or program) that already does all of that ... someone you can purchase for very little money.


  9. #8
    Senior Member
    Join Date
    Aug 2011
    Posts
    226
    Member #
    29153
    Liked
    4 times
    Sorry, I now know what you are asking about the database. It's a mysql database.

    I have the song list taken care of now. I just need to get the checkbox straight for the other thing I'm working on.

    Here's how I'm dealing with that so far:

    echo "<tr>\n";
    echo $_POST['shelves'][$loopId];
    echo "<br>";
    echo "<td><input type="checkbox" name="shelves[]"
    $ck_shelves >shelves</td>\n";
    echo "<td><input type="checkbox" name="lights[]"
    $ck_lights>lights</td>\n";
    echo "<td><input type="checkbox" name="desks[]"
    $ck_desks>desks</td>\n";
    echo "<td><input type="checkbox" name="wallShelves[]"
    $ck_wallShelves>wallShelves</td>\n ";

    echo "</tr>\n";


    edit:
    As a matter of fact, I haven't password protected it yet. You can see what I have so far here.

    blackpipeideas.com/admin/editinfo.php
    Last edited by Glenn; May 06th, 2019 at 07:30 AM.

  10. #9
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,689
    Member #
    5580
    Liked
    717 times
    In your database I assume you are using a primary key column for each row. That would be a sequential key or ID number that gets incremented for each row and is never duplicated. If a row gets deleted, that ID goes away and never gets used again. So rows come and go and the database grows and shrinks, but those ID number stay with the original rows they were assigned to and never change.

    That is important because when you use checkboxes for example, like you use shelves[], when PHP processes that array, the array ONLY contains the checkboxes that were checked. So now, how does it know which checkboxes they are?

    Let's look at this one:
    echo "<td><input type="checkbox" name="shelves[]"

    You can give each checkbox a unique "value". When PHP processes the array, the array will contain the values of only the checkboxes that are checked.

    Give the checkboxes a value that 1) defines which is checked, and 2) which ID number is in the database.

    echo "<td><input type="checkbox" name="shelves[]" value="232|shelves">
    echo "<td><input type="checkbox" name="shelves[]" value="13|shelves">
    echo "<td><input type="checkbox" name="shelves[]" value="160|shelves">
    echo "<td><input type="checkbox" name="shelves[]" value="16|shelves">
    echo "<td><input type="checkbox" name="shelves[]" value="193|shelves">

    Now, if I check only the 2nd and 3rd one, my array called shelves will only have 2 values in it.
    13|shelves
    160|shelves

    If I explode that value, separated by pipes |, I know which ID it is in the database and what I'm actually looking at.

    foreach($shelves as $shelf){
    list($id,$item)=explode("|",$shelf);
    echo "The id is $id and the item is $item <br />";
    }

    I use pipes | as the delimiter because pipes are not used for anything else.
    It makes it easier. You could use dashes, underlines, etc. I just like using pipes | | |

    I can now do things in the database that only affect those two items.
    I know what to look for (shelves) and I know which row to update ... WHERE item = '$shelf' AND id=$id
    Remember that ID is not the row number, but a column that contains unique ID numbers. The rows constantly change as you add and remove items.

    Hope that helps you out.


  11. #10
    Senior Member
    Join Date
    Aug 2011
    Posts
    226
    Member #
    29153
    Liked
    4 times
    Thanks.That's good advice. I can work with that. What about making it automatically be checked? Both after reading the database and after submitting the form.
    Last edited by Glenn; May 07th, 2019 at 07:06 PM.


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:50 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com