CodeIgniter database error (SUM) - database

Hello i've problem like this
this is my model:
$sql = "SELECT saldo.kode, saldo.nama, SUM(bayar.jmlh_bayar) FROM saldo, bayar WHERE saldo.nama = bayar.akun ORDER BY saldo.nama";
$result = $this->db->get($sql);
return $result->result();
this is controller:
public function show()
{
$this->load->model('uas/sampah_model');
$terserah['c']= $this->sampah_model->get();
//$terserah['kode']=$this->sampah_model->kode();
$this->load->view('UAS/saldo',$terserah);
}
this is my view:
<tr>
<?php
foreach ($c as $item){
echo "<td>";
echo $item->kode;
echo "</td>";
echo "<td>";
echo $item->nama;
echo "</td>";
echo "<td>";
echo $item->jmlh_bayar;
echo "</td>";
echo "<td>";
echo anchor('UAS/utama/hapus/'.$item->kode,'Hapus');
echo "</td>";
echo "</tr>";
}
?>
Can anybody help me?

Hope this will help you :
Instead of get use query
Your query should be like this :
$sql = "SELECT saldo.kode, saldo.nama, SUM(bayar.jmlh_bayar)
FROM saldo, bayar WHERE saldo.nama = bayar.akun
ORDER BY saldo.nama";
$result = $this->db->query($sql);
return $result->result();

Related

how can i combine href with id number

Trying to get some data from db but I can't use href with id number in code.
I tried everything but coldn't make it.
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array($db->quoteName('title')));
$query->select(array($db->quoteName('id')));
$query->select(array($db->quoteName('start_date')));
$query->select(array($db->quoteName('end_date')));
$query->from($db->quoteName('#__rbid_auctions'));
$db->setQuery($query);
$results = $db->loadObjectList();
// display the results
foreach ( $results as $result) {
echo <<<HTML
$result->title .
HTML;
echo "<p>" . $result->start_date . "</p>";
echo "<p>" . $result->end_date . "</p>";
}
?>
I will be appreciated if someone help me.
Thanks in advance
Demonstration of issue then my suggested solution: (Online Demo)
$result = new stdClass();
$result->id = 333;
$result->title = 'title text';
echo <<<HTML
$result->title .
HTML;
Output:
Notice: Undefined variable: id in /in/s1YZG on line 7
title text .
Notice that $id isn't a declared variable. If it was, it would be rendered but all characters between the curly braces are treated literally because you are trying to echo within an echo.
Without heredoc syntax (heredoc can be funny about tabbing depending on php version):
echo "{$result->title} . "; // curly braces may help IDEs with highlighting
New Output:
title text .
As for your query building syntax...
You can save some typing and chain the method calls onto getQuery().
None of the quoteName() calls are necessary for stability/security, but if you insist on toeing Joomla's preferred practices, you can call quoteName() on the array in select().
Suggested Code:
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName(array('title', 'id', 'start_date', 'end_date')))
->from($db->quoteName('#__rbid_auctions'));
$db->setQuery($query);
if (!$results = $db->loadObjectList()) {
echo "No results";
} else {
foreach ($results as $row) {
echo "{$row->title} . ";
echo "<p>{$row->start_date}</p>";
echo "<p>{$row->end_date}</p>";
}
}
Here is another post where loadObjectList() is called after a SELECT query which includes query error checking: https://joomla.stackexchange.com/a/22963/12352
When you have Joomla questions, please post them on Joomla Stack Exchange.
I would try it like this:
also you are trying to echo $id which isnt assigned. should be $results->id
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array('title', 'id', 'start_date', 'end_date' ));
$query->from($db->quoteName('#__rbid_auctions'));
$db->setQuery($query);
$results = $db->loadObjectList();
// display the results
foreach ( $results as $key => $result) {
echo ' <a href="index.php?option=com_rbids&task=viewbids&id='.$result->id .'>'.$result->title .'</a>';
echo "<p>" . $result->start_date . "</p>";
echo "<p>" . $result->end_date . "</p>";
}
?>

Need help in printing records from database

