Joomla database - Get result from different table with results from first table - joomla

Basically i'm trying to get data from a table. When I pull all the row, I get the branch as ID, and i'm trying to cross refferance it with the branch table so I can get the title and display it. What is the proper way of doing this. I don't have much experiance with functions, but i'm thinking a function could come in handy here. Thank you in advance for your help!
<table class="table table-striped">
<tbody>
<tr>
<td><h5>Title</h5></td>
<td><h5>Location</h5></td>
<td><h5>Time</h5></td>
</tr>
<?php
// Get a database object
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__jobs_description');
// sets up a database query for later execution
$db->setQuery($query);
// fetch result as an object list
$result = $db->loadObjectList();
foreach ( $result as $row ) {
echo "<tr>";
echo "<td>";
echo $row->title;
echo "</td>";
echo "<td>";
getbranchtitle($row->branch);
echo "</td>";
echo "<td>";
echo $row->type;
echo "</td>";
echo "</tr>";
}
function getbranchtitle(){
// Get branch name
$db2 = JFactory::getDbo();
$query2
->select($db->quoteName(array('id', 'title')))
->from($db->quoteName('#__vna_jobs_branch'))
->where('id = '. ($row->branch))
->order('ordering ASC');
$result2 = $db->loadObjectList();
echo $result2->title;
}
?>
</tbody>
</table>

This should be solved with a join instead of your current solution:
...
$query = $db->getQuery(true);
$query->select('j.*');
$query->select('b.title as branchtitle');
$query->from('#__jobs_description', 'j');
$query->leftJoin('#__vna_jobs_branch b on (j.branch=b.id)')
// sets up a database query for later execution
$db->setQuery($query);
...
Now branchtitle will be directly available in your foreach-loop
...
echo "<tr>";
echo "<td>";
echo $row->title;
echo "</td>";
echo "<td>";
$row->branchtitle; // from your query join
echo "</td>";
...

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.

In Laravel 6, how to define a custom menu when working without a database? [duplicate]

I was hoping to find a way to create an array with the registered routes paths within Laravel 4.
Essentially, I am looking to get a list something like this returned:
/
/login
/join
/password
I did come across a method Route::getRoutes() which returns an object with the routes information as well as the resources but the path information is protected and I don't have direct access to the information.
Is there any other way to achieve this? Perhaps a different method?
Route::getRoutes() returns a RouteCollection. On each element, you can do a simple $route->getPath() to get path of the current route.
Each protected parameter can be get with a standard getter.
Looping works like this:
$routeCollection = Illuminate\Support\Facades\Route::getRoutes();
foreach ($routeCollection as $value) {
echo $value->getPath();
}
You can use console command:
Laravel 4 as asked in question
php artisan routes
Laravel 5 more actual
php artisan route:list
Helpers (Laravel 4) :
Usage:
routes [--name[="..."]] [--path[="..."]]
Options:
--name Filter the routes by name.
--path Filter the routes by path.
--help (-h) Display this help message.
--quiet (-q) Do not output any message.
--verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
--version (-V) Display this application version.
--ansi Force ANSI output.
--no-ansi Disable ANSI output.
--no-interaction (-n) Do not ask any interactive question.
--env The environment the command should run under.
For Laravel 5, you can use artisan command
php artisan route:list instead of php artisan routes.
I created a route that will list each route and its respective details in an html table.
Route::get('routes', function() {
$routeCollection = Route::getRoutes();
echo "<table style='width:100%'>";
echo "<tr>";
echo "<td width='10%'><h4>HTTP Method</h4></td>";
echo "<td width='10%'><h4>Route</h4></td>";
echo "<td width='10%'><h4>Name</h4></td>";
echo "<td width='70%'><h4>Corresponding Action</h4></td>";
echo "</tr>";
foreach ($routeCollection as $value) {
echo "<tr>";
echo "<td>" . $value->getMethods()[0] . "</td>";
echo "<td>" . $value->getPath() . "</td>";
echo "<td>" . $value->getName() . "</td>";
echo "<td>" . $value->getActionName() . "</td>";
echo "</tr>";
}
echo "</table>";
});
Improving #jeanfrg's answer
It has a few deprecated functions. It shows error while editing an answer, hence posting it here.
Laravel 6, 7 & 8
Put it inside routes/web.php
Route::get('routes', function () {
$routeCollection = Route::getRoutes();
echo "<table style='width:100%'>";
echo "<tr>";
echo "<td width='10%'><h4>HTTP Method</h4></td>";
echo "<td width='10%'><h4>Route</h4></td>";
echo "<td width='10%'><h4>Name</h4></td>";
echo "<td width='70%'><h4>Corresponding Action</h4></td>";
echo "</tr>";
foreach ($routeCollection as $value) {
echo "<tr>";
echo "<td>" . $value->methods()[0] . "</td>";
echo "<td>" . $value->uri() . "</td>";
echo "<td>" . $value->getName() . "</td>";
echo "<td>" . $value->getActionName() . "</td>";
echo "</tr>";
}
echo "</table>";
});
Demo:
Access it via <url>/routes
//Laravel >= 5.4
//Controller index()
$app = app();
$routes = $app->routes->getRoutes();
return view ('Admin::routes.index',compact('routes'));
//view
<table id="routes-table" class="table table-bordered table-responsive">
<thead>
<tr>
<th>uri</th>
<th>Name</th>
<th>Type</th>
<th>Method</th>
</tr>
</thead>
<tbody>
#foreach ($routes as $route )
<tr>
<td>{{$route->uri}}</td>
<td>{{$route->getName()}}</td>
<td>{{$route->getPrefix()}}</td>
<td>{{$route->getActionMethod()}}</td>
</tr>
#endforeach
</tbody>
</table>
A better way to get it readable is to register a route and print it in web browser with the artisan output directly
Route::get('routes', function() {
\Artisan::call('route:list');
return \Artisan::output();
});
if you have compiled routes like /login/{id} and you want prefix only:
foreach (Route::getRoutes() as $route) {
$compiled = $route->getCompiled();
if(!is_null($compiled))
{
var_dump($compiled->getStaticPrefix());
}
}
Code
Laravel <= 5.3
/** #var \Illuminate\Support\Facades\Route $routes */
$routes = Route::getRoutes();
foreach ($routes as $route) {
/** #var \Illuminate\Routing\Route $route */
echo $route->getPath() . PHP_EOL;
}
Laravel >= 5.4
/** #var \Illuminate\Support\Facades\Route $routes */
$routes = Route::getRoutes();
foreach ($routes as $route) {
/** #var \Illuminate\Routing\Route $route */
echo $route->uri. PHP_EOL;
}
Artisan
Laravel 4
php artisan routes
Laravel 5
php artisan route:list
$routeList = Route::getRoutes();
foreach ($routeList as $value)
{
echo $value->uri().'<br>';
}
use Illuminate\Support\Facades\Route;
On Laravel 5.4, it works, 100 %
Console command for those who use Oh-my-zsh with Laravel 5 plugin
la5routes
For Laravel 5.4.* This code works fine.
Route::get('routes', function() {
$routeCollection = Route::getRoutes();
echo "<table style='width:100%'>";
echo "<tr>";
echo "<td width='10%'><h4>HTTP Method</h4></td>";
echo "<td width='10%'><h4>Route</h4></td>";
echo "<td width='10%'><h4>Name</h4></td>";
echo "<td width='70%'><h4>Corresponding Action</h4></td>";
echo "</tr>";
foreach ($routeCollection as $value) {
echo "<tr>";
echo "<td>" . $value->methods()[0] . "</td>";
echo "<td>" . $value->uri() . "</td>";
echo "<td>" . $value->getName() . "</td>";
echo "<td>" . $value->getActionName() . "</td>";
echo "</tr>";
}
echo "</table>";
});
Not all the routes are available all the time.
For example if you want to get the routes from the RouteServiceProvider then you might need to use the booted callback:
$this->booted(function () {
dump(Route::getRoutes());
}

What am I missing in my foreach and/or echo

I am trying to cycle through the results of my query to display in table format on my page.
What am I missing in my foreach and my echo to cycle through the results in my display table.
{source 0}
<?php
$user =& JFactory::getUser();
if (!$user->guest)
$name = $user->username;
$db =& JFactory::getDbo();
$query = $db->getQuery(true);
$query = "
SELECT image_url
FROM #__image_data
WHERE user_name = '$name'";
$db->setQuery($query);
$list = $db->loadObjectList();
foreach ($list as $item){
$item->image_url;
}
?>
<td><?php echo $item->image_url;?></td><br/>
<td><?php echo $item->image_url;?></td><br/>
<td><?php echo $item->image_url;?></td><br/>
{/source}
{source 0}
<?php
$user =& JFactory::getUser();
if (!$user->guest)
$name = $user->username;
$db =& JFactory::getDbo();
$query = $db->getQuery(true);
$query = "
SELECT image_url
FROM #__image_data
WHERE user_name = '$name'";
$db->setQuery($query);
$list = $db->loadObjectList();
foreach ($list as $item){
echo "<td>".$item->image_url."</td><br/>";
}
?>
{/source}
You can easily loops through and get the <td> as below.
Method 1 - Here you don't have to concatenate since you are using ""
$list = $db->loadObjectList();
foreach ($list as $item){
echo "<td> $item->image_url </td><br/>";
}
Method 2
$list = $db->loadObjectList();
foreach ($list as $item){
echo '<td>'.$item->image_url'.</td><br/>';
}

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.

Code Igniter Generating Query Result

I'm having trouble generating my query result.
$query = $this->db->query("YOUR QUERY");
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
Will this work?
$query = $this->db->query("YOUR QUERY");
foreach ($query->result_array() as $row)
{
echo $row['0'];
echo $row['1'];
echo $row['2'];
}
Thank you.
As Edward mentioned, the array returned is an associative array. While there is no standard way to index into assoc array using integers, you could do it this way:
$resultarray = $query->result_array();
// get an array of keys in result
$keys = array_keys($resultarray[0]);
foreach ($resultarray as $row)
{
echo $row[$keys[0]];
echo $row[$keys[1]];
echo $row[$keys[2]];
}
$query->result_array() generates an associative array. You can't access it's elements by index.

Resources