Reputation: 1400
I have a small issue I ran into and would like to figure out the logic behind it. On my website, I have a form where admin will submit various info regarding a user, including some links. Now, I have no idea how many links there will be, maybe 5, maybe 35. Point is I don't know the upper limit, so how would I go about making that form? Here's an idea that might work:
Once add customer form is open, I'll have the admin type in the number of links for submission; Based on that number, input fields would be created; PHP will then check if columns already exist, if not, it will create & submit them; Display page for customer will then select * from data and display it accordingly;
Here's what I don't know how to pull off here: How do I create X input fields based on number X? How do I make php add columns like this: Link1, Link1status (predefined), Link2, Link2status (predefined). etc? How do I make the display page for customer echo only the number of links present in a similar fashion: Link1: $Link1 Link1status: $Link1status etc.
I'm not looking for you to write the code for me, I'm just having issues with logic where I need to create html based on some number.
Thank you for your help! Best Fisher
Upvotes: 2
Views: 193
Reputation: 1833
You can make two separate tables, one with the users, one with all links.
Table users with columns (ID, name (for example), ...)
Table links with columns (ID, user_id, link, link_status (for example), ...)
Example: you have the user 'John Doe' with three properties, let's say hair color (brown), length (180 cm) and weight (80 kg), you get this:
Table users with 1 row > (1, John Doe) //(ID, color)
Table links with 3 rows > (1, 1, hair color, brown), (2, 1, length, 180 cm), (3, 1, weight, 80 kg) //(ID, user_id, property, value)
You can apply this on your system. If you want to get all links of an user, just search in the 'links' table with the 'user_id'.
To make a form for the insertion of this records, use a table with one starting row and a hidden row with empty inputs. A user can add more rows with inputs to add links (you can use jQuery's clone()
function to copy the hidden row with empty inputs). You can use name=array[]
(with the inputs) to get an array result after POST.
Upvotes: 1
Reputation: 1344
As in the comments, you should probably use a javascript based solution:
Just have a button to "add another link":
<div class="links-wrap">
<input type="text">
</div>
<a class="new-link">Add another link</a>
Then:
$('.new-link').click(function() {
$('.links-wrap').append('<input type="text">');
});
Then you can pull the data from all link fields.
Upvotes: 1
Reputation: 480
You can use javascript to add links as you complete the form (like an "add new field button"), which will allow you to create as many inputs as you want. But if you want to keep it simple, just replace all the fields for the links with an textarea, and have the admin to add the links separated by colon or semicolon. All you have to do is to use the explode
function to get the links before add them to the database or whatever you want to do with them.
Upvotes: 0