Load Record by ID for View CakePHP 3 - cakephp-3.0

I have what is probably a very basic question. I have a column in my computer record with the id of the customer who owns the computer. I am trying to show their name in a table next to the computers' and found a temporary solution. I am using this in my view (which probably isn't proper MVC) and also makes the page take forever to load (I am showing 100 computers per page).
<td>
<?php
foreach ($customers as $customer):
if ($computer->cust_id == $customer->id) {
echo $this->Html->link(
$customer->first.' '.$customer->last,
['controller'=>'Customers', 'action'=>'view', $customer->id]
);
}
endforeach;
?>
</td>
I also tried to do $this->Customers->get($computer->cust_id) after loading the model in the Controller but it says I need a Customer Helper. What is the CakePHP method of taking care of this issue?
Thanks!

Related

Dynamically generating SQL data on new page with PHP

I'm having trouble with this website I am creating.
Basically I'm trying to create news articles on a page that once you click on the article you'd be taken to a page that would load the content for that article. Is it possible to set it up in a way that I can load the content on a new page depending on the SQL primary key of the article selected?
I can load the articles on the page from the sql database no problem its just determining what data is loaded on a new page that is beating me.
Thanks in advance for any advice anyone can offer in this, even if its just pointing me in the right direction.
I have tried searching online first with no luck so far.
You can use $_GET to create a dynamic content using one page only
EXAMPLE:
article.php?news_id=1:
<?php
if(isset($_GET['news_id'])){
$newsID = $_GET['news_id'];
//query all the data you need using that $newsID
}
?>
you can pass the news-id as variable in url and get that variable using $_GET method,then execute the sql query.
<?php
$id =$_GET['id];
?>
mysql-"Select * from Table where id =$id"

sql query for the fetching back the drop-down list selected option

I have drop down list in my website and the options are stored in database in a separate table and fetched back to drop down list. Now whenever I select any option from drop down list the value of option is stored in a main table. Now how do I fetch back the selected option from the database to website?
Could you provide some more detail? I assume that when the user loads the page you want the dropdown menu to display the same value it had the last time they logged in or something like that?
If such be the case, you need a few things. To start with your body tag needs an onload function that will get called when the page is loaded:
<body onload="dosomething()">
In your javascript dosomething() function you will make an ajax call to the back end which will probably be a php script.
This script will fetch the value from mysql and return it.
Your ajax code will have an onreadystatechange() function which will receive the response from the back end and do something like
document.getElementById('foo').value=whatevercamebackviaajax
Written off the top of my head, and somewhat vague, but I or somebody could probably do better with a bit more information.
You need to add javascript to your tags.
Now we can see the scenario where we get the option for one single user from the database so that it will be easy for understanding and you shall make your own code if you need to customize it.
PHP method:
Scenario: Keep Selected the login username is equal to the clientname in select tag.
<?php
$select="SELECT * FROM TABLE where id='".$_GET['uid']."'";
// here `uid` is the logged in user id
$q=mysqli_query($conn,$select) or die($select);
$row=mysqli_fetch_array($q);
?>
<select name="sclient" id="sclient" class="reginput"/>
<option value="">Select Client</option>
<?php
$sel="select * from user where type='client'";
$que=mysqli_query($conn,$sel) or die($sel);
while($res=mysqli_fetch_array($que))
{ ?>
<option value="<?php echo $res['login_name']; ?>"<?php if($row['clientname']==$res['login_name']) echo 'selected="selected"'; ?>><?php echo $res['login_name']; ?></option>
<?php } ?>
</select>
Parameters:
$conn - Connection Variable to DB. Db Connectivity using mysqli

CakePHP Invoicing App & Deleting Data From Another Controller/Model

I'm new to CakePHP and I could do with some newbie advice please.
I'm creating an invoicing application and the way I have it set up is with seperate tables (and so Models and Controlllers) for Invoices and Items. The Invoice table holds the data such as 'Invoice Number', 'Date' and 'Client ID' for example, and the Items table stores individual records/items that make up each invoice with 'Quantity', 'Unit Price' etc. The two are associated correctly.
When I call the 'edit' view for Invoices, obviously passing an ID, I retrieve the record and then also loop through the all associated records returned from the Items table to - all works as planned and are retrieved and displayed correctly.
What I want to be able to do from this view though, is to be able to delete individual items from the invoice by clicking on a delete button next to each - ideally with a confirmation box. How would this be acheived exactly? What would be the best method? Would I use a function in the Items controller to do this without leaving the page? How would I call the function in that controller from the Invoice controller/view? Or would another method such as the CakePHP query() function be used for this?
I hope I've explained this setup clearly enough for someone to offer any help and/or advice for which I would be very thankful.
Thanks in advance
The simple way:
In your Invoice/view.ctp
Inside your foreach loop you would do something like this:
//this is loose code since you have not pasted any in your question
<?php echo $this->Html->link('Delete', array('controller'=>'Item', 'action'=>'delete', $item['Item']['id'])); ?>
then create the function in your ItemController.php
function delete($id=NULL){
if($id){
$this->Item->delete($id);
$this->Session->setFlash(__('Invoice item deleted', true));
$this->redirect($this->referer());
}
}

Framework doesn't see a new column in production database table?

I created a site with CakePHP responsible for displaying my CV. I would like to add a new feature relying on that if i still work at some company it should display text: "until now", if dont it should display a proper date. The problem is that when I adding a new column to my database named isActive and I set up value 1 to one of the entries (other entries have value 0) page displays value 0 for all entries. This problem occurs on production server. On my development server everything is ok.
View:
<?php
if ($experience['Experience']['isActive'] == 0 ) {
echo $this->Time->format('Y.m', $experience['Experience']['dateEnd']);
/*tmp*/ echo $experience['Experience']['isActive'];
} else if($experience['Experience']['isActive'] == 1){
echo 'until now';
}
?>
What may be a problem?
CakePHP caches the table structures. Delete the caches all will be good in the world again.
If you make a change to your database schema when the debug level is 0, these model caches won’t be refreshed; if you get errors or unexpected behavior after making a database change, a likely fix is:
rm -f app/tmp/cache/models/*
As discussed here.

How do I display database query statistics on Wordpress site?

I've noticed that a few Wordpress blogs have query statistics present in their footer that simply state the number of queries and the total time required to process them for the particular page, reading something like:
23 queries. 0.448 seconds
I was wondering how this is accomplished. Is it through the use of a particular Wordpress plug-in or perhaps from using some particular php function in the page's code?
Try adding this to the bottom of the footer in your template:
<?php echo $wpdb->num_queries; ?> <?php _e('queries'); ?>. <?php timer_stop(1); ?> <?php _e('seconds'); ?>
To explain pix0r's code:
$wpdb->num_queries is the number of database queries.
_e is for localization: http://faq.wordpress.net/view.php?p=50
timer_stop() returns the amount of time taken to render the page: http://codex.wordpress.org/Function_Reference/timer_stop
wordpress has a easy solution now
There is a function called get_num_queries() and as specified in other answers timer_stop().
<?php echo get_num_queries(); _e(' queries'); ?> in <?php timer_stop(1); _e(' seconds'); ?></p>
get_num_queries() - returns the number of database queries during the WordPress execution and accepts no parameters