I am working on a form that needs to be able to update two different tables.
I am using the Order controller and need to update the order table. The other table I need to update is CampaignCustomers, where I would like to update the campaignID.
The two Models are associated, and here is the result when I debug $order:
array(
'Order' => array(
'OrderID' => (int) 1574996,
'OrderType' => '3',
'UPSTrackingNumber' => null,
'CreatedDate' => 'Mar 30 2019 12:42:00:000PM',
'ShippedOnDate' => null,
'Notes' => null,
'OrderedBy' => 'TIM',
'UserID' => (int) 431,
'CampaignCustomerID' => (float) 78156512,
'TaxPercentage' => (int) 0,
'DiscountID' => null,
'DiscountPercentage' => (int) 8,
'TotalPrice' => (float) 7.75,
'OrderStatusID' => (int) 13,
'LabelCategory' => null,
'LabelPrinted' => (int) 0,
'InvoicePrinted' => (int) 0,
'ShippingMethodID' => (int) 9,
'SaturdayDelivery' => (int) 0,
'ShippingAddress' => 'ert',
'ShippingPrice' => (float) 7.75,
'ConfirmationEmail' => null,
'PaymentMethod' => 'Bill In Full',
'PurchaseOrderNumber' => ' dfqsdfsdafe3r23rwererewrw',
'CreditCardHolderName' => null,
'CreditCardNumber' => null,
'CreditCardExpirationDate' => null,
'CreditCardStreet' => null,
'CreditCardZipCode' => null,
'CreditCardCVV' => null,
'TransactionID' => null
),
'CampaignCustomer' => array(
'CampaignCustomerID' => (float) 78156512,
'CampaignID' => (int) 422,
'CustomerID' => (int) 3633
),
This is my view:
<?php
debug($order);
echo $this->Html->div("box");
echo $this->Html->tag("h3","Edit Order ".$order['Order']['OrderID']);
echo "<p><b>Company: </b>" . $order["Company"]["CompanyName"] . "</p>";
echo "<p><b>Order ID: </b>" . $order["Order"]["OrderID"] . "</p>";
echo $this->Form->create(("Order"));
echo $this->Form->input("PurchaseOrderNumber");
echo $this->Form->input("CampaignID",array("options"=>$campaigns,"label"=>"Campaign","selected"=>$order["Campaign"]["CampaignID"]));
echo $this->Form->input("CreatedDate");
echo $this->Form->input("OrderType",array("options"=>$orderType,"selected"=>$order["Order"]["OrderType"]));
echo $this->Form->input("OrderedBy");
echo $this->Form->input("ShippingMethodID",array("options"=>$shipping_method_ids,"selected"=>$order["Order"]["ShippingMethodID"],"label"=>"Shipping Method"));
echo $this->Form->input("SaturdayDelivery");
echo $this->Form->input("UserID",array("options"=>$users,"selected"=>$order["Order"]["UserID"],"label"=>"Sales Agent"));
echo $this->Form->input("DiscountPercentage");
echo $this->Form->input("ShippingAddress");
echo $this->Form->end("Update Order");
echo "</div>";
?>
The only thing that won't update is the CampaignID field, because it is a different table.
Does anyone know how to solve this problem?
Thanks!
You've not passed the CampaignCustomer data in your form, so when the post request is made, there's nothing for it to update in terms of CampaignCustomer
You need to include this information in your form in either hidden fields or visible ones to ensure it gets updated.
echo $this->Form->create();
echo $this->Form->input("Order.PurchaseOrderNumber");
echo $this->Form->input("Order.CampaignID",array("options"=>$campaigns,"label"=>"Campaign","selected"=>$order["Campaign"]["CampaignID"]));
echo $this->Form->input("Order.CreatedDate");
echo $this->Form->input("Order.rderType",array("options"=>$orderType,"selected"=>$order["Order"]["OrderType"]));
echo $this->Form->input("Order.OrderedBy");
echo $this->Form->input("Order.ShippingMethodID",array("options"=>$shipping_method_ids,"selected"=>$order["Order"]["ShippingMethodID"],"label"=>"Shipping Method"));
echo $this->Form->input("Order.SaturdayDelivery");
echo $this->Form->input("Order.UserID",array("options"=>$users,"selected"=>$order["Order"]["UserID"],"label"=>"Sales Agent"));
echo $this->Form->input("Order.DiscountPercentage");
echo $this->Form->input("Order.ShippingAddress");
echo $this->Form->input("CampaignCustomer.CampaignCustomerID");
echo $this->Form->input("CampaignCustomer.CampaignID");
echo $this->Form->input("CampaignCustomer.CustomerID");
echo $this->Form->end("Update Order");
Related
Here is my code.php:
$info = array (
0 =>
array (
'num' => 1,
'name' => '15 TV',
'stream_type' => 'live',
'stream_id' => 219,
'stream_icon' => 'https://is1-ssl.mzstatic.com/image/thumb/Purple71/v4/5d/2f/1c/5d2f1cb1-3a21-71ff-c10c-e27253ba13dc/source/512x512bb.jpg',
'epg_channel_id' => NULL,
'added' => '1535765481',
'category_id' => '3',
'custom_sid' => '',
'tv_archive' => 0,
'direct_source' => '',
'tv_archive_duration' => 0,
),
479 =>
array (
'num' => 125,
'name' => 'LA DOS',
'stream_type' => 'live',
'stream_id' => 323,
'stream_icon' => 'http://www.dazplayer.com/img_chh/la_dos_espa%C3%B1a.png',
'epg_channel_id' => NULL,
'added' => '1535838540',
'category_id' => '13',
'custom_sid' => '',
'tv_archive' => 0,
'direct_source' => '',
'tv_archive_duration' => 0,
),
);
I have these array and I want to print it, the arrays go to the number 0 from 479. I tried to use the code that it is below but it is not working as well as I want, because it is not printing the right variables. I know that I have to use a loop and the command line for each but I could not be able to do it work.
The variables that I am interested in are:
name
stream_id
stream_icon
category_id
<?php
foreach($info as $x => $x_value) {
echo "Name=" . $x . ", Value=" . $x_value;
echo "<br>;";
}
?>
If someone could help me fixing the bug, i will be thankful.
Regards, Dix
You can try like this way with foreach() loop as $key=>$value pattern. To get values you've to use $value['field_name_for_that_you_need_value']. For example: $x_value['name'] to get the value of name field
<?php
foreach($info as $x => $x_value) {
echo "Name=" . $x_value['name'] . ", Stream Id=" . $x_value['stream_id'].", Stream Icon=".$x_value['stream_icon']. "Category Id=". $x_value['category_id'];
echo "<br>;";
}
?>
DEMO: https://3v4l.org/cvTsc
You could use this method:
print_r($info);
Which will be in your context:
foreach($info as $key => $value)
{
echo "Name=" . $key . ", Value=" . print_r($value, true);
echo "<br>";
}
Documentation
The goal is to print out data from my database, through my view.ctp file, and be able to view it in my local host.
Here's the data I am working with:
array(
'DataWriteError' => array(
'modified' => object(MongoDate) {
sec => (int) 1507762384
usec => (int) 402000
},
'created' => object(MongoDate) {
sec => (int) 1507762384
usec => (int) 402000
},
'errors' => array(
(int) 0 => array(
'index' => (int) 13370,
'code' => (int) 11000,
'message' => 'E11000 duplicate key error index: live_system.599c4ec3-0e24-408d-b8a6-067445404121.$Refcode_1 dup key: { : "VA1394315696" }'
),
(int) 1 => array(
'index' => (int) 14987,
'code' => (int) 11000,
'message' => 'E11000 duplicate key error index: live_system.599c4ec3-0e24-408d-b8a6-067445404121.$Refcode_1 dup key: { : "VA1394315697" }'
)
),
'campaign_id' => '59dea09d103fb4cb428fdef2',
'id' => '59dea0d0bed1ec43d21cf65b'
)
)
I have everything else displayed other than ERRORS. Errors has two arrays within it and I cannot for the life of me figure out where my code is wrong.
Here is my code to echo to view:
<td><?php foreach ($this->request->data['DataWriteError']['errors'] as $i => $item);
foreach ($item as $e => $etem) {
echo $this->request->data['DataWriteError']['errors']['index']['errors']['message'][$i];
echo $this->request->data['DataWriteError']['errors']['index']['code']['message'][$e];
} ?></td>
Not sure where I am going wrong. If there are any ideas, please let me know. Thanks!
Have you tried blow:
foreach ($this->request->data['DataWriteError']['errors'] as $i => $item) {
echo $item['message'];
}
I use CakePHP framework. I have an array which corresponds a model's records. I have to handle it in the way, that all the integers and floats are output as integers and floats just like in java without quotes(111 or 11.1 instead of this '111.1' or '11.1'). I found the way to return all the values in this way: return json_encode($data, JSON_NUMERIC_CHECK);. The question is: is there any way to exclude some numeric fields to be outpt in this way? In other words: I have two numeric fields: field1 and field2, and they have to be with quotes. Meantime all other numeric fields must be without quotes. How could I implement this ?
My array looks so:
array(
(int) 0 => array(
'password' => '*****',
'id' => '2',
'number' => '2',
'debtor_number' => null,
'name' => 'Ziegler',
'firstname' => 'Lisa',
'address' => 'Frau',
'title' => '',
'name_extension' => '',
'company' => '',
'company_function' => '',
'street' => 'Feldbergstr. 13-15',
'street2' => null,
'postbox' => '',
'street_or_postbox' => '1',
'zip' => '60318',
'city' => 'Frankfurt am Main',
),
(int) 1 => array(
'password' => '*****',
'id' => '3',
'number' => '3',
'debtor_number' => null,
'name' => 'Trappatoni',
'firstname' => 'Günther',
'address' => 'Herr',
'title' => '',
'name_extension' => '',
'company' => '',
'company_function' => '',
'street' => 'Trilluper Weg 17',
'street2' => null,
'postbox' => '',
'street_or_postbox' => '1',
'zip' => '60594',
'city' => 'Frankfurt am Main',
First of all, a little example of how JSON_NUMERIC_CHECK works:
$data = [112, '34', 5.6, '7.8'];
echo json_encode($data) . PHP_EOL;
echo json_encode($data, JSON_NUMERIC_CHECK) . PHP_EOL;
... prints:
[112,"34",5.6,"7.8"]
[112,34,5.6,7.8]
There isn't any feature to determine which values should be parsed (and if they wanted to add it, what would its syntax be?). The only way to get the results you want is to ensure that source data is already using the appropriate PHP data types. And that's something you can certainly do (though the exact details depend on your specs):
$person = [
'age' => 33,
'zip_code' => '09007',
];
$person['age'] = is_null($person['age']) ? null : (int)$person['age'];
echo json_encode($person);
... prints:
{"age":33,"zip_code":"09007"}
Another solution for this issue if it's important enough for you to solve it in a robust way, and that would be to use the JMS Serializer library.
You would need to define a class for your data, but after that, whatever types you have defined via annotation will be honored during serialization.
With that said, Alvaro's post shows that casting will work. So, something like this should also work:
// Fix all Numerics
$data = json_decode(json_encode($data, JSON_NUMERIC_CHECK), true);
// Fix your 2 strings
$callback = function($row) {
if (array_key_exists('field1', $row)) {
$row['field1'] = (string)$row['field1'];
}
if (array_key_exists('field2', $row)) {
$row['field2'] = (string)$row['field2'];
}
return $row;
}
$data = array_map($callback, $data);
// This should be ready for final encode
return json_encode($data);
I tried this and it worked fine for me because I would only check JSON_NUMERIC_CHECK on id and with this
echo json_encode($article, JSON_NUMERIC_CHECK)
,and I got id and telephone as int.
Try this
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$row['id'] = (int)$row['id'];
$row['telephone'] = (string)$row['telephone'];
$db_data[] = $row;
}
//format json
$article->status = true;
$article->message = "success";
$article->employee = $db_data;
//return json
echo json_encode($article);
} else {
}
I am developing a web app using cake php 2.
I have an issue while displaying data from 2 tables...
My models :
<?php
class Discipline extends AppModel{
public $hasMany = "Student";
}
?>
<?php
class Student extends AppModel{
public $belongsTo = array(
"Discipline" => array(
"className" => "Discipline",
"foreignKey" => "discipline_id"
)
);
}
?>
Here is my studentscontroller :
<?php
class StudentsController extends AppController{
function admin_index(){
if($this->request->is('put') || $this->request->is('post')){
$student = $this->request->data['Student'];
if($this->Student->save($this->request->data)){
$this->Session->setFlash("L'eleve a bien été modifié","notif");
}
}
$d['student'] = $this->Student->find('all',array(
'contain' => "Discipline"
));
$this->set($d);
}
}
I am trying to display student's data using this view :
<?php
foreach($student as $k => $v){
$v = current($v);
echo "<td>Action</td>";
echo "<td>Label</td>";
echo "<td>".$v['nom']." ".$v['prenom']."</td>";
echo "<td>".$v['sexe']."</td>";
echo "<td>".$v['naissance']."</td>";
echo "<td>".$v['Discipline']['designation']."</td>";
echo "<td>".$v['comite']."</td>";
echo "<td>".$v['classe']."</td>";
echo "<td>".$v['elite']."</td>";
echo "<td>".$v['alerte']."</td>";
echo "<td>".$v['quota1']."</td>";
echo "<td>".$v['quota2']."</td>";
echo "<td>".$v['total']."</td>";
echo "<td>Pris</td>";
echo "<td>Restant</td>";
echo "<td>Supp</td>";
}
?>
But i have an issue on this line :
echo "<td>".$v['Discipline']['designation']."</td>";
It says this error :
notice (8): Undefined index: designation [APP\View\Students\admin_index.ctp, line 47]
I am used to develop on cakephp 3 and I am pretty embarassed with that error.. what to do to display data from Disciplines table from my student view ?
Any idea ? Thx
EDIT: I did a debug on my StudentsController, and I found the data I wanna display :
array(
'student' => array(
(int) 0 => array(
'Student' => array(
'id' => '2',
'prenom' => 'Jean',
'nom' => 'Michel',
'sexe' => '1',
'naissance' => '2015-08-02',
'age' => '12',
'classe' => '1',
'discipline_id' => '1',
'comite' => 'test',
'categorie' => 'test',
'elite' => true,
'alerte' => 'test',
'quota1' => '12',
'quota2' => '12',
'total' => '24',
'note' => 'tete'
),
'Discipline' => array(
**'id' => '1',
'designation' => 'Alpin'**
)
)
)
)
i think you should change the view
Change:
foreach($student as $k => $value){
$v = current($value);
to:
foreach($student as $k => $v){
$v = current($v);
and change:
echo "<td>".$v['Discipline']['designation']."</td>";
to
echo "<td>".$value['Discipline']['designation']."</td>";
You overwrite the variable $v with the first found array, so $v would be $v['Student']
I for myself won't use current to go inside a array for CakePHP, but use $v['Student'] everywhere
I'm have time overlap check inside while loop and I would also like to display events title with results which is in another array.
Without double foreach loop I can echo $event->title; and it displays all event titles. If I put array $event which has $event->title, inside foreach which I have for overlap testing it only displays first title.
I'm newbie PHP and I'm banging my head with complete solution for a couple of days now. Any help would be awesome :(
while (list(,$event_row) = each($events)) {
$event->loadEvent($event_row[0]);
foreach ($events as $thisevent) {
$event_array = array($event->id,$event->title);
$conflicts = 0;
foreach ($events as $thatevent) {
if ($thisevent[0] === $thatevent[0]) {
continue;
}
$thisevent_from = $thisevent[1];
$thisevent_ends = $thisevent[2];
$thatevent_from = $thatevent[1];
$thatevent_ends = $thatevent[2];
if ($thatevent_ends > $thisevent_from AND $thisevent_ends > $thatevent_from) {
echo $event->title;
$conflicts++;
echo "Event #" . $thisevent[0] . " overlaps with Event # " . $thatevent[0] . "\n";
}
}
if ($conflicts === 0) {
echo "Event #" . $thisevent[0] . " is OK\n";
}
}
}
Without double foreach var_export($event) displays all event arrays.
array( 'title' => 'Event 1', 'description' => 'Event description 1', 'contact' => 'event contact 1', 'url' => '', 'link' => '', 'email' => 'event#eventemail.com', 'picture' => '', 'color' => '#009933', 'catName' => 'Events', 'catDesc' => 'General events', 'startDate' => 1432918800, 'endDate' => 1432926000, 'startDay' => 29, 'startMonth' => 5, 'startYear' => 2015, 'endDay' => 29, 'endMonth' => 5, 'endYear' => 2015, 'recurStartDate' => NULL, 'recurEndDate' => NULL, 'id' => '11', 'catId' => 1, 'status' => true, 'recType' => '', 'recInterval' => 0, 'recEndDate' => 1432850400, 'recEndType' => 0, 'recEndCount' => 1, ))
And var_export($events) array displays three times all arrays with id, start date and end date:
array ( 0 => array ( 0 => '11', 1 => 1432918800, 2 => 1432926000, ), 1 => array ( 0 => '13', 1 => 1432918800, 2 => 1432926000, ), 2 => array ( 0 => '16', 1 => 1432926000, 2 => 1432929600, ), )
Thank you for looking into it and giving any possible solutions hints, ideas :)