Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 days ago.
Improve this question
I have a web application which has a select input with over 100 options. All of these options are hard-coded into the html and I have to update this list regularly. Is there a way where I can have this list automatically updated by connecting to a database?
I haven't tried anything yet. I am just wondering where to start.
Well, 100 choices is a "lot" of options.
but, you would use some kind of "data aware" control to do this.
However, some kind of "drill down" or organizing the choices would make a LOT of sense here.
I can't recall EVER being on a page with 100+ choices. So, some kind of "re-think" of what the whole business process and goal is about here I think is warranted.
Software "solutions" start out with some kind of scope. So, "more" then just tossing up 100 choices is the whole problem in the first place.
So, we need two things:
The data base table "list" of choices.
the database table that "saves" + "stores" the user selecitons.
Now, of course, since over time the list of choices will and can, and should be able to change. Thus, we need to ALSO normalize the data (if we don't, then we in for a VERY difficult ride and will have to write ever mode code).
So, we will assume say:
MyTripPlanner - a record to describe the trip, and let me choose place(s) I want to visit.
MyTripHotels - a child table that lists out the "many" selections.
MyHotels - a table from which the choices come from.
So, our markup will look like this:
<div id="TripRecord" style="float: left">
<h3>Trip Details</h3>
<div style="float: left">
<h3>TripDate</h3>
<asp:TextBox ID="txtTripDate" runat="server"
TextMode="Date" f="TripDate">
</asp:TextBox>
</div>
<div style="float: left; margin-left: 35px">
<h3>Notes</h3>
<asp:TextBox ID="txtNotes" runat="server"
TextMode="MultiLine"
f="Notes" Height="139px" Width="469px"></asp:TextBox>
</div>
</div>
<div style="clear: both"></div>
<h3>Pick Hotels</h3>
<asp:CheckBoxList ID="CKHotels" runat="server"
DataValueField="ID"
DataTextField="HotelName" CellPadding="8"
CellSpacing="5" RepeatColumns="10"
RepeatDirection="Horizontal">
</asp:CheckBoxList>
Note how there is NOT much markup.
Our code to load is this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadTrip()
LoadHotels()
LoadMyChoices
End If
End Sub
Sub LoadTrip()
End Sub
Sub LoadHotels()
Dim strSQL =
"SELECT ID, HotelName FROM tblHotels where HotelName is not null
ORDER BY HotelName"
CKHotels.DataSource = Myrst(strSQL)
CKHotels.DataBind()
End Sub
Sub LoadMyChoices()
' get list of current user hotel choices, and "check" the boxes in the list.
Dim UserID = 123 ' faking user id since we don't have logon's setup yet
Dim rstMyHotels As DataTable =
Myrst($"SELECT ID, Hotel_ID, UserID FROM tblMyHotelChoices WHERE UserID = {UserID}")
' now check the checkboxes
For Each OneCheckItem As ListItem In CKHotels.Items
Dim SearchRows As DataRow() = rstMyHotels.Select($"Hotel_ID = {OneCheckItem.Value}")
If SearchRows.Length > 0 Then
OneCheckItem.Selected = True
End If
Next
End Sub
And now our results for 98 choices is this:
So, note how very little markup we required.
That is due to using a database to "drive" the choices, and the number of check boxes we have.
So, if tomorrow you add 5 more hotels, then they will "just" show up in the list, since the check box list is not hard coded, but has a datasource based on the Hotels table.
this approach ALSO means that you can add more hotels, users can choose out of that list, and ZERO changes are required to the code logic.
Now, I don't have a submit button, but it would simple "reverse" the code that picked up the ones I have already selected (from the database).
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm working on a database for a news site, and am not sure what the best practice is to split up the tables. It will have four different categories, each containing different fields:
News
Headline
Subhead
Date
Story content
Optional photo
Webinar
Date
Time
Link
Event
Name
Date
Time
Location
Description
Link
Contest
Start date
Name
Description
Should I make:
a single table for all of them
a different table for each category type
or a table with all of the common fields, and separate tables for each category's unique values?
There is no one "right" answer. It depends on several factors.
(Fortunately, you didn't mention implementing an Entity Attribute Value (EAV) model. I strongly recommend you avoid that.)
Some factors to consider:
Consider relationships... will each "category" have the same relationships to other entities? You show "event" having an attribute of location... is that an attribute, or does that represent a relationship to another entity?
Consider how your queries will need to be written. Will you frequently need to query rows from several of the "category" together? For a large number of rows, for best performance, you'll want to avoid inline views tat use UNION ALL to combine separate tables into a single view.
There is lots of good information available about mapping an "inheritance hierarchy" into a relational database, the pros and cons of each approach, and examples of what that looks like, and (most importantly) what the queries look like.
FOLLOWUP
If the requirement is to handle each category separately, and you don't need to query the combined category, then I would go with a separate table for each category (Option 2), for ease and simplicity.
If you need to query the combined category (e.g. select from all category ORDER BY date_col, then I would probably go with a single table, with a discriminator column (category) to be able to distinguish which category a row is. Based on the limited number of attributes, I'd be tempted to define all the attributes, and then just populate the ones that apply to each category. (Option 1)
The third option is workable too. All three options can be made to work.
This is my first question so please excuse any protocol errors!
I am also new to Laravel 5. I have watched the Fundamental video series (especially "syncing tags" and have that working for simple pivot tables) and have searched the forum and further online but am not actually even sure if what I am searching for is the correct way to go about achieving what I wish.
I am trying to create/update/edit a user's profile to track their work experience. So a user can have multiple positions and those positions are to be linked to companies.
I would like the company table to be separate so that users can select existing companies from the dropdown. And the same for position - if a position exists, such as "CEO", I would like the user to be able to select that instead of adding in a new one.
I created a form with fields as below:
<div class="workexperience">
<!--Work Experience / position -->
<!-- Form Input -->
<div class="form-group">
{!!Form::label('positions_list', 'Work Experience:')!!}
{!!Form::select('positions_list', $positions, null, ['id'=> 'positions_list', 'class'=>'form-control','multiple'])!!}
</div>
<!--company-->
<div class="form-group" >
{!!Form::label('companies_list', 'Company:')!!}
{!!Form::select('companies_list', $companies, null, ['id'=>'companies_list','class'=>'form-control','multiple'])!!}
</div>
</div>
<div id="workexperience-placeholder"></div>
<a onclick="cloneMe('#workexperience')" class="btn btn-default btn-block"><span class="icon-entypo icon-plus"></span><b>Add Another</b></a>
and am using jquery clone to allow the user to add in new sets of positions and companies
function cloneMe(thisDiv)
{
$(thisDiv).clone(false).insertBefore(thisDiv+"-placeholder:last");
}
This visually does what I want but is not posting an array of positions and companies as I would have expected.
So,what I am struggling with is how to save/ sync users, positions and companies.
I created pivot tables for company_user, position_user and company_position as below:
company_user: company_id user_id
position_user position_id user_id
company_position company_id position_id
and had imagined a scenario as follows:
User is created,
Positions are linked to user (using sync method)
Companies are linked to user (using sync method)
positions are linked to companies(?)
But I can't work out how to sync positions and companies since they might need to be created first. Basically trying to work out how to retain the relationship between positions and companies, especially since there could be multiples of those per user.
EDIT
So I am trying the suggestion of adding in a company_ID to the position_users table.
What I can't work out is how to sync the info coming in from position and company fields, especially since the position or company may or may not currently exist on the database.
I am using withPivot:
public function users()
{
return $this->belongsToMany('App\User')->withPivot('company_id')->withTimestamps();
}
to add in the company_id field to the pivot table. But I am not sure how to add in that id field to the array when it has to be potentially created first.
Following Laracasts - 23 - Syncing Tags, I am using
$user->positions()->sync($this->processPositions($positions));
Where the processPositions function returns an array of all associated id's for positions coming from the form - whether they previously existed or are newly created.
However I am not seeing how to add the company_id to this sync function.
Just a note that the position and company fields can be cloned so there are potentially multiple "sets" of positions and companies.
I would have three tables: users, companies, and work_history. The work_history would be your pivot table but with a few extra columns: job_title, start_date and end_date. That way, a user can have and belong to many work roles.
If you want roles to be re-used, then you could replace the job_title column in the work_history table with a position_id column.
I asked another question - Laravel 5: synching an extra field via pivot (answered by #lukasgeiter) - and that helped me solve my issue so I would like to have this question marked as closed.
Thanks for reading, I was wondering if anyone knew if there is any way to retrieve multiple fields from a row in a database table automatically. What I mean is something similar to retrieving multiple fields from columns with code like this
SELECT *
FROM table1
WHERE column1 LIKE 'info'
ORDER BY date_added DESC
LIMIT 4
My plan is to have four fields in a row, which will not always contain data, and have php code populate tabs.Some pages may only have one tab while others might have four. The problem is each column will probably need a different name. I don't know if this can be done and don't know if I explained it very well. Any input would be appreciated, thank you.
Sorry for not making it clear. I use that line of code for retrieving rows that have 'info' in the 'column1' column limited to four results. I should probably not have added that line of code into this question. What I am trying to do is have an automated system that will retrieve multiple fields from a single row and populate tabs. At present most rows have part of a youtube video code which, using php, gets inserted into the rest of the code on the webpage. What my goal is is to have multiple youtube videos contained in separate tabs fed in from a database. Some rows might only contain one video while others may contain four.
The problem is that I need to have four columns, one for each video, and I think the columns will need different names. My thinking is if there is data in one of the columns then, due to the code, it will contain the word 'youtube' so if I search that column of that row to see if it contains the word 'youtube' and if it does then add it to the webpage. My problem is I am not sure how to repeat that for the other tabs using other columns. If I each separately in each tab then the pages with only one or two youtube videos will have the extra code remaining in the other tabs.
Sorry this probably doesn't make much sense either. Its like the thinking of a madman. As you can probably tell I am not the most experienced man in the world but thanks for reading.
After some time and research I have come up with an answer to my question. Just to clarify what I was trying to do, I wanted to have 1 to 4 youtube clips on my page with thumbnails below. Only one clip would be displayed at a time.
I created 4 new columns in the database, video1, video2, video3, video4, and in these I put the youtube video code or set them to NULL. Now I created tabs that would only be displayed if the video field was not NULL.
<?php
if ($video1 != NULL) { ?>//skips if there are no youtube videos in database
<div id="tabs" class="ui-tabs">
<ul class="ui-tabs-nav">
<li class="youtbe-li"><a href="#tabs-1">
<img src="http://img.youtube.com/vi/<?=$subject['video1']?>/0.jpg"></a></li>
//uses a thumbnail from youtube
<?php
if ($video2 != NULL) { ?>
<li class="youtbe-li"><a href="#tabs-1">
<img src="http://img.youtube.com/vi/<?=$subject['video2']?>/0.jpg"></a>
</li>
<?php }?>//repeat for the rest
</ul>
<div id="tabs-1">
<div class="youtube-clip">
<iframe width="730" height="436"
src="//www.youtube.com/embed/<?=$subject['video1']?>"></iframe>
</div>
</div>//repeat for the rest
</div>
Is there a way to call field rows in a URL without using the column name??
So I currently have a posting site where users can select category or subcategories of choice from drop downs, how it's currently setup my site outputs links to the categories chosen such as..
topics.php?category=Food&sub_cat=Pies
topics.php?sub_cat=Pies
This allows users to go to either one of the links, or both
topics.php?category=Food&sub_cat=Pies
To give more functionality I am looking at adding textboxes instead of drop downs, the problem is users will more than likely enter the data in different boxes than other users, ie.
User 1. catbox: Food subcatbox: Pies
User 2. catbox: Pies subcatbox: Food
So in this case my current URL system won't return accurate results, so my question is would there be a way where "category" or "subcategory" could be replaced and just put the results together without them being listed in 2-5 different fields therefore not returning all the results that = to that value? "food" or "pie" in this example.
topics.php?xxx=Food&xxx=Pies
or
topics.php?xxx=Pies&xxx=Food
Looking at So homepage if you click "php" it will put php in the URL, click mysql and it will put "php+mysql" that sort of thing.
you can use parent child method in your database.your table would be like this
id - parent_id - category_name - depth
when you want to insert a data to your table it's depth will be one plus it's parent depth
when someone post to your page you first take query witch of the inputs has most depth then that will be your subcategory.
Calling field rows via parameters in your URL may be a very bad idea. It's a perfect way to allow a massive SQL injection attack. So, the answer is probably "yes, but HOLY MOLY PLEASE DON'T!"
Now it may be that your code is parsing these out on the back end and protecting them via any of a variety of methods, I can't tell from the amount of code posted.
In a web form i will ask the user for their job experiences, this data will have no fixed lenght. I need to let the user insert all the items he needs, every item will content 3 fields; job title, description and year.
My firts problem is, how can i ask in the html form for the items? i mean, whats the best way to ask items with no fixed lenght using html/php (and maybe ajax)? I saw some sites that have a button (add one) when you hit it a new item slot is showed, but i have no idea of how to implement this, an example will be sufficient.
The second part is, how can i managed the data flow in post or get?, until now, i only use fixed fields, so i always know in my php script how many post or get vars i will get. How can i use multiple POST vars without knowing the amount of them?
And the last one (and the more important), how will be the best structure for my table in MySQL? If i get multiple items for a fixed table where i will have all my users, how can i resolve the multiple items issue? For example, if my table is:
User | password | job_experiences
admin | root | (this is just a cell, how can i save multiple items here?)
jonh | 1234 | (this is just a cell, how can i save multiple items here?)
Thanks for any help!!!!
Those are 3 questions, and it's best to post 3 questions, instead of discussing all of them. I will post the basics, and if you have specific questions, ask.
First, use button to add, and a JavaScript to clone an existing row (which can have more then one input field). For fieldnames use something like company_name[] - the [] is the important part, at this will send the field as an array. If you are editting profile, you can use company_name[$id] to preserve the mapping.
Second, in PHP you will receive this as $_POST['company_name'] which will be numeric array with all the company names. Or if you specify $id - with the corresponding keys. So, you have to loop trough all company_names, if there are other fields - you retrieve them the same way, using the current key. Example:
for (i =0; i<$_POST['comany_name'].length;i++) {
$company = $_POST['comany_name'][$i];
$start_year = $_POST['from'][$i];
...
}
Next, you need 1 table for the users (username, password), and another for job experiences (userid, company, description, from, to). This is called 1:M relation