I have a question. How do I do to rescue the variable $row in this function ListarGeral(). so I can use the function EditarGeral() . I tried to do, but nothing happened
here's what I tried to do:
<?php
include ('Banco.php');
Class GeralControle {
private $consulta;
private $row = array();
public function GeraloDao() {
$novaconexao = new Banco();
$this->consulta = $novaconexao->conectar();
}
public function ListarGeral() {
$this->GeraloDao();
echo '<table border="1" cellspacing="2">
<tr>
<td>ID</td>
<td>CPF</td>
</tr>';
if ($resultado = $this->consulta->query("select * from cadastro")) {
while ($this->row = $resultado->fetch_row()) {
echo '<tr>
<td>'.$this->row[0].'</td>
<td>'.$this->row[1].'</td>
</tr>';
echo $this->row[0];
}
$resultado->close();
}
echo '</table>';
}
public function EditarGeral(){
$this->ListarGeral();
echo $this->row[0];
}
}
$pessoa2 = new GeralControle();
$pessoa2->ListarGeral();
$pessoa3 = new GeralControle();
$pessoa3->EditarGeral();
Thank you!
if ($resultado = $this->consulta->query("select * from cadastro")) {
$rows = array();
while ($this->row = $resultado->fetch_row()) {
echo '<tr>
<td>'.$this->row[0].'</td>
<td>'.$this->row[1].'</td>
</tr>
';echo $this->row[0];
array_push($rows, $this->row);
}
EditarGeral($rows);
}
Related
I want to know how to include variables within sql query (PDO) and how to send values into these variables.
because I want to have select options with corresponding values (which are table names in the DB). And when an option is clicked, value will be sent to a declared variable $taxability.
Please be gentle with my delicate brain guys. I really want to learn. It's just that there are times that I really don't understand. :) Thanks!
Question: how to send VALUES of each OPTION, when clicked, to $taxability variable.
please help me to reconstruct something in my code. Thanks
database.php
<?php
class Database
{
private static $dbName = 'reports' ;
private static $dbHost = 'localhost' ;
private static $dbUsername = 'root';
private static $dbUserPassword = '';
private static $cont = null;
public function __construct() {
die('Init function is not allowed');
}
public static function connect()
{
// One connection through whole application
if ( null == self::$cont )
{
try
{
self::$cont = new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName,
self::$dbUsername,
self::$dbUserPassword);
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
return self::$cont;
}
public static function disconnect()
{
self::$cont = null;
}
}
?>
tables.php
<form action="POST">
Month:
<select name="month">
<option value="2017_oct">Oct</option>
<option value="2017_nov">Nov</option>
<option value="2017_dec">Dec</option>
</select>
</form>
<table width="100%" class="table table-striped table-bordered table-hover" id="tbltotal">
<thead>
<tr>
<th>DISTRICT</th>
<th>RPU</th>
<th>AREA</th>
<th>MARKET VALUE</th>
<th>ASSESSED VALUE</th>
</tr>
</thead>
<tbody>
</tbody>
<?php
include 'database.php';
$pdo = Database::connect();
$taxability= "";
$sql = ('SELECT
substring(pin,5,2) as District,
count(arpn) as RPU,
sum(area) as AREA,
sum(marketvalue) as MV,
sum(assessedvalue) as AV
FROM **$tablename**
WHERE taxability="$taxability"
group by District ASC');
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['District'] . '</td>';
echo '<td class="RPU">'. $row['RPU'] . '</td>';
echo '<td class="AREA">'. $row['AREA'] . '</td>';
echo '<td class="MV">'. $row['MV'] . '</td>';
echo '<td class="AV">'. $row['AV'] . '</td>';
}
Database::disconnect();
?>
<script type="text/javascript">
var tds = document.getElementById('tbltotal').getElementsByTagName('td');
var sum = 0; sum2 = 0; sum3 = 0;sum4 = 0;
for(var i = 0; i < tds.length; i ++) {
if(tds[i].className == 'RPU') {
sum += isNaN(tds[i].innerHTML) ? 0 : parseInt(tds[i].innerHTML);
}
if(tds[i].className == 'AREA') {
sum2 += isNaN(tds[i].innerHTML) ? 0 : parseInt(tds[i].innerHTML);
}
if(tds[i].className == 'MV') {
sum3 += isNaN(tds[i].innerHTML) ? 0 : parseInt(tds[i].innerHTML);
}
if(tds[i].className == 'AV') {
sum4 += isNaN(tds[i].innerHTML) ? 0 : parseInt(tds[i].innerHTML);
}
}
document.getElementById('tbltotal').innerHTML += '<tr style="font-weight:bold;"><td >total</td><td>' +
sum + '</td><td>' + sum2 +'.00</td><td>' + sum3+ '</td><td>'+sum4+'</td></tr>';
</script>
</table>
I want to delete the image not only in database, but in folder too.
this is my model
public function delete($id)
{
if ($this->db->delete("np_gallery", "id = ".$id))
{
return true;
}
}
this is my controller
public function delete_image($id)
{
$this->np_gallery_model->delete($id);
$query = $this->db->get("np_gallery");
$data['records'] = $query->result();
$this->load->view('admin/gallery/gallery_listing',$data);
}
this is my view
<table class="table table-bordered">
<thead>
<tr>
<td>Sl No</td>
<td>Tag</td>
<td>Image</td>
<td>Action</td>
</tr>
</thead>
<?php
$SlNo=1;
foreach($records as $r)
{
?>
<tbody>
<tr>
<?php $image_path = base_url().'uploads';?>
<td><?php echo $SlNo++ ; ?></td>
<td><?php echo $r->tag; ?></td>
<td><img src="<?php echo $image_path; ?>/images/gallery/<?php echo $r->picture;?>" style=" width:35%; height:100px;"/></td>
<td>
</td>
</tr>
</tbody>
<?php } ?>
</table>
I succeed in deleting the data in the database, but the image in the folder are not also be deleted.
Add some extra code in your controller:
public function delete_image($id)
{
$image_path = base_url().'uploads/images/gallery/'; // your image path
// get db record from image to be deleted
$query_get_image = $this->db->get_where('np_gallery', array('id' => $id));
foreach ($query_get_image->result() as $record)
{
// delete file, if exists...
$filename = $image_path . $record->picture;
if (file_exists($filename))
{
unlink($filename);
}
// ...and continue with your code
$this->np_gallery_model->delete($id);
$query = $this->db->get("np_gallery");
$data['records'] = $query->result();
$this->load->view('admin/gallery/gallery_listing',$data);
}
}
Note: alternativelly, you can do it inside your model delete() method instead. Consider where it better fits your applicaction needs.
Try these
foreach ($query_get_image->result() as $record)
{
// delete file, if exists...
$filename = $image_path . $record->picture;
if (file_exists($filename))
{
unlink($filename);
}
// ...and continue with your code
$this->np_gallery_model->delete($id);
$query = $this->db->get("np_gallery");
$data['records'] = $query->result();
$this->load->view('admin/gallery/gallery_listing',$data);
}
Code:
<?php
require 'Controllers/MenuController.php';
$menuController = new MenuController();
if(isset($_POST['types']))
{
$menuTables = $menuController->CreateTypeTables($_POST['types']);
}
else
{
$menuTables = $menuController->CreateTypeTables("%");
}
$title = 'Menus';
$content = $menuController->CreateMenuDropdownList(). $menuTables;
include 'Template.php';
?>
Basically when i do it this way, my page doesnt anything, not even a echo
It doesn't even show my background pic from css
But when i exclude the function:
<?php
require 'Controllers/MenuController.php';
$menuController = new MenuController();
// if(isset($_POST['types']))
//{
// $menuTables = $menuController->CreateTypeTables($_POST['types']);
// }
// else
// {
// $menuTables = $menuController->CreateTypeTables("%");
// }
$title = 'Menus';
$content = "test";
//$menuController->CreateMenuDropdownList() . $menuTables;
include 'Template.php';
?>
it shows my background pic and the content; 'test'.
I think there is something wrong with my functions but i triple checked them
Sooooo help!
Here is the MenuController.php file;
<?php
require ("Model/MenuModel.php");
class MenuController {
function CreateMenuDropdownList(){
$menuModel = new MenuModel();
$result = "<form action = '' method = 'post' width = 200px'>
Type:
<select name = 'types' >
<option value = '%' >All</option>
".$this->CreateOptionValues($menuModel->GetMenuTypes()).
"</select>
<input type = 'submit' value = 'Search' />
</form>";
return $result;
}
function CreateOptionValues(array $types){
$result = "";
foreach ($types as $type){
$result = $result . "<option value='$type'>$type</option>";
}
return $result;
}
function CreateTypeTables($types){
$menuModel = new MenuModel();
$menuArray = $menuModel->GetItemsByType($types);
$result = "";
foreach ($menuArray as $key => $product){
$result = $result .
"
<table class = 'coffeeTable'>
<tr>
<th></th>
<th>Name: </th>
<td>$product->desc</td>
<tr>
<tr>
<th></th>
<th>Type: </th>
<td>$product->type</td>
<tr>
<tr>
<th></th>
<th>Price: </th>
<td>$product->price</td>
<tr>
<form method='post'>
<button type='submit' name='insert' value='$product->id' class='button1'>Add</button>
</form>
</table>";
}
return $result;
}
}
Here is the code, and this code is producing the following output: screenshot of output
the idea is to have a different header everytime the number of beds changes.
$tableTop = '<table> ... <tbody>';
$tableBottom = '</tbody> </table>';
$last_bed = null;
foreach ($unitsForSaleData as $row) {
if($units_for_sale['beds'] == 0){
if ($last_bed == null) {
echo '<h1> Studios For Sale </h1>';
echo $tableTop;
$last_bed = $units_for_sale['beds'];
}
} else {
if ($units_for_sale['beds'] !== $last_bed) {
if ($last_bed !== null) { // End previous table
echo $tableBottom;
}
echo '<h1>'.$units_for_sale['beds'].' Bedroom Condos For Sale </h1>';
echo $tableTop; // Start new table
$last_bed = $units_for_sale['beds'];
}
}
?>
<tr> ... </tr>
<?php
} // end loop
Solved it with an isset instead of an == null. Here is the full code for anyone else who needs something like this done:
$tableTop = '<table> ... <tbody>';
$tableBottom = '</tbody> </table>';
$last_bed = null;
foreach ($unitsForSaleData as $row) {
if($units_for_sale['beds'] == 0){
if (!isset($last_bed)) {
echo '<h1> Studios For Sale </h1>';
echo $tableTop;
$last_bed = $units_for_sale['beds'];
}
} else {
if ($units_for_sale['beds'] !== $last_bed) {
if ($last_bed !== null) { // End previous table
echo $tableBottom;
}
echo '<h1>'.$units_for_sale['beds'].' Bedroom Condos For Sale </h1>';
echo $tableTop; // Start new table
$last_bed = $units_for_sale['beds'];
}
}
?>
<tr> ... </tr>
<?php
} // end loop
So, i've been using codeigniter for a while and I've never had an issue running a query in my model and returning it to my controller which then passes it into my view where I access it as an object, like so:
MODEL (somemodel)
function getdata()
{
$query = $this->db->get('sometable');
return $query;
}
CONTROLLER (somecontroller)
function controldata()
{
$this->load->model('somemodel');
$data['dbdata'] = $this->somemodel->getdata();
$this->load->view('someview',$data);
}
VIEW (someview)
<?php
foreach($dbdata->result() as $row) {
echo $row->id;
echo $row->name;
echo $row->whatever;
echo "<br />";
}
?>
But now for whatever reason in 2.0 it's making use $dbdata->result_array() as $row instead.
Basically it's returning the results as an array instead.
It doesn't make any sense, has something changed in the new version?
Thanks,
So here's my code:
MODEL (admin_model)
function show_allproducts($sort,$order,$limit,$offset)
{
$this->db->select('products.id,productcategories.categoryname,products.name,products.internetspecial,products.added,products.modified');
$this->db->from('products');
$this->db->join('productcategories','products.productcategories_id = productcategories.id');
$this->db->order_by($sort,$order);
$this->db->limit($limit,$offset);
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query;
}
else
{
return FALSE;
}
}
CONTROLLER (admin)
function show_allproducts()
{
$data['title'] = 'All Products';
$this->load->library('pagination');
$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/admin/show_allproducts';
$config['total_rows'] = $this->db->get('products')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 5;
$config['full_tag_open'] = '<div class="paggination right">';
$config['full_tag_close'] = '</div>';
$config['prev_link'] = '< prev';
$config['next_link'] = 'next >';
$config['cur_tag_open'] = '<a class="active">';
$config['cur_tag_close'] = '</a>';
$this->pagination->initialize($config);
$this->load->model('admin_model');
$data['tabledata'] = $this->admin_model->show_allproducts('name','ASC',$config['per_page'],$this->uri->segment(3));
$data['nav'] = 'admin/pagesections/nav';
$data['maincontent'] = 'admin/pages/show_allproducts';
$this->load->view('admin/template',$data);
}
VIEW (show_all_products)
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<?php if($tabledata) { ?>
<tr>
<th>ID</th>
<th>CATEGORY</th>
<th>NAME</th>
<th>ADDED</th>
</tr>
<?php foreach($tabledata->result() as $row) { ?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->categoryname; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->added;) ?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td>NO DATA</td>
</tr>
<?php } ?>
</table>
Unless you specify $dbdata->result_array() as $row , It wont return the results as array. The same code which you've posted must be working in CI 2.0.
If not, may be the problem with $this->db->get('sometable').
Try with $this->db->query('select * from sometable'). But as far as i know, both are same...
Ok folks, no lie, I found the freaking problem.
Looking at the fourth td where $row->added; is at, there's a close parantheses at the end of it that was causing 4 days of mayhem.
Kindest regards to all those who tried to help. Sometimes it doesn't matter how many eyes are on it I guess.