PHP - Page times out when trying to access an object - php

So i've been developing an Admin panel and I just stumbled on a problem I haven't seen before.
When I'm trying to access my 'PageContent' object the page seems to instantly time out. The object is filled correctly. The object array im trying to access only has 12 records.
Function that creates the objects:
public function getPages()
{
$pages = array();
$rs = $this->con->fetchResult($this->con->doQuery("SELECT * FROM PANEL_PAGES"));
foreach($rs as $row)
{
$pages[] = new ContentPage($row[0]->iPid, $row[0]->strTitle, $row[0]->dlastEdit, $row->strBy);
}
return $pages;
}
Index.php
ob_start();
session_start(); // Start a session
<?php
if(!Engine::adminIsLogged()) {
header("Location: " . ADMIN_BASE);
}
ob_end_flush();
?>
Content.page.php
<?
require ADMIN_TPL . "head.tpl";
require ADMIN_BIN . "controllers/page.php";
$pageMan = new PageManager();
$pages = $pageMan->getPages();
?>
I try to access the objects in this way. Been doing it like this for years, so I'm stuck really.
<?php
foreach($pages as $page)
{
?>
<tr>
<td><?php echo $page->getPid(); ?></td>
<td><?php echo $page->getTitle(); ?></td>
<td><?php echo $page->getLastEdited(); ?></td>
<td><?php echo $page->getEditedBy(); ?></td>
<td>
<img src="resources/images/icons/pencil.png" alt="Edit" />
</td>
</tr>
<?php
}
?>

Related

Passing Parameters from View to Controller when a hyperlink is clicked and return parameters from Controller to view

This is View file. I am trying to pass- "$row['ISBN'] . ':SearchByAuthor:' . $Author"- abc:SearchByAuthor:aka - value to function increment_author to controller file.
<?php
?>
<tr>
<td><?php
echo $row['ISBN'];
?></td>
<td><?php
echo $row['name'];
?></td>
<td><?php
echo $row['title'];
?></td>
<td><?php
echo $row['year'];
?></td>
<td><?php
echo $row['price'];
?></td>
<td><?php
echo $row['publisher'];
?></td>
<td><?php
echo $row['number'];
?></td>
<td> <a href="<?php
echo base_url();
?>/increment_author/<?php
echo $row['ISBN'] . ':SearchByAuthor:' . $Author;
?>">
Add to cart</a></td>
</tr>
<?php
?>
This is contoller file and I am doing some calculations and trying the same to return to above search page, which is view. However, it's not working. I am trying to convert native php code to codeIgniter framework. Please help me out in passing parameters from Controller to View and vice versa. Thanks in Advance
public function increment_author($SearchByAuthor)
{
session_start();
$db = new mysqli('localhost','root','','cheapbook') or die('Error connecting to MySQL server.');
mysqli_set_charset($db, 'utf8');
if(isset($SearchByAuthor))
{
// echo $_GET['add'];
$pieces=explode(":", $SearchByAuthor);
$quantity="SELECT D.ISBN, D.title, sum(D.number) number FROM
(SELECT A.ISBN, title, number number FROM
book A, Stocks B WHERE A.ISBN='$pieces[0]' and
A.ISBN=B.ISBN)D
GROUP BY D.ISBN, D.title";
$result = mysqli_query($db,$quantity);
while($quantity_row = mysqli_fetch_array($result))
{
if($_SESSION['cart_'.$pieces[0]])
{
$chan=0;
}
else
{
$_SESSION['cart_count']+=1;
}
if($quantity_row['number']!=$_SESSION['cart_'.$pieces[0]] )
{
$_SESSION['cart_'.$pieces[0]]+=1;
$_SESSION["cartTotal"]+=1;
}
}
$data['SearchByAuthor']=$pieces[0];
}
//header("location:search_vi?SearchByAuthor=".$pieces[0]);
//$data['SearchByAuthor']=$pieces[0];
$this->load->view('search',$data);
}
You don't have to use session_start actually you should use Session Library.
Also there is a whole Database layer in CI.
Try to debug your problem in more details. Check if your $data variable contains proper result, and in your view this will be visible via $SearchByAuthor variable.

Displaying the nested foreach with if else in php codeigniter

Controller
public function admin()
{
// get the current date employee logged in details
$data['get_attendance'] = $this->attendance_model->get_attendance();
// get the active employee details
$data['get_employee'] = $this->attendance_model->get_employee();
//print_r($data['get_employee']); exit();
// attendance page
$data['header'] = "Employee";
$data['sub_header'] = "Attendance employee";
$data['main_content'] = 'attendance/admin_list';
$this->load->view('employeelayout/main',$data);
}
Model
public function get_attendance()
{
return $this->db->where('date',date('Y-m-d'))->get('attendance')->result_array();
}
public function get_employee()
{
return $this->db->where('is_active',1)->get('employee')->result_array();
}
Views
<?php $count = 1 ?>
<?php foreach ($get_employee as $employee) { ?>
<tr class="gradeX">
<td><?php echo $count++ ?></td>
<td><?php echo $employee['first_name'] . ' ' . $employee['last_name'] ?></td>
<td><?php echo $employee['employee_number'] ?></td>
<?php foreach ($get_attendance as $attendance) : ?>
<!-- if employee exists -->
<?php if($employee['employee_id'] == $attendance['employee_id'] ) { ?>
<td><?php echo date('M-Dj-Y',strtotime($attendance['date'])) ?></td>
<td><?php echo $attendance['mark_in_time'] ?></td>
<td>mark out going</td>
<td>Active</td>
<?php break; ?>
<?php } elseif($employee['employee_id'] != $attendance['employee_id'] ) { ?>
<td><?php echo "i'm absent" ?></td>
<?php break; ?>
<?php } ?>
<?php endforeach; ?>
</tr>
<?php } ?>
I want to display the currently logged in (present) employee and display the absent employees too. I'm getting the employee details from employee table.
and attendance details from attendance table. if an employee is absent and I want to display the absent else display the login details. where here foreach loop not displaying properly with if condition. what's wrong with the loop.
try this:
<?php foreach ($get_employee as $employee) : ?>
<tr class="gradeX">
<td><?php echo $count++ ?></td>
<td><?php echo $employee['first_name'] . ' ' . $employee['last_name'] ?></td>
<td><?php echo $employee['employee_number'] ?></td>
<?php foreach ($get_attendance as $attendance) : ?>
<!-- if employee exists -->
<?php if($employee['employee_id'] == $attendance['employee_id']) : ?>
<td><?php echo date('M-Dj-Y',strtotime($attendance['date'])) ?></td>
<td><?php echo $attendance['mark_in_time'] ?></td>
<td>mark out going</td>
<td>Active</td>
<?php elseif ($employee['employee_id'] != $attendance['employee_id']) : ?>
<td><?php echo "i'm absent" ?></td>
<?php else : ?>
<?php endif; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
Apply left join in place of two different queries.
Because currently you have applied two loops and their comparisons, will consume a lot of execution time And I don't think this is the good code If you get thousands of data than definitely you can analyse the execution time.
So it's better to apply join query.

Get error "You do not have sufficient permissions to access this page" While creating custom plugin

I have created custom plugin in wordpress for get data from the database. And I have added two link to perfrom action on records .One is Edit and another is Delete. I have written code for delete link like this
Delete
When I click on delete link it will show error like
You do not have sufficient permissions to access this page
My plugin code is
function submit_review()
{
add_options_page("Submit Review","Submit Reviews",1,"submit_review","submit_review");
global $wpdb;
$id = get_current_user_id();
$q = "select * from wp_submit_review where user_id = " . $id;
$result = $wpdb->get_results($q,OBJECT);
if(!empty($result))
{
?>
<div class="wrap">
<h1>Submitted Reviews</h1>
<div style="width: 100%;">
<table cellpadding="5" class="wp-list-table widefat fixed pages">
<thead>
<tr>
<th><b>Review Id</b></th>
<th><b>User Name</b></th>
<th><b>Submitted Category</b></th>
<th><b>New Listing Name</b></th>
<th><b>New Listing Address</b></th>
<th><b>New Listing City</b></th>
<th><b>New Listing Description</b></th>
<th><b>Image</b></th>
<th><b>R-option</b></th>
<th><b>New Listing Rating</b></th>
<th><b>New Listing Review</b></th>
<th><b>Actions</b></th>
</tr>
</thead>
<tbody>
<?php
foreach($result as $res)
{
?>
<td><?php echo $res->review_id; ?></td>
<td><?php echo get_the_author_meta('user_login',$res->user_id); ?></td>
<td><?php
if($res->submit_category == 1)
{
echo "Service";
}
else if($res->submit_category == 2)
{
echo "Restaurant";
}
else
{
echo "Product";
}
?>
</td>
<td><?php echo $res->newlistingname; ?></td>
<td><?php echo $res->newlistingaddress; ?></td>
<td><?php echo $res->newlistingcity; ?></td>
<td><?php echo $res->newlistingdesc; ?></td>
<td><img src="<?php echo home_url()."/uploads/".$res->image; ?>" height="50px" width="50px"/></td>
<td><?php echo $res->roption; ?></td>
<td><?php echo $res->newlistingrating; ?></td>
<td><?php echo $res->newlistingreview; ?></td>
<td>Edit | Delete</td>
<?php
}
?>
</tbody>
</table>
</div>
</div>
<?php
}
else
{
echo "No review find";
}
}
function delete_my_review()
{
echo $_GET['id'];
}
add_action("admin_menu","delete_my_review");
function submit_review_menu()
{
add_menu_page("Submit Review","Submit Review","manage_options","submit_review", submit_review);
}
//add_action("admin_menu","submit_review");
add_action("admin_menu","submit_review_menu");
So how to call delete_my_review() function to perform delete action?
simply create a function with ajax and php. following example worked for me.
html delete button
<td><button onclick="delete_ab(<?php echo $results->id; ?>)">Delete</button></td> // pass your id to delete
javascript function
function delete_ab(str)
{
jQuery.post(
// see tip #1 for how we declare global javascript variables
MyAjax.ajaxurl,
{
// here we declare the parameters to send along with the request
// this means the following action hooks will be fired:
// wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
action : 'myajax_submit',
// other parameters can be added along with "action"
postID : str
},
function(response)
{
document.getElementById(str).style.display='none';
}
);
}
main function in php
function myajax_submit() {
// get the submitted parameters
$postID = $_POST['postID'];
global $wpdb;
$qry=$wpdb->query("DELETE FROM `price_table` WHERE id=$postID");
// generate the response
echo "hello";
// IMPORTANT: don't forget to "exit"
exit;
}

Wordpress Plugin Function Error?

I recently inherited and transferred a wordpress website that contains various custom plugins. Everything seems to be working correctly except one link in the admin section assocaited with a plugin. The page comes up blank when the link is clicked when it should list all bookings. There are no syntax errors.
I know this is a long shot, but I was wondering if anybody can notice any problems with the below function that handles the page? Any insight would be greatly appreciated.
/**
* List all bookings
*/
function admin_list_bookings($class_id = false, $wrapper=true) {
global $ae_EventManager;
// View single?
if (isset($_GET['view_booking']) && is_numeric($_GET['view_booking'])) { $this->admin_view_booking($_GET['view_booking']); return; }
// Delete posts?
if (isset($_POST['DeleteLogs']) && !empty($_POST['sel_bookings']) && is_array($_POST['sel_bookings'])) {
foreach ($_POST['sel_bookings'] as $delete_id) {
wp_delete_post($delete_id, true);
}
echo '<div class="updated"><p>Selected logs have been deleted.</p></div>'."\n";
}
// Load all bookings
$booking_log_type = 'private';
if (isset($_GET['logtype'])) $booking_log_type = 'any';
$bookings = get_posts(array(
'post_type' => $this->booking_type,
'numberposts' => -1,
'post_status' => $booking_log_type,
'post_parent' => (is_numeric($class_id) ? $class_id : false),
));
// Output
if ($wrapper) {
echo '<div class="wrap">'."\n";
echo '<h2>View Class Bookings</h2>'."\n";
echo '<form class="plugin_settings" method="post" action="'.esc_url($_SERVER['REQUEST_URI']).'">'."\n";
echo wp_nonce_field($this->nonce);
}
?>
<table class="widefat">
<thead><tr><?php if ($wrapper) { ?><th><input type="checkbox" id="check_all" /></th><?php } ?>
<th>Date</th><th>Class</th><th>Name</th><th># Seats</th><th>Booking Cost</th><th>Status</th></tr></thead>
<tbody>
<?php
foreach ($bookings as $booking) {
$booking->post_content = $this->__unserialize($booking->post_content);
$log = $booking->post_content;
// echo '<pre>'; print_r($booking); echo '</pre>';
$view_link = 'edit.php?post_type='.$ae_EventManager->course_type.'&page&view_booking='.$booking->ID;
?>
<tr>
<?php if ($wrapper) { ?>
<td class="ctrl"><input type="checkbox" name="sel_bookings[]" value="<?php echo $booking->ID; ?>" /></td>
<?php } ?>
<td><?php echo $this->format_time_ago($booking->post_date); ?></td>
<td><?php echo $log['course_name'].'<br />'.date('d-m-Y H:ia', strtotime($log['class_date'].' '.$log['class_time'])); ?></td>
<td><?php echo $log['tickets'][0]['firstname'].' '.$log['tickets'][0]['lastname']; ?></td>
<td><?php echo $log['number_seats']; ?></td>
<td><?php echo '$'.number_format($log['transaction_total'], 2); ?></td>
<td><?php echo $log['payment_method'].'<br />'.$log['eway_response']['ResponseMessage']; ?></td>
</tr>
<?php
}
?>
Just add a closing curly bracket } at the end of admin_list_bookings function

parsing XML web service

i have this code controler CI:
function cek_xml() {
$response = $_SESSION ['nim'];
// fetch data
$respons = $this->curl->simple_get($url = "http://localhost/restful/index.php/restful/buku/nim/$response/format/xml");
if (empty($response)) {
show_error('can`t access :' . $response);
}
$data['cuaca'] = $this->format->factory($respons, 'xml')->to_array();
$this->load->view('data_buku_XML', $data);
}
in view :
<?php $no=1;?>
<?php //$this->benchmark->mark('rest_start'); ?>
<?php foreach ($cuaca as $row) { ?>
<?php foreach ($row as $row) { ?>
<tr class="<?php echo ($no % 2 == 0) ?>">
<td><?php echo $no; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['loan_date'] ?></td>
<td><?php echo $row['due_date'] ?></td>
<?php $no=$no+1;?>
</tr>
<?php } ?>
<?php } ?>
The problem is when i get the title, loan date, due date more than one variable it's okay.
But if get 1 title it's will show:
Message: Illegal string offset 'title'
But if i put // like in one foreach:
// foreach ($row as $row) {
It will show 1 variable title but error in more one variable ...
You are reusing same name for variables
Change the variable name
<?php foreach ($row as $r)
and use as
<td><?php echo $r['title']; ?></td>

Categories