How to use #for loop in laravel controller - laravel

Currently I have this code in my blade file
#for ($i = 0; $i < count($orderProduct->column['Position']); $i++)
<p>{{ $orderProduct->column['Position'][$i] }}</p>
<p>{{ $orderProduct->column['Color'][$i] }}</p>
#endfor
How do I use the #for loop in controller so that it outputs the same result?
I tried something like this
foreach ($i = 0; $i < count($orderProduct->column['Position']); $i++) {
$message .= "<p>" . $orderProduct->column['Position'][$i] . " : " . $orderProduct->column['Color'][$i] . "</p>";
}
But it doesn't work.

foreach is for looping through arrays and other iterable objects, so it doesn't work with the same parameters as a for loop.
Simply change foreach to for.
for ($i = 0; $i < count($orderProduct->column['Position']); $i++) {
$message .= "<p>" . $orderProduct->column['Position'][$i] . " : " . $orderProduct->column['Color'][$i] . "</p>";
}

Firest you have error in your json data
$orderProduct = {"Position":["first","second","third","fourth"],"Color":["Red",Blue,Pink,Teal]}
Blue,Pink,Teal doenst contain single quote or double quote.
So it should be
$orderProduct = '{"Position":["first","second","third","fourth"],"Color":["Red","Blue","Pink","Teal"]}';
then you can use foreach like below
$orderProduct = '{"Position":["first","second","third","fourth"],"Color":["Red","Blue","Pink","Teal"]}';
$data=json_decode($orderProduct);
foreach ($data as $key=>$value){
echo "<h4>".$key."</h4>";
echo implode("<br>",$value);
}
This will ouput like below
Position
first
second
third
fourth
Color
Red
Blue
Pink
Teal
Also you can avoid implode if you want
foreach ($data as $key=>$value){
echo "<h4>".$key."</h4>";
foreach ($value as $subkey=>$subvalue){
echo $subvalue."<br>";
}
}

Related

How to count downlines in Laravel?

$count = 0;
$query = User::where('sponser_id',$_SESSION['ruserid'])->get();
//echo count($query);
$count2=count($query);
//die();
$count=0;
foreach($query AS $objChild)
{
$query2 = User::where('sponser_id',$objChild->sponser_id)->get();
$count=$count+count($query2);
echo $query2[0];
echo "<br>";
echo "====";
}
return $count+$count2;
How to convert above php code in laravel controller to get downlines?
It run only till first query count.

How to convert MySqli Loop Code Laravel controller

I want to work on a while loop in a foreach loop but I can't convert it to Laravel controller system. Please help.
foreach (range('a', 'z') as $i)
{
echo $i = strtolower($i);
$sql = "SELECT * FROM `list` Where name like '$i%' Limit 30";
$result = mysqli_query($con, $sql);
echo"<hr><h3>Name Starting with ".strtoupper($i)."</h3>";
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$id = $row['id'];
$name = $row['name'];
echo "<li>".$row['name']."</li>";
}
echo "<li>More</li>";
echo "</ul>";
}
The code in Laravel that I have is
foreach (range('a', 'z') as $i)
{
$list = DB::table('list')->where('name', 'LIKE', $i.'%')->limit(30)->get();
return view('pages.all', ['list' => $list]);
}
This code only gives data for names starting with alphabet "a" but won't proceed with the rest of the characters.
Store it on the array based on your search.
$list = array();
foreach (range('a', 'z') as $i) {
$list[$i] = DB::table('list')->where('name', 'LIKE', $i.'%')->limit(30)->get();
}
return view('pages.all', ['list' => $list]);
Customize your view, to showing the list against each character (a-z).
[Edit]
View file can be changed like below,
#foreach ($list as $keyI => $moredata)
<hr>
<h3>Author's Name Starting with "{{strtoupper($keyI)}}"</h3>
<ul class="tags-list">
#foreach ($moredata as $data)
<li>{{$data->name}}</li>
#endforeach
</ul>
#endforeach

Translate code to laravel blade

