Using for loop value in foreach loop - laravel

I would like to use a value inside a for loop in foreach in blade file. Does anyone know how to do this? I would like to do something like this:
#for ($i = 0; $i < 10; $i++)
#foreach($meals->where('meal_category_id', {{ for loop value }}) as $meal))
#endforeach
#endfor
Thanks for help :)

Did you try:
#for ($i = 0; $i < 10; $i++)
#foreach($meals->where('meal_category_id', '=', $i) as $meal))
{{ $meal }}
#endforeach
#endfor

Related

Insertion sort script on powershell

I am trying to make insertion sort algorithm in case of studying on powershell.
Code:
$TestArrayList = [System.Collections.ArrayList]#(8, 2, 11, 12, 5, 6, 7)
for ($i = 0; $i -lt $TestArrayList.Count; $i++) {
$key = $TestArrayList[$i]
$j = $i-1
while($j -gt 0 -and $key -lt $TestArrayList[$j]){
$TestArrayList[$j+1] = $TestArrayList[$j]
$TestArrayList[$j] = $key
$j = $j-1
#Write-Output $TestArrayList[$i]
}
Write-Output $TestArrayList[$i]
}
Output of the code is next:
8
2
11
12
12
12
12
Can you please help me to analyze, what's the problem. I tried to make it sorted from small to bigger one.
Expected to be sorted:
2, 5, 6, 7, 8, 11, 12
There are two problems with your code right now:
The output is not the finally sorted list, it's intermediate output from the Write-Output statement.
There's an off-by-one bug in the nested while loop that'll cause the first item to always be ignored.
To fix the first issue, simply remove the Write-Output statement from the loop.
To fix the second issue, change the first comparison in the while condition to $j -ge 0 instead of $j -gt 0:
for ($i = 0; $i -lt $TestArrayList.Count; $i++) {
$key = $TestArrayList[$i]
$j = $i - 1
while($j -ge 0 -and $key -lt $TestArrayList[$j]){
$TestArrayList[$j+1] = $TestArrayList[$j]
$TestArrayList[$j] = $key
$j = $j - 1
}
}
# the list is now sorted correctly
$TestArrayList

accessing index array from gotmpl

I have this list of an array and want to print something if index = 0, but print something else after that.
I've looked at this docs but seems not successful... all of them is printing something else instead.
{{ range $i, $v := .Lists }}
{{ if $i = 0 -}}
do something
{{- else -}}
do something else
{{- end -}}
{{- end }}
try == not =.
maybe like this
{{ if $i == 0 -}}
in documment:
And the following comparison operators are also supported: eq: Equal (==)...
https://docs.gomplate.ca/syntax/#functions

How can we fetch images from database to dashboard using CodeIgniter 3

Image path is getting stored in the database and file is getting uploaded in the upload path but the image is not displaying in the tabular format.
Here is the code for the view:
<tbody>
<?php
$query = $this->db->query("SELECT * FROM banner_mgmt LIMIT 1");
// $url = base_url().
foreach ($query->result() as $row)
{
echo "<tr>";
echo "<td>$row->id</td>";
echo "<td>$row->banner_name</td>";
echo "<td>".'<img src="base_url().$row->banner_image" style="height:150px; width:150px;" alt="not found">'."</td>";
echo "<td>$row->banner_image</td>";
echo "</tr>";
}
?>
</tbody>
Assuming that base_url().$row->banner_image does actually result in a correct path/image name...
You need to check your string concatenation.
What you have... (overly complicated)
echo "<td>".'<img src="base_url().$row->banner_image" style="height:150px; width:150px;" alt="not found">'."</td>";
What you should have...
echo '<td><img src ="'.base_url().$row->banner_image.'" style="height:150px; width:150px;" alt="not found"></td>';
You need to think about how you want the string to turn out, which helps you determine when and where to use ', " and . properly.
Here I am wrapping the string using ' (single quotes), which allows me to use " (double quotes) in the HTML properties without much effort.
Read up on how to use base_url() in the Codeigniter user guide...
So you could have
echo '<td><img src ="'.base_url($row->banner_image).'" style="height:150px; width:150px;" alt="not found"></td>';
You could even use " (double quotes) which evaluate php but you will need to escape any 'internal' double quotes you want to display.
echo "<td><img src =\"base_url($row->banner_image)\" style=\"height:150px; width:150px;\" alt=\"not found\"></td>";
So, you need to read the answer at What is the difference between single-quoted and double-quoted strings in PHP?

Unescaped pug attribute not processed by laravel blade

I'm using Laravel Pug https://github.com/BKWLD/laravel-pug, and the file is named *.pug.blade.php. I'm trying to set an attribute value.
| <?php $x = "any"; ?>
div(class!="{{ $x }}")
| {{ $x }}
the output html is
<div class="{{ $x }}">any</div>
Interestingly, in attribute, the php variable is accessed with no < ? php ? > or {{ }} enclosures, just like javascript variables
| <?php $x = "any"; ?>
div(class=$x)
| {{ $x }}
the HTML output
<div class="any">any</div>
Also, the dollar sign can be omitted, and interpolation can be used
| <?php $x = "any"; ?>
div(class = x + "test")
| {{ $x }}
the HTML output
<div class="anytest">any</div>

Create a dynamic query using php inputs through dropdowns

I need to print data from database based on the selection in the dropdowns where three drops down lists are being shown. The user can select either one dropdown list or two or three based on his choice.But the should be based on the selected values in the selected dropdown lists. I'm new to php and I'm a learner. Can anyone sort out the problem here in my code.
if(isset($_POST['submt']))
{
$a = $_POST['prog'];
$b = $_POST['cntr'];
$c = $_POST['sectr'];
$a1 = 'Programme_name';
$b1 = 'Center_name';
$c1 = 'Name_of_trade';
$x=0; $y=0; $p=0; $q=0;
if($a=='' && $b!='' && $c!='') { $x = $b1; $y = $c1; $p = $b; $q = $c; }
if($b=='' && $c!='' && $a!='') { $x = $c1; $y = $a1; $p = $c; $q = $a; }
if($c=='' && $a!='' && $b!='') { $x = $a1; $y = $b1; $p = $a; $q = $b; }
echo $x." ".$y;
mysql_connect("localhost","root","sherk005");
mysql_select_db("erp");
$hai = mysql_query("SELECT * FROM student_master_1 WHERE $x = '$p' AND $y = '$q'");
while(mysql_fetch_row($hai)>0) {
echo $hai['Partner_name'] . " " . $hai['Programme_name'];
echo "<br>";
}
}
You should probably escape your variables in your query to get it right.
$hai = mysql_query("SELECT * FROM student_master_1 WHERE ".$x." = '".$p."' AND ".$y." = '".$q."'");
Consider also using mysqli functions as mysql are deprecated : http://php.net//manual/en/book.mysqli.php

Resources