Translate code to laravel blade - laravel-5

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

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 use #for loop in laravel controller

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>";
}
}

How to get url of a category in magento

am trying to display all the second level categories and my code is
<?php $storeId = Mage::app()->getStore()->getId();
//Get category model
$_category = Mage::getModel('catalog/category')->setStoreId($storeId);
$_categoryCollection = $_category->getCollection();
$_categoryCollectionIds = $_categoryCollection->getAllIds();
//Remove root category from array
unset($_categoryCollectionIds[0], $_categoryCollectionIds[1]);
?>
<div id="accordian_hover">
<?php
$o = null;
$o .= '<ul>';
foreach ($_categoryCollectionIds as $catId) {
$_category = $_category->load($catId);
if($_category->getLevel() == 2) {
$catChildren = $_category->getChildren();
if(!empty($catChildren)) {
$o .= '<li> '.$_category->getName().'';
$o .= '<ul>';
$categoryChildren = explode(",", $catChildren);
foreach ($categoryChildren as $categoryChildId) {
/* #var $_childCategory Mage_Catalog_Model_Category */
$_childCategory = $_category = Mage::getModel('catalog/category')->setStoreId($storeId)->load($categoryChildId);
$o .= '<li>'.$_childCategory->getName().'</li>';
// If you wish to display the total number of products for each category, uncomment this line
// $o .= '<span class="totalNumberOfProducts">'.$_childCategory->getProductCollection()->count().'</span>';
}
$o .= '</ul></li>';
}
}
} $o .='</ul>';
echo $o;
?>
</div>
But the top menu url is wrong all other are displaying correct but the main category url is wrong(2nd category) Please help me , and i have to display the third level menu also when the user hover on category...
Live Link http://toolskaart.com/
Hope the below code will solve your problem.
Mage::getModel('catalog/category')->load($_category->getId())->getUrl()
It works for me.

Codeigniter search with pagination error

Hello I have a problem with my search in CI. In the first page the results are displayed ok, but on second page it shows a 1064 SQL error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND cpt_echip.cpt_echip_nr_inventar LIKE '%%' LIMIT 10 , 10' at line 1
Here is the code (functions that are used):
MODEL:
function search($start, $limit){
$match = $this->input->post('search');
$tip_echip = $this->input->post('cpt_tip_echip_nume');
$q = $this->db->query("SELECT * FROM (cpt_echip)LEFT OUTER JOIN cpt_utl ON cpt_utl.cpt_utl_marca = cpt_echip.cpt_utl_marca WHERE cpt_echip.cpt_tip_echip_id = $tip_echip AND cpt_echip.cpt_echip_nr_inventar LIKE '%$match%' LIMIT $start , $limit");
$rezultat = $q->result();
return $rezultat;
}
function num_filter(){
$tip_echip = $this->input->post('cpt_tip_echip_nume');
$this->db->where('cpt_tip_echip_id', $tip_echip);
$q = $this->db->get('cpt_echip');
$number = $q->num_rows();
return $number;
}
CONTROLLER:
function search(){
$data = $this->helpdesk_model->general();
$start_row = $this->uri->segment(3);
$per_page = 10;
if(trim($start_row) == ""){
$start_row = 0;
}
$config['base_url'] = base_url() . '/index.php/helpdesk/search/';
$config['total_rows'] = $this->helpdesk_model->num_filter();
$config['per_page'] = $per_page;
$config['num_links'] = 10;
$config['first_link'] = 'Prima pagina';
$config['last_link'] = 'Ultima pagina';
$this->pagination->initialize($config);
$data['pagini'] = $this->pagination->create_links();
$data['query'] = $this->helpdesk_model->search($start_row, $per_page);
$this->load->view('rezultat', $data);
}
VIEW:
<table border="1">
<tr>
<th>Nr. Inventar</th>
<th>Nume</th>
<th>Utilizator</th>
</tr>
<?php foreach($query as $row): ?>
<tr>
<td><?php echo anchor('helpdesk/detalii_echipament/'.$row->cpt_echip_nr_inventar, $row->cpt_echip_nr_inventar, array('data-fancybox-type'=>'iframe', 'class'=>'fancybox')); ?></td>
<td><?php echo $row->cpt_echip_nume; ?></td>
<td><?php echo $row->cpt_utl_nume; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $pagini; ?>
Without the search and filter, the pagination works fine.
It means the $_POST array is empty. There will not be anything posted when you navigate using links to the second page. You can pass the search data by url or store in session at first submission and use it.
Just check using print_r($_POST) to see what is there $_POST array.

Having Problem with $_get in Codeigniter

I have a page called list.php which retrieves from database and shows all the student names with their respective ids in a list. In raw php, this is how I have done this-
include("connect.php");
$query = "SELECT * FROM marks ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
mysql_close();
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$studentname = mysql_result($result,$i,"studentname");
$studentid = mysql_result($result,$i,"studentid");
?>
And then---
<? echo $studentname ?>
<?
++$i; } } else { echo "No Record Found"; }
?>
When a user clicks on any of the student names, it takes the user to the page of that particular student and in that page I have a code like following-
include("connect.php");
$number = $_GET['studentid'];
$qP = "SELECT * FROM student WHERE studentid = '$number' ";
$rsP = mysql_query($qP);
$row = mysql_fetch_array($rsP);
extract($row);
$studentid = trim($studentid);
$studentname = trim($studentname);
$studentgender = trim($studentgender);
The above code is working just fine. Now as far as I know $_get is disable in Codeigniter. But how to do the exact same thing that I mentioned above in codeigniter if $_get is disabled ? I went through some tutorials on alternative way of using $_get, but I didn't understand those well. Would you please kindly help? Thanks in Advance :)
The usage of $_GET is discouraged in CI.
The simpliest way to rewrite that files is not using $_GET. Just add method='post' to your forms and tell your ajax requests to use post, if you have any. Use $_POST in PHP instead of $_GET.
If you are absolutely sure you need get requests passing parameters, you have to enable query strings. You can do it in your CI's config file:
$config['enable_query_strings'] = TRUE;
See section 'Enabling query strings' for details. But enabling query strings will cause Url helper and other helpers that generate URLs to malfunction.
So my suggestion is to use POST requests.
UPDATE Replace
<? echo $studentname ?>
With
<? echo $studentname ?>
Create method studentprofile:
<?php
class Yourcontroller extends CI_Controller {
public function studentprofile($id)
{
include("connect.php");
$number = $id;
$qP = "SELECT * FROM student WHERE studentid = '$number' ";
$rsP = mysql_query($qP);
$row = mysql_fetch_array($rsP);
extract($row);
$studentid = trim($studentid);
$studentname = trim($studentname);
$studentgender = trim($studentgender);
// and so on...
}
}
?>

Resources