Load automatically the rows in TabularForm Yii2 - ajax

Iam trying to make this:
I need that when I create a new poll, it loads the aspects and a dropdown with the valorations choices list from the database like this:
I have a table for aspects, and another for valorations, which is a number from 1 to 5.
Normally, when I create a new poll i have to add manually all the rows and select the aspect and the valoration, but in this way its too much job, so i want it to load all the aspects and it would just need to set the valoration for each row and save it. thanks and sorry for my english

As far as I can understand your query, you want something like this:
You have a database table: Aspects (which I expect has static data)
You have a database table: valorat.(which I expect has static data of 1-5)
Now, whenever you create a new poll, it should automatically populate itself with the aspects and valoration dropdown table.
If I am right, you can try the following logic,
You can run the following logic
//run a loop for each aspect row fetched from database
<?php foreach($aspects as $value): ?>
<tr>
<td>
//here goes the id
</td>
<td>
<?= $value ?> //here goes your aspect value
</td>
<td>
<select>
//here run a loop for valorations you fetched from database
<?php foreach($valoration as $key=>$value): ?>
<option value="<?=$key?>"><?= $value ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<?php endforeach; ?>
In this way, you can achieve the desired table with the aspects and valoration.
If you are using the Yii2 grid view, it is far more simple than this. But without looking at your code and database tables, I can't give you more.
Thank You!! I hope you will get some idea how to do this.

Related

MVC consideration in Laravel

Is it right to use php code or blade code in view file using laravel?
for MVC consideration is it better to sepearate front end code like HTML with any server side code and keep them in corresponding controller file ?
for example using like this:
in view:
<table>
<Loop> <!-- instate of using #foreach -->
<tr>
<td>
<-UserId->
</td>
<td>
<-UserName->
</td>
</tr>
</Loop>
</table>
in controller:
$html = view('page');
$loop_section=my_own_func_to_get_loop_tag_content($html);
//edit loop_section var with php foreach and return resault to $modified_loop_section var
$html=str_replace($loop_section,$modified_loop_section,$html);
return $html;
Updated:
above code is just an example way that isn't seemed a good way.
but i look for better way to separate any php code (including if foreach etc) with html code in view file without using a custom tag and code?
Yes you should definitely use Blade code in your templates. So function my_own_func_to_get_loop_tag_content should be processed in template file, but it should only show data. All business logic (such as DB querying, sorting, calculations, ...) should be done in your controllers. View is only for displaying data, so there shouldn't be any logic.

Joomla Component - Site Model Issue

I've been developing a Real Estate component for several of my clients. I have most everything working, except getting images from a separate table.
One example of a site model is City. Within the city model, the primary get (query) is a list of properties, based on the city. Example, if from either the cities view, or menu item, a city such as Dallas is selected, then in the single city view only property listings where property city = Dallas will show.
That part is working great, as it is supposed to. The problem is that every listing has multiple images, stored in an images table. On the property list view, only a single image (out of potentially many) needs to be pulled an displayed.
Adding as a "linked" sub-query within the main properties query doesn't work, as it will create multiple duplicate property listings for each image. So, I created as a custom method, get image.
The compiler that I am using to aid in the building doesn't support a signature method (method that takes values), so I needed to add as a class value. In the getItems function for properties, I created the class value of:
$this->a_property_id = $item->id;
Then, in the getImage custom function I added
$query->where('a.propid = ' . $db->quote($this->a_property_id));
as part of the query. In theory, this should pull a single item where a.propid equals the property id, such as property id=3, then pull an item from images where propid=3.
Lastly, I added the image code in the site view default.php as part of the foreach:
<?php foreach ($this->items as $item): ?>
<li>
<div class="uk-grid uk-panel uk-panel-box">
<div class="uk-width-medium-1-3">
<?php if(empty($this->image->path)){ ?>
<div> <img class="uk-thumbnail uk-thumbnail-medium" src="<?php echo JURI::root().'media/com_mostwantedrealestate/images/No_image_available.png'; ?>"> </div>
<?php } else { ?>
<div> <img class="uk-thumbnail uk-thumbnail-medium" src="<?php echo JURI::root() . $this->image->path . $this->image->filename; ?>"></div>
<?php } ?>
</div>
<div class="uk-width-medium-1-3 uk-float-left">
<a href="<?php echo 'index.php?option=com_mostwantedrealestate&view=property&id='.$item->id;?>" title="<?php echo $item->name;?>" rel="" >
<h3><?php echo $item->name; ?></h3>
</a>
</div>
This is kind of working, but not completely, so I'm unsure what I may be missing. With the way I have this coded, it's displaying an image. If there is only a single property listing, then it shows the proper image, however if there a, lets say, two properties: id=1 and id=3, then it shows propid=3 from the image table for both listings, rather than showing propid=1 for id=1 and propid=3 for id=3.
Any thoughts on what I might be missing?
You're looping over the $this->items but you use the same $this->image object for all of them. So you see the same image over and over again.
You can fix this by adding the image information to the item. Since you want to show just one image you can easily do this with single query joining your item table and your image table.
Another way is to combine the necessary data from different queries in your model so you can use something like $item->image->path.

Can't use data in template's #foreach anymore

I've got a controller exposing object-data to a view:
$artifacts = Artifact::with('product');
return View::make('index', compact('artifacts'));
This used to work fine until last week. For some reasons now I can't use the data anymore in the template itself.
//this works
<?php
print_r($artifacts->first());
?>
//this doesnt
#foreach ($artifacts as $artifact)
<?php
print_r($artifact);
?>
#endforeach
when using eager loading, you need to get the actual resultset.
$artifacts = Artifact::with('product')->get();

Call another controller function based on drop down selection

I'm building a site that hosts stories that have multiple chapters and on my view I would like the user to be able to select from a drop down menu and go to the chapter they have selected.
I have the drop down populating with information from the database, I'm just having trouble calling another controller based on that selection.
In my view this is my drop down
<select>
<?foreach($chapter as $row) :?>
<option value="<?=$row->chapter_title?>"><?=$row->chapter_title?></option>
<?endforeach;?>
</select>
I did try and add a link to the controller in the option, but that didn't work.
<select>
<?foreach($chapter as $row) :?>
<option value="<?=$row->chapter_title?>">
<?=anchor('story/viewChapter/'.$row->chapter_id, $row->chapter_title);?>
</option>
<?endforeach;?>
</select>
Is there any other way I can do this that does not involve javascript?
Unfortunately, you will have to use javascript:
If your using jquery you could try something like this:
$('#select_id').change(function(){
var chapter = $(this).val();
$(location).attr('href','http://www.mysite.com/books?chapter='+chapter);
});
Here is an example:
http://jsbin.com/efidar/1/

Class in a <option> tag with Codeigniter

Is it possible to add a class attribute in <option> tag using CodeIgniter?
<select name='state'>
<option value="usa" class='top'>USA</option>
<option value="ny">NY</option>
</select>
If not, how to extend the Form helper to support this?
Yeah you can create your own helpers. See "Extending Helpers" here in the docs. I would do what is says and copy a "MY_helper.php" version to your application folder; don't mess with the core unless you really have to.
http://codeigniter.com/user_guide/general/helpers.html
You could use the values array and set a class and item in an array (and change the foreach near line 327 in the helper) or pass another array and check it in the foreach.
Personally I prefer not using helpers for such simple tasks... An inline if statement should be enough for your case.
If I'd want to dynamically populate an tag I'd do something like this:
<?php
$result; //My result set
?>
<select>
<?php foreach($result as $line): ?>
<?php $class = ($condition===TRUE)?'this_class':''; ?>
<option class="<?php echo $class; ?>"><?php echo $line->data; ?></option>
<?php endforeach; ?>
</select>
It might seem a little messy now, but for it feels better to write HTML when you need it instead of letting helpers do that.
If you use an IDE it even helps you use HTML highlighting which allows to better visualise your markup, without losing your php highlighting when you need :)

Resources