In textile, how do I set the class of an (un)ordered list? - textile

Using textile, I can set the class of a table by writing
table(myClass).
| a | b | c |
produces
<table class="myClass">
...
But how can I do the same thing for (un)ordered lists?
# Item 1
# Item 2
# Item 3
to make textile product
<ol class="myClass">
<li>...

Related

How to eval a string containing column names?

As I cannot attach a conditional formatting on a Table, I need an abstract function to chech if a set of records or all records have errors inside, and show these errors into forms and/or reports.
Because, to achieve this goal in the 'standard' mode, I have to define the rule [○for a field of a table every time I use that field in a control or report, and this means the need to repeate the same things an annoying lot of times, not to tell about introducing errors and resulting in a maintenance nightmare.
So, my idea is to define all the check for all the tables and their rows in an CheckError-table, like the following fragment related to the table 'Persone':
TableName | FieldName | TestNumber | TestCode | TestMessage | ErrorType[/B][/COLOR]
Persone | CAP | 4 | len([CAP]) = 0 or isnull([cap]) | CAP mancante | warning
Persone | Codice Fiscale | 1 | len([Codice Fiscale]) < 16 | Codice fiscale nullo o mancante | error
Persone | Data di nascita | 2 | (now() - [Data di nascita]) < 18 * 365 | Minorenne | info
Persone | mail | 5 | len([mail)] = 0 or isnull([mail] | email mancante | warning
Persone | mail | 6 | (len([mail)] = 0 or isnull([mail]) | richiesto l'invio dei referti via e- mail, | error
| | | and [modalità ritiro referti] = ""e-mail"" | ma l'indirizzo e-mail è mancante |
Persone | Via | 3 | len([Via]) = 0 or isnull([Via]) | Indirizzo mancante | warning
Now, in each form or report which use the table Persona, I want to set an 'onload' property to a function
' to validate all fields in all rows and set the appropriate bg and fg color
Private Sub Form_Open(Cancel As Integer)
Call validazione.validazione(Form, "Persone", 0)
End Sub
' to validate all fields in the row identified by ID and set the appropriate bg and fg color
Private Sub Codice_Fiscale_LostFocus()
Call validazione.validazione(Form, "Persone", ID)
End Sub
So, the function validazione, at a certain point, as exactly one row for the table Persone, and the set of expressions described in the column [TestCode] above.
Now, I need to logically evaluate the TestString against the table row, to obtain a true or a false.
If true, I'll set the fg and bg color of the field as normal
if false, I'll set the the fg and bg color as per error, info or warning, as defined by the column [ErrorType] above.
All the above is easy, ready, and running, except for the red statement above:
How can I evaluate the teststring against the table row, to obtain a result?
Thank you
Paolo

Is there any Eloquent way to get sum of relational table and sort by it?

Example:
table Users
ID | Username | sex
1 | Tony | m
2 | Andy | m
3 | Lucy | f
table Scores
ID | user_id | score
1 | 2 | 4
2 | 1 | 3
3 | 1 | 4
4 | 2 | 3
5 | 1 | 1
6 | 3 | 3
7 | 3 | 2
8 | 2 | 3
Expected Result:
ID | Username | sex | score_sum (sum) (desc)
2 | Andy | m | 10
1 | Tony | m | 8
3 | Lucy | f | 5
The code I use so far:
User model:
class User extends Authenticatable
{
...
public function scores()
{
return $this->hasMany('App\Score');
}
...
}
Score model
class Job extends Model
{
//i put nothing here
}
Code in controller:
$users = User::all();
foreach ($users as $user){
$user->score_sum = $user->scores()->sum('score');
}
$users = collect($users)->sortByDesc('score_sum');
return view('homepage', [
'users' => $users->values()->all()
]);
Hope my example above make sense. My code does work, but I thought there must be an Eloquent and elegant way to do this without foreach?
There are 2 options for doing this in an Eloquent way.
Option 1
The first way is to do this to add the score_sum as an attribute that is always included when querying the users model. This is only a good idea if you will be using the score_sum the majority of the time when querying the users table. If you only need the score_sum on very specific view or for specific business logic then I would use the second option below.
To do this you will add the attribute to the users model, you can look here for documentation: https://laravel.com/docs/5.6/eloquent-mutators#defining-an-accessor
Here is an example for your use case:
/app/User.php
class User extends Model
{
.
.
.
public function getScoreSumAttribute($value)
{
return $this->scores()->sum('score');
}
}
Option 2
If you just want to do this for a single use case, then the easiest solution is just to use the sum() function in the eventual foreach loop you will be using (most likely in the view).
For example in a view:
#foreach($users as $user)
<div>Username: {{$user->username}}</div>
<div>Sex: {{$user->sex}}</div>
<div>Score Sum: {{$user->scores()->sum('price')}}</div>
#endforeach
Additionally, if you do not want to do this in a foreach loop you can use a raw query in the Eloquent call in your Controller gets the `score_sum'. Here is an example of how that can be done:
$users = User::select('score_sum',DB::raw(SUM(score) FROM 'scores'))->get();
I did not have a quick environment to test this, you might need a WHERE clause in the DB::raw query
Hope this helps!
This is as nice as it gets:
User::selectRaw('*, (SELECT SUM(score) FROM scores WHERE user_id = users.id) as score_sum')
->orderBy('score_sum', 'DESC')
->get();

Database Table: Quantity or redundancy

I'm building a database which have a lot of items for a bike shop. This bike shop have many of the same items such as 100 wheels of size 4 and color 'red'. My question is:
Is is better to add a 'Quantity' field to the entity set and put all similar items in one entity (example 1) or is it better to have an entity for each item (example 2)?
Example 1:
id | color | size | quantity
1 | red | 4 | 100
Example 2:
id | color | size
1 | red | 4
2 | red | 4
3 | red | 4
etc.
The first - qqhantity field - unless you have a reason to track for example serial numbers, and even then you may go to v1 and use a separate column.
Generally: get a copy of the Data Model Ressoure Book Vol 1 - it has a ton of discussions about standard business data problems, among them an inventory system. You will learn a lot.

How do I display a tree view based on this given database data?

The table looks like:
id | title | parent | depth |
---+-------+--------+-------+
1 | blah | 0 | 0
---+-------+--------+-------+
2 | blah | 0 | 0
---+-------+--------+-------+
3 | blah | 0 | 0
---+-------+--------+-------+
4 | blah | 2 | 1
---+-------+--------+-------+
5 | blah | 2 | 1
---+-------+--------+-------+
6 | blah | 5 | 2
---+-------+--------+-------+
I want to take that data and output it into a tree like view. Similar to:
1 - blah
2 - blah
4 - blah
5 - blah
6 - blah
3 - blah
Someone suggested I use the query:
SELECT title, depth FROM table ORDER BY parent, id
I could then use the depth to create the whitespace on the left and format the tree. This works up until I use HTML. I want to be able to format it nicely in valid HTML and then style it with CSS, etc...
Using just the depth, I can't think of a way to wrap the comments in a div class="parent/child" type structure.
How can I format the data?
Use a <ul> in HTML, with a <li> for each item. Every time you increase in depth compared to the last item, add one <ul> to the current <li> instead of closing it. If the current depth is lower than the previous, close off one </ul></li> pair for each level of depth difference.
if you already have figured out how to get the depth of an entry (or the number of ancestors that is), then you could try the following:
css code:
.tab {
margin-left: 20px;
}
rendered html code:
<p>
<span class="">1 - blah</span><br />
<span class="">2 - blah</span><br />
<span class="tab">4 - blah</span><br />
<span class="tab">5 - blah</span><br />
<span class="tab tab">6 - blah</span><br />
<span class="">3 - blah</span>
</p>
and you'll use your logic to determine how many "tab" to add to the span's class attribute. each "tab" you add puts that line 20 more pixels to the right.

SQL - Update table changing one column value based on another table

Sorry if the title is not as descriptive as it should be but it is kind of difficult to explain in one sentence what I am trying to do ;).
I have one table that links parent objects with its respective childs. And I have another table with all the objects (parents and childs) with its respectives images. However, the image is just set for the parents objects. I would like to update this last table and set the childs image the same image that is already set for its parent. Besides, as there is more than one image for each object, I would like to set one in particular, which I can know based on an attribute column.
My tables look something like:
RelationTable
child-id
parent-id
ImageTable
object-id
attribute-id
image-url
And here goes an example in order to clarify things:
RelationsTable
child-id | parent-id
3 | 1
4 | 1
5 | 2
ImageTable
object-id | attribute-id | image-url
1 | goodimage | image1.jpg
1 | badimage | image1b.jpg
2 | goodimage | image2.jpg
2 | badimage | image2b.jpg
3 | goodimage | no
3 | badimage | no
4 | goodimage | no
4 | badimage | no
5 | goodimage | no
5 | badimage | no
So, I would like to set the images of objects 3, 4 and 5 (child ones) to its respective parent images, but to the 'correct' ones, that is the images with 'goodimage' as attribute-id.
At the end it should look like:
1 | goodimage | image1.jpg
1 | badimage | image1b.jpg
2 | goodimage | image2.jpg
2 | badimage | image2b.jpg
3 | goodimage | image1.jpg
3 | badimage | no
4 | goodimage | image1.jpg
4 | badimage | no
5 | goodimage | image2.jpg
5 | badimage | no
Actually, I don't care if 'badimage' is set as well, but the important one is 'goodimage'.
I've been trying something like:
UPDATE ImageTable
SET image = (SELECT image-url FROM ImageTable WHERE ImageTable.object-id = RelationTable.parent-id AND ImageTable.attribute-id = 'goodimage')
WHERE ImageTable.object-id = RelationTable.child-id AND ImageTable.attribute-id = 'goodimage'
but it's not working since it is not correct SQL syntax. I don't know if I should use a variable (never used one) or if this can be done with just one SQL sentence.
Any help would be much appreciated.
something like this?
NOTES:
This could be merged into one
non-subquery statement but I was
lazy.
I did not test, expect typos
;WITH goodlist AS
(
SELECT child-id, image-url
FROM relationstable
left join imagetable on relationstable.child-id = relationstable.child-id and attribute-id = "goodimage"
)
UPATE imagetable
set imagetable.image-url =
(SELECT top 1 image-url from goodlist where child-id = imagetable.object-id)
WHERE imagetable.image-url = "no"
First of all, if the relation between parent and child is 1:n, why don't you just add that information to the same table? e.g. fieldname "parentid" (if it's empty/null it's only a parent). Benefit: you don't need an additional table and can easily create a hierarchy if needed.
And for the image, I guess you want to display this somewhere in your code, but it should be easier to just modify the SELECT to get the image-url of the parent is no image is given for the child. Benefit: no need to update the table when you get new entries and it would also be possible to for some child entries to have their own image (if needed).
edit: just noticed your new comment about the open-source project part, of course this makes it hard to change the database design. But maybe it's still easier to move some of the problems to the programming side (unless this is handled by the open-source software as well).