Save is not working as expected in cakephp 1.2 - php

using cakePhp for an existing product they have old version 1.2.
I just started using CakePhp.
I tried to save some data to the table.
here is my code.
function add(){
//echo $id;
pr($this->data);
if(!empty($this->data)){
$this->User->create();
if($this->User->save($this->data)){
debug($this->User->validationErrors);
exit();
$this->Session->setFlash("Data saved man");
$this->redirect(array('action'=>'index',2));
}
}
}
Below is debug data.
Array
(
[user] => Array
(
[first_name] => hello
[last_name] => man
[email] => lrer#drei.cm
[password] => 123
[created_at] => Array
(
[month] => 01
[day] => 17
[year] => 2014
[hour] => 08
[min] => 56
[meridian] => am
)
[modified_at] => Array
(
[month] => 01
[day] => 17
[year] => 2014
[hour] => 08
[min] => 56
[meridian] => am
)
)
)
app\controllers\users_controller.php (line 36)
Array
(
)
(default) 0 query took ms
Nr Query Error Affected Num. rows Took (ms).
I have no clue why data is not saving to database. I need to get it work for version 1.2. thanks

I think user key should have capital U as User. Check your view code to make sure that the model passed to $form->create('User') not 'user'

Related

Generating a php array from mysql and want to use ID from db as array keys

The current output is..
Array
(
[0] => Array
(
[ID] => 20
[name] => Peter
[datetime] => Tuesday 26th Oct 21 3:50am
)
[1] => Array
(
[ID] => 21
[name] => Paul
[datetime] => Tuesday 26th Oct 21 4:44am
)
)
I would like the array output to be..
Array
(
[20] => Array
(
[ID] => 20
[name] => Peter
[datetime] => Tuesday 26th Oct 21 3:50am
)
[21] => Array
(
[ID] => 21
[name] => Paul
[datetime] => Tuesday 26th Oct 21 4:44am
)
)
The code I am currently using to generate the array is..
$sql=mysqli_query($conn,"SELECT * FROM `live`");
/*every time it fetches the row, adds it to array...*/
while($liveuserdata[]=mysqli_fetch_array($sql, MYSQLI_ASSOC));
I can't show you what i've tried as I don't know where to begin dispite several rephrased searches :-/
It is as simple as:
$sql = mysqli_query($conn,"SELECT * FROM `live`");
$liveuserdata = [];
while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {
$liveuserdata[$row['ID']] = $row;
}

Combine intersecting keys and values in 2 arrays

I'm running through all of the array functions on php.net and unable to figure this out.
Essentially I want to take these two arrays:
Array
(
[0] => stdClass Object
(
[month] => October
[year] => 2015
[credit] => 1000.00
)
[1] => stdClass Object
(
[month] => September
[year] => 2015
[credit] => 200.00
)
)
Array
(
[0] => stdClass Object
(
[month] => October
[year] => 2015
[debit] => 2000.00
)
[1] => stdClass Object
(
[month] => August
[year] => 2015
[debit] => 50.00
)
)
...and have the output look like this:
Array
(
[0] => stdClass Object
(
[month] => October
[year] => 2015
[credit] => 1000.00
[debit] => 2000.00
)
[1] => stdClass Object
(
[month] => September
[year] => 2015
[credit] => 200.00
[debit] => 0
)
[2] => stdClass Object
(
[month] => August
[year] => 2015
[credit] => 0
[debit] => 50.00
)
)
I'm looked to merge "month" and "year" and combine the other keys, using a default value if the key doesn't exist. Any guidance?
Assuming $debits and $credits are the arrays shown in your question, I would approach it like this:
First loop over the credits, inserting them into the new "combined" array and adding a default value for debit as you go.
foreach ($credits as $credit) {
$credit->debit = 0.00; // provide a default value for debit
$combined[$credit->year . $credit->month] = $credit;
}
Then loop over the debits. Since there is the possibilities that entries will already be there from credits, there needs to be a check for this. This part should update existing values inserted from credits, or insert new values if there is no existing value.
foreach ($debits as $debit) {
if (isset($combined[$debit->year . $debit->month])) {
// update the debit if the entry already exists
$combined[$debit->year . $debit->month]->debit = $debit->debit;
} else {
// otherwise create a new entry with a default value for credit
$debit->credit = 0.00;
$combined[$debit->year . $debit->month] = $debit;
}
}
// If you need the results to be numerically indexed, you can use array_values
$numerically_indexed = array_values($combined);

While iterating through an array of associative arrays, does PHP's foreach order iterations by key instead of index?

I am having troubling using foreach with an array of associative arrays, where the keys in the associative arrays are numbers.
$rows = $_POST["row"];
// print_r($rows);
foreach ($rows as $r) {
fwrite($f, $r["date"]);
fwrite($f, "#");
fwrite($f, $r["desc-short"]);
fwrite($f, "#");
// etc.
}
The POST variable contains arrays identified by row[index]. If I stick in a print_r() it displays the POST values in the order they appeared in the original form (which is not necessarily numerical order, as rows can be inserted in the middle and the counter represents when they were added, not where), but when I iterate with foreach it ends up printing row[8] (assuming eight rows) last, even though it was inserted after row 2 (for example).
It seems that because my keys are numbers, foreach is treating the keys as if they were the order. How can I avoid this behavior?
Output of example data from print_r($rows):
Array ( [1] => Array ( [date] => 12/12/2013 [desc-short] => Show title [desc-long] => A sample long description [start-time] => 12:30 [duration] => 13 [rating] => TVY ) [2] => Array ( [date] => 12/12/2013 [desc-short] => TEST [desc-long] => TEST [start-time] => 12:45 [duration] => 14 [rating] => TVY ) [8] => Array ( [date] => 12/12/2013 [desc-short] => Calendar of Events [desc-long] => A list of local events displayed every hour on the hour [start-time] => 13:00 [duration] => 15 [rating] => TVY ) [3] => Array ( [date] => 12/12/2013 [desc-short] => Show title [desc-long] => A sample long description [start-time] => 12:45 [duration] => 12 [rating] => TVY ) [4] => Array ( [date] => 12/12/2013 [desc-short] => Calendar of Events [desc-long] => A list of local events displayed every hour on the hour [start-time] => 13:00 [duration] => 15 [rating] => TVY ) [5] => Array ( [date] => 12/12/2013 [desc-short] => Show title [desc-long] => test [start-time] => 13:15 [duration] => 100 [rating] => TVY ) [6] => Array ( [date] => 12/12/2013 [desc-short] => Calendar of Events [desc-long] => A list of local events displayed every hour on the hour [start-time] => 15:00 [duration] => 15 [rating] => TVY ) )
And yes, 7 is missing. I'll have to look into that as well. Rows number 1-6 were loaded from a file, while [8] was added in their midst later with JavaScript.
You can't avoid this if your keys are stuck like this. That's just how PHP works. If you need to maintain the order then you'll need to prefix numbers with letter

PHP Array with Key Values don't match

I have a query that I'm running in php. The DB is SQL Server 2008.
The query in PHP is:
"SELECT * FROM applicants WHERE applicants.families_id = '{$family_array['id']}'";
Where the $family_array id is matched against the families_id. I get a single row as a result. I save that row to an array in PHP by using the mssql_fetch_array function. When I print this array I get the following:
Array
(
[0] => 26
[id] => 21
[1] => 21
[user_id] => 21
[2] => Kristi
[mother_fname] => Kristi
[3] => Lochala
[mother_lname] => Lochala
[4] => Nathan
[father_fname] => Nathan
[5] => Lochala
[father_lname] => Lochala
[6] =>
[app_emergency] =>
[7] =>
[upload_mother_passport] =>
[8] =>
[upload_mother_visa] =>
[9] =>
[upload_father_passport] =>
[10] => 0
[upload_father_visa] => 0
[11] => nathan-lochala
[user_added_username] => nathan-lochala
[12] => Mar 19 2013 01:00:37:660PM
[user_added_date] => Mar 19 2013 08:48:00:000AM
[13] => 192.168.88.15
[user_added_ip] => 192.168.88.15
[14] =>
[user_updated_username] =>
[15] =>
[user_updated_date] =>
[16] =>
[user_updated_ip] =>
[17] => 21
[18] => nathan-lochala
[username] => nathan-lochala
[19] => b9a234cb37ce2b75d77befecabfa650e39489e0b
[hash_password] => b9a234cb37ce2b75d77befecabfa650e39489e0b
[20] => Nathan
[fname] => Nathan
[21] => Lochala
[lname] => Lochala
[22] => 2
[num_child] => 2
[23] => Mar 19 2013 08:48:00:000AM
[24] => 192.168.88.15
[25] =>
[26] =>
[27] => nathan-lochala#shk.qsi.org
[email] => nathan-lochala#shk.qsi.org
[28] => parent
[access] => parent
)
If you notice, the index [0] does not match the corresponding key value [id]. Why is this? I've ran the exact same query using the SQL Server Manager and it performs as expected, but when I fetch that array in PHP only the first key value gets skewed. I've tried everything I can think of short of recreating the table. Any ideas?
EDIT:
Running mssql_fetch_assoc() gives me the following results:
Array
(
[id] => 21
[user_id] => 21
[mother_fname] => Kristi
You are not including all the pertinent information either in the posted SQL or in the table data/structure you've included. Your query is SELECT * FROM applicants WHERE applicants.families_id = ? yet the table structure you've posted does not contain a families_id column (nor is it named applicants). Nor does it contain much of the data in the posted array, e.g., hash_password, username, etc.
From this I deduce that you're actually doing a JOIN on a users table. What's most likely occurring is that the JOIN is including the id column from the users table which is overwriting the id in the main table (families/applicant, whatever it's called) once the array is built. user_id is already included in your main table so you should explicitly list the columns in your SQL statement, leaving out the users.id column.
You need to make it mssql_fetch_array($result, MSSQL_ASSOC) as MSSQL_BOTH is assumed.
I found the answer here:
http://php.net/manual/en/function.mssql-fetch-array.php

time in array - how to convert to timestamp?

I have an array with time in PHP:
[time] => Array (
[year] => 2007
[month] => 1
[day] => 2
[hour] => 1
[minute] => 1 )
)
i must save this in MySQL. in MySQL i have format
2007-01-02 01:01
how is the best method for modify array with time to timestamp?
sprintf("%04d-%02d-%02d %02d:%02d", $arr['time']['year'], $arr['time']['month'], $arr['time']['day'], $arr['time']['hour'], $arr['time']['minute']);

Categories