My condition in view blade cause I ussually used native php coding :
$jlhkriteria = Kriteria::count();
kriterias table : id_kriteria, kriteria_name
#for ($i = 1; $i <= $jlhkriteria; $i ++)
<?php $queri1 = mysqli_query($mysqli, "SELECT * FROM kriterias WHERE id_kriteria='$i' ORDER BY id_kriteria ASC");
$kriteria1 = mysqli_fetch_array($queri1, MYSQLI_ASSOC); ?>
#for ($j = $i + 1; $j <= $jlhkriteria ; $j ++)
<?php $queri2 = mysqli_query($mysqli, "SELECT * FROM kriterias WHERE id_kriteria='$j' ORDER BY id_kriteria ASC");
$kriteria2 = mysqli_fetch_array($queri2, MYSQLI_ASSOC); ?>
<?php echo $i . $j; ?>
<?php echo $kriteria1['kriteria_name']; ?>
<?php echo $kriteria2['kriteria_name']; ?>
#endfor
#endfor
In Laravel 5 I used this code because I think query in View are bad ideas, any ideas to make this in MVC like without query it in Blade view.
You will need to create model and controller classes that contain your business logic then pass the data to a view. Something similar to that:
Model:
class Kriteria extends Model
{
protected $table = 'kriterias';
}
Controller:
class KriteriaController extends Controller
{
public function index()
{
$kriteria = Kriteria::orderBy('id_kriteria')->get();
return view('index', compact('kriteria'));
}
}
View:
#for($i=0; $i < count($kriteria); $i ++)
#for($j = $i + 1; $j < count($kriteria) ; $j ++)
{{ $i . $j }}
{{$kriteria[$i]->kriteria_name.' '.$kriteria[$j]->kriteria_name}}
#endfor
#endfor

magento category search

I have two main child under navigation in category tree as below images
I want to list category and subcategories for either Region or Activities, I have tried two different function for that
$collection = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('name')->load();
foreach ($collection as $cat) {
echo $cat->getName();
}
this will return all child from both Region and Actities and another function
$children = Mage::getModel('catalog/category')->getCategories(10);
foreach ($children as $category) {
echo $category->getName();
}
Reference link
this return only subcategories
I want to list all category, sub-categories and sub-subcategory and so on... for Region
Thank in advance
Try this one:
$rootcatId= 10;
$categories = Mage::getModel('catalog/category')->getCategories($rootcatId);
function get_categories($categories) {
$array= '<ul>';
foreach($categories as $category) {
$cat = Mage::getModel('catalog/category')->load($category->getId());
$count = $cat->getProductCount();
$array .= '<li>'.
'<a href="' . Mage::getUrl($cat->getUrlPath()). '">' .
$category->getName() . "(".$count.")</a>\n";
if($category->hasChildren()) {
$children = Mage::getModel('catalog/category')->getCategories($category->getId());
$array .= get_categories($children);
}
$array .= '</li>';
}
return $array . '</ul>';
}
echo get_categories($categories);

CodeIgniter - Checking to see if a radio button is checked in the database

Im having a bit of trouble putting some code together ... What im trying to do is add some code to the code i have at the moment to check radiobuttons that are checked in the database.
The code i have at the moment takes all roles from the database, outputs them using a foreach statement, but also splits the results into 2 columns, this is what i have at the moment.
<?php
$i = 0;
$output = "";
foreach($roles as $row){
if($i > 0){
$i = 0;
}
if($i == 0) {
$output .= "<div class='box'>";
}
$output .= '<div class="row">';
$output .= ' <input name="_'.$row->key.'" type="radio" id="'.$row->key.'" class="radio" />';
$output .= ' <label for="'.$row->key.'" style="text-transform: lowercase;">'.$row->name.'</label>';
$output .= '</div>';
if($i ==0) {
$output .= "</div>";
}
$i++;
}
if($i != 1) {
$output .= "</div>";
}
echo $output;
?>
Ok, so what i want to do is check the radio button in the code that i posted, only when there is a match in the database, So to get the values that were checked by the user, i use the following.
Model
function get_roles_by_id($freelancerid)
{
$query = $this->db->query('SELECT * FROM '.$this->table_name.' WHERE user_id = "'.$freelancerid.'"');
return $query->result();
}
Then my controller looks like this
$data['positions'] = $this->freelancer_roles->get_roles_by_id($freelancer_id);
As that is bring back an array i cant use a foreach statement to check the radio button id's that are returned in the positions array.
Could someone help me to figure this out.
Cheers,
I think I understand your question and it seems a fairly simple thing you are trying to do. If
You should have your model only return an array of the names of the checkboxes saved by the user in the following format: array("checkbox1", "checkbox2", "checkbox3") then in your output
you can simply use the native php function in_array()
for example:
$output .= '<div class="row">';
$output .= ' <input name="_'.$row->key.'" type="radio" id="'.$row->key.'" class="radio"';
if(in_array($row->name, $data['positions']) { $output .= ' checked '; }
$output . = '/>';
$output .= ' <label for="'.$row->key.'" style="text-transform: lowercase;">'.$row->name.'</label>';
$output .= '</div>';
As a side note, you have the following code:
if($i > 0){
$i = 0;
}
if($i == 0) {
$output .= "<div class='box'>";
}
If you follow the logic in that code you will see that $i will always equal 0 for the second if statement, making both if statements redundant.

Resources