I want to print records from db like this.
http://www.imagesup.net/dm-613781138202.png
I have tried for loop and foreach both.
The sample code is:
<?php
$str = '1,2,3,4,5,6,7,8,9,10,11,12,13,14';
$str2 = (explode(",",$str));
echo '<table border="1">';
foreach ($str2 as $str3)
{
echo '<tr>';
for($i=0;$i<5;$i++)
{
echo '
<td>'.$str3.'</td>
';
}
echo '</tr>';
}
echo '</table>';
?>
I have tried many others but not getting required result.
#kami you should replace + with . like this.
<?php
$str = '1,2,3,4,5,6,7,8,9,10,11,12,13,14';
echo '<table>';
$list = explode(",", $str);
$itemsPerRow = 7;
for ($i = 0; $i < sizeof($list); $i+=$itemsPerRow)
{
echo '<tr>';
for($j = 0; $j < $itemsPerRow; $j++)
{
$val = isset($list[$i + $j]) ? $list[$i + $j] : '';
echo '<td>' .$val. '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
Try something like this
<?php
$str = '1,2,3,4,5,6,7,8,9,10,11,12,13,14';
echo '<table>';
$list = explode(",", $str);
$itemsPerRow = 7;
for ($i = 0; $i < sizeof($list); $i+=$itemsPerRow)
{
echo '<tr>';
for($j = 0; $j < $itemsPerRow; $j++)
{
$val = isset($list[$i + $j]) ? $list[$i + $j] : '';
echo '<td>' . $val . '</td>';
}
echo '</tr>'
}
echo '</table>'
?>
The above uses two for loops to iterate over the data. The outer loop controls the row, and the inner loop controls the content.

How to get a list of registered route paths in Laravel?

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());
}

Communicating with a DB in LimeJS

What is the proper way to load variables into my game from a DB?
I tried using Ajax and the Prototype library, but that doesn't seem to work. Here's what I did:
In my main .js game file...
var rawVocab = new Array();
var optionVocab = new Array();
new Ajax.Request('load_vocab.php', {
onSuccess : function(xmlhttp) {
eval(xmlhttp.responseText);
}
});
'load_vocab.php' looks like this...
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$username = "user";
$password = "***************";
try {
$conn = new PDO('mysql:host=localhost;dbname=tygrif_school', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('SELECT * FROM vocabulary_game WHERE game_type = :game_type');
$stmt->execute(array('game_type' => 'target'));
$i=0;
while($row = $stmt->fetch()) {
echo "rawVocab[".$i."]['word']='".$row['word']."';";
echo "rawVocab[".$i."]['translation']='".$row['translation']."';";
echo "rawVocab[".$i."]['example_sentence_1']='".$row['example_sentence_1']."';";
$i++;
}
$stmt = $conn->prepare('SELECT * FROM vocabulary_game');
$stmt->execute(array());
$i=0;
while($row = $stmt->fetch()) {
echo "optionVocab[".$i."]['word']='".$row['word']."';";
echo "optionVocab[".$i."]['translation']='".$row['translation']."';";
echo "optionVocab[".$i."]['example_sentence_1']='".$row['example_sentence_1']."';";
$i++;
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo '</response>';
?>
Is there some built in way to handle this with the goog library?
Apparently, Google Closure has a built-in way (goog.net.XhrIo) to handle Ajax calls.
1 - http://docs.closure-library.googlecode.com/git/class_goog_net_XhrIo.html
2 - http://www.daveoncode.com/2009/11/17/goog-net-xhrio-make-simple-ajax-calls-with-google-closure/

cakePHP: how to get iterator

Im using something similar to this code:
<?php foreach ($items as $item): ?>
<div><?php echo $item['Item']['content']; ?></div>
<?php endforeach; ?>
And i'd like to know which element is every item, because i want to add class "last-in-row" for every fourth item in row. How to make something like this code?
<?php for ($i=1; $i <= $items.count; $i++) {
echo "<div ";
if ($i % 4 == 0) {
echo " class=\"last-in-row\""; }
echo ">$items[$i]</div>";
}; ?>
I haven't tested it but the following should work.
<?php
$i = 1;
foreach ($items as $item) {
$class = ($i % 4 == 0) ? '' : 'class="last-in-row"';
echo "<div $class>{$item['Item']['content']}</div>";
$i++;
}
?>
p.s. I hope you are sanitizing $item['Item']['content'].
What you are trying to do could be done with css3. This will mean you do not need to add a class which is fat better as later on you might want 3 or 5 in a row.
div:nth-child(4n+4) {
....
clear: both;
....
}
The CakePHP option without css3
foreach ($items as $i => $item) {
echo $this->Html->tag('div', $item['Item']['content'], array(
'class' => ($i + 1) % 4 === 0 ? 'last' : null
));
}
You nearly had it in your second example, just change $items.count to the value of count($items)
$itemsCount = count($items);
for ($i=1; $i <= $itemsCount ; $i++) {
echo "<div ";
if ($i % 4 == 0) {
echo " class=\"last-in-row\""; }
echo ">{$items[$i]}</div>";
}

Resources