I have a table named tags which contains thousands of tags, and increasing. While creating a new post (via newPost blade), I want to pass all tags from controller into this blade and populate them in a dropdown list (with typing functionality) to help the user adding tags. I feel this will be a lot of data to be sent with every post creation, which would affect performance. Am I right ? Any suggestions?
Related
I am new to Laravel and trying to get my head around how to allow a user to repeat a field.
I have a form that allows a user to create a basic page which has a title and description, I'd like to allow the user to then if they choose create another title and description field using a "Add more" button.
They allow them to re-order these field collections.
Cheers,
Chris
This seems to be less about Laravel and more about general DB Schema, PHP and UI practices. So due to the broad spectrum of answers this requires, I will answer mostly using best practice concepts to avoid being overly wordy (using Laravel for context).
Front-end
For the ability to add another set of fields on the fly, you will want to use a form array:
<div class="field-set">
{{ Form::text('fields[0]["title"]', Input::old('fields[0]["title"]')) }}
{{ Form::textarea('fields[0]["description"]', Input::old('fields[0]["description"]')) }}
</div>
+ Add more
Your 'add-more-link' should have a javascript trigger that will duplicate and append the 'field-set' element with an iteration of the index in the form array
(e.g. fields[1]["title"], fields[2]["title"])
Note: You can do this without JS, but you will have to repost the page with some parameter that increments the amount of fields to create and then loop through that many times while rendering the field sets on DOM generation.
To allow for reordering, I recommend jQuery UI's Sortable. Note: You can accomplish this without JS too by POSTing up or down increments for each field set, but more processing would need to occur in the back-end.
Back-end
In the model where you handle the save, grabbing the data via Input::get('fields') will store it into a PHP array that you can iterate through to validate, parse and store the data.
As for your DB schema, you will need to account for the creation of any number of new fields. This means that will likely need a separate table that will be joined on users.
(Columns: id, user_id, title, description)
In Laravel you can define a Has Many Relationship on the users model that will grab all the titles and descriptions for any given user.
http://laravel.com/docs/eloquent#relationships
I'm not sure you are using forms correctly, although its a little hard to tell from you question.
If you want to create a new resource (with a title and description) you dont add new form fields. You can have an 'add more' button, but this button just spawns another instance of your form which can then be posted.
But to be honest, you question is so vaugue its hard to tell what your problem is...
I am trying to create a form which provide a checkbox element on each row. Problem is that I have 2000 rows which takes some time to load and also it is not easy to navigate through whole list.
Is there a way to create some kind of pagination in Drupal form ?
There's no built-in pagination of field values within the context of a larger form that I'm aware of. Instead, you'd probably want to consider a Javascript solution, where you load all 2000 form values, but you use JS and CSS to hide all but the first page. Then you create Javascript forward/back and page links which dynamically hide the first "page" of checkboxes and load the page in question.
The reason I recommend this, instead of an AJAX request that loads only the first 20 records and then dynamically loads more via a pager, is that you'd have to separately track and store which values had been checked (since the AJAX would literally throw away and reload the next 20 checkbox values). By contrast, if it doesn't slow the page down to load all 2000 checkboxes as, say, 100 individual sections behind the scenes, and then use your custom JS pager to show/hide the pages, your user could check and uncheck while paging and all the values would be remembered. (It's an often overlooked feature of HTML forms that they retain their field values even when hidden via CSS, which can be super helpful when you're aware of it).
I don't think a code snippet would be too useful here because this is a fairly open-ended problem, but the basic process would be:
Use hook_form_alter() to change the specific checkbox group field(s). Specifically, you would use markup to add the paginator controls after running the database query to retrieve the results and determine the number of pages.
Also as part of using hook_form_alter(), you would loop a page at a time and generate all 100 (or whatever number) pages of checkbox options, setting all but the first page to display:none in the <div> tags surrounding the checkbox options.
Create JS or jQuery functions in your site's custom theme, or put the code into an includes/ folder and load it dynamically through hook_form_alter (not elegant - I recommend always having a custom theme or sub-theme available). This function would listen for the link press and the current page and hide/show the proper CSS blocks.
If you wanted to get fancy, you could also create a JS-enabled page number field where you could type in a page number and hit Enter, or a search feature that would return individual results (more complex since now you'd have to be able to show/hide all individual records), and a check all/uncheck all feature for individual pages. Have fun!
I'm creating a blog for a website I am building. The main blog page obviously has each blog listed as it should. But I want each blog to also have it's own individual page on the website. I want this page to be generated on creation of the blog post.
My question is, what would be the best method of creating this page. If I use the php file functions to create it, I would need to fill up a $data variable with hundreds of lines of HTML for the page. Which I guess is feasible, IF I am also able to dynamically change the variable to work for the new content that needs to be posted on said page.
Is there better methods? Would PHP work for this? Any suggestions would help.
In general, I would do it more or less like this:
1. Create a 'Blogs' SQL database table.
Create an 'id' column (primary key), and columns for fields like 'blog_date', 'title', 'author', and 'html_content'.
2. Create a 'Blog' class using php.
Create properties which correspond to the SQL fields in your 'Blogs' table, including 'id'.
3. Create public 'loadFromDB()' and 'saveToDB()' methods
These methods should load and save the SQL values of a row in 'Blogs' (selected by id) to and from the class properties.
4. Create a public 'view()' method for the 'Blog' class
This is simply an HTML view/template of a single blog entry displaying date, title, author, etc.
5. Create an 'index.php' blog page in a folder on the server
You want to use this page to display a single blog entry.
Use a blog's SQL id when calling this page from links in your main blogs page, for example via http GET: 'http://www.yoursite.com/blogs/blog/index.php?id=30'. In your php code inside index.php, where you want the entry to appear, do something like this:
$id_default = 27; // the blog entry which must appear when there is no query parameter
$id = empty($_GET['id']) ? $id_default : $_GET['id'];
$B = new Blog($id);
$B->loadFromDB();
$html = $B->view();
echo $html;
In this way, you don't have to create an HTML page for EVERY blog entry, or put ALL the entries on one page. You can just use one page created dynamically with a GET parameter instead.
Obviously there is more you can/must do, such as creating commenting displays and forms and a 'Comments' SQL table, etc. But this should give you some ideas.
Its possible to create individual page for each blog dynamically. Here are the steps you can follow.
Create one master template. Add some special tags where you want dynamic content.
While adding new post, Read the content from that master template, replace appropriate special tags with actual values
Write finally generated content into new file and save it to associated location.
You are ready to access that page.
I have a database of different stores.
When a user clicks on a store name, I want an Ajax function to run displaying information about the store in a different div.
Information categories for all stores wll be the same: products carried, location, general information, etc.
I could easily make it so that each different store uses a different file name as an argument to the ajax function, and all files would have the same layout/format but with different data.
However i feel like this is bad form. How can i make it so that i have one fixed template and all that changes is the information specifics imputed into the template?
Please note that the store information display pages will also need to be able to have clickable links of their own (i.e. click on location and a google map pops up).
Is it something to do with XML? I dont know much about it.
Instead of returning a template, return the data.
So it says getstore.php?id=2 which returns a json string
{"name":"my store", "info" :"blah"}
Then you use java script to insert a new div, populated with that data.
I am looking at an entity called content where I will store data about the content (textfield1, textfield2, bgcolor etc.) in an array inside the entity content (if this is not the best way to go please advice me).
The reason I want to put it in an array instead of just making separate entity fields for it is that I will have different 'content templates' so the amount and type of data fields will be different for each template (and each template of course has its own formbuilder; ContentXType.php, contentYType.php etc.). While one content type might have only one textfield, another might have 10.
I initially started on a design with datafield1, datafield2 etc. but realized that will leave me with a bunch of nullvalues and won't really be pretty =)
At savetime I will generate an html output for this content in a different field called contentRendered.
At edit time I will again want to be able to open up the different datafields from my array in different form widgets, so for example textfield1 in a textfield, textfield2 in a textarea, and bgcolor (the third value of my array) in a colorpicker form widget (I guess I will use a textfield with a jQuery color picker widget).
So my little issue is whether entity type array is the best way to go for this, can I even from within my form builder pick out value 1 from the array and put that in one field, value 2 in a different form field etc.?
Or do I need to go with say a new entity called content_data and use relations?
Or would a better way to go be to define a new entity for each kind of content I plan on using, and then embedding a form for that content type in my main content form?
After some more research I will define a new entity for template in which I will specify my templates, and have different twig files for the rendering of each template.
The actual data for each template, which will be of variable amount of fields, will be stored in a serialized array.