Thursday, 3 October 2013

Cakephp get data from another model

Cakephp get data from another model

I'm developing an e-commerce website. I have 2 tables - Customers & Shipping
Field in Customers - id, name, address, city, postal & country Field in
Shipping - id, address, city, postal & country
The process flow, after the customer click checkout button, they need to
fill up shipping form (shipping - add.ctp).
But in add.ctp, i also want to show address from table customer. So, I can
put check button - Use this address as shipping address.
Anyone can help how the controller & add.ctp looks like? Thank You.

Wednesday, 2 October 2013

Windows Phone 8 push notification push channel always creates new channel uri

Windows Phone 8 push notification push channel always creates new channel uri

I wanted to check that my push notification implementation is correct.
Each time I open my app (in actual fact I register the push channel only
on a specific page so it's each time I go back and forth from that page) a
new push channel uri is created which I store in my mobile services
database to send push notifications to. This doesn't seem correct to me as
each time the app/page is open a new push channel uri is generated and so
the list of channel uri's just grows and grows for each device that uses
my app. I'd assume that you create a push channel, store the channel uri
and push to it as needed. I will make note here that I am using raw push
notifications.
I understand that push channels will expire every so often but for me it's
occuring each time I back out of the app/page and therefore when
onNavigateTo is called I find the push channel which does exist and a new
channel uri is always created. Is this correct?
My code is as follows:
protected override void OnNavigatedTo(NavigationEventArgs e) {
registerPushChannel(); }
private void registerPushChannel()
{
// The name of our push channel.
string channelName = "RawSampleChannel";
// Try to find the push channel.
pushChannel = HttpNotificationChannel.Find(channelName);
// If the channel was not found, then create a new connection to
the push service.
if (pushChannel == null)
{
pushChannel = new HttpNotificationChannel(channelName);
// Register for all the events before attempting to open the
channel.
pushChannel.ChannelUriUpdated += new
EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new
EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
pushChannel.HttpNotificationReceived += new
EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);
pushChannel.Open();
}
else
{
// The channel was already open, so just register for all the
events.
pushChannel.ChannelUriUpdated += new
EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new
EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
pushChannel.HttpNotificationReceived += new
EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);
// code which passes the new channel URI back to my web service
}
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
pushChannel.Close();
}
So to clarify, the app is opened and the push channel is registered and
the channel uri is saved in my web service. The web service then sends
notifications to the channel uri. When I exit the app or page and return
to it, the push channel is found but there a new channel uri is create
which is saved to my web service. My channels table in effect just keeps
growing and growing.
So is this the way it should work with new channel uri's continually
generated? It kind of doesn't make sense to me. I'm not sure how toast and
tile notifications work but I'd assume the channel uri needs to be static
when the app closes to keep receiving notifications while the app is
closed, but perhaps that could be a functionality of bindtotoast and
bindtotile and what I'm doing is correct because it's to do with raw
notifications.

Moving robot in Java on specified length

Moving robot in Java on specified length

A robot starts at location (0,0) facing east, carrying 7 beepers: Need to
write a method called that accepts an integer parameter and a boolean
parameter. The method should move the robot along the row for the number
of squares specified by the integer parameter, putting down one beeper in
each square (excluding the robot's starting square). Can be assumed that
the integer parameter will be a value that allows the robot to do this
safely. The robot should finish in the square where it put down the last
beeper. If the boolean parameter is true then the program should print out
how many beepers were spread over the row. The message should be one of
the following:
If 0 beepers were spread over the row, the message should be Spread no
beepers
If 1 beeper was spread over the row,the message should be Spread 1 beeper
Otherwise the message should be Spread n beepers,where n is the number of
beepers put down.
My code:
void spreadBeepersOverRow(int x, boolean y) {
while(x>0) {
moveRobotForwards();
dropItemFromRobot();
--x;
}
if(y==true&&x==6||x<6&&x>0)
println("Spread "+ x +" beepers");
else
if(y == true&&x==7)
println("Spread no beepers");
}
}
I am confusing with the conditions of the task... My version gives some
mistakes when compiling

Target elements with different ids Jquery

Target elements with different ids Jquery

How do I target a particular element used with different ids.For instance
that have the ids row and col. I have tried doing this with jquery.
var $drag = $( "#col" );
var $drag2 = $( "#row" );
$("td", $drag, $drag2).droppable({ accept: ".special" });
But the second id "row" stored in drag2 is not selected. What is the
proper way of doing this in jquery.

Signaling in OpenMP

Signaling in OpenMP

I am writing computational code that more-less has the following schematic:
#pragma omp parallel
{
#pragma omp for nowait
// Compute elements of some array A[i] in parallel
#pragma omp single
for (i = 0; i < N; ++i) {
// Do some operation with A[i].
// This time it is important that operations are sequential. e.g.:
result = compute_new_result(result, A[i]);
}
}
Both computing A[i] and compute_new_result are rather expensive. So my
idea is to compute the array elements in parallel and if any of the
threads gets free, it starts doing sequential operations. There is a good
chance that the starting array elements are already computed and the
others will be provided by the other threads doing still the first loop.
However, to make the concept work I have to achieve two things:
To make OpenMP split the loops in alternative way, i.e. for two threads:
thread 1 computing A[0], A[2], A[4] and thread 2: A[1], A[3], A[5], etc.
To provide some signaling system. I am thinking about an array of flags
indicating that A[i] has already been computed. Then compute_new_result
should wait for the flag for respective A[i] to be released before
proceeding.
I would be glad for any hints how to achieve both goals. I need the
solution to be portable across Linux, Windows and Mac. I am writing the
whole code in C++11.

Tuesday, 1 October 2013

Windows API `GetPixel()' always return `CLR_INVALID`, but `SetPixel()` is worked well?

Windows API `GetPixel()' always return `CLR_INVALID`, but `SetPixel()` is
worked well?

My OS is windows 7 64-bits with 2 monitors display.
I use GetPixel(), but it always return CLR_INVALID as result like that:
COLORREF result = GetPixel(dc,x,y);
My GetDeviceCaps(RASTERCAPS) returns result that RC_BITBLT is enabled.
GetDeviceCaps(COLORMGMTCAPS) returns result is CM_GAMMA_RAMP.
Most importantly, if I SetPixel(dc,x,y,RGB(250,250,250)) in advance, and
GetPixel(dc,x,y) later, I can retreive correct result at all. So I think
my coordination should be alright.
Well. I have no idea about why GetPixel() always return CLR_INVALID, but
SetPixel() is always worked well? Any question?

minimum value not in an array of intervals

minimum value not in an array of intervals

You have a list of N integer intervals, and the possible integer values
range from 1 to N^3.
For example, you can have N=5 and intervals
([1,5],[2,9],[18,25],[10,15],[20,22]). I'm trying to figure out an
algorithm to return the smallest integer not in those intervals, i.e. 16,
with a runtime of O(N).
Please help!