This is my code from controller where I used looping to show this.
<input type="hidden" class="notif_date" value="'.$created_at.'">
the sample data of this are multiple dates: 2019-05-28 13:45:45
Now I'm trying the get the last date that will be shown using JavaScript.
Currently I have this code:
var last_date = document.getElementsByClassName("notif_date");
console.log(last_date);
But this only gets the array of input types
So I tried to use last() and place like this console.log(last_date.last()); and this gives me an error like this
Uncaught TypeError: last_date.last is not a function
How can I get the last value of date using JavaScript?
To get last element you can use following code
let elements = document.getElementsByClassName("notif_date");
console.log(elements[elements.length-1]);
This code will provide you the correct output which you want.
Edited
If you want to get value of last element then use this:-
console.log(elements[elements.length-1].val());
Note: But you can't get value directly from document.getElementsByClassName("notif_date"). Because this return multiple object of multiple class elements.
But if you want to get value of all the elements then you need to loop this.
Using new Array.prototype.at() method.
let elements = [...document.getElementsByClassName("notif_date")];
let last_input = elements.at(-1);
let last_value = elements.at(-1).value;
console.log(last_input, last_value);
Related
I simply want to know how to access array elements retrieved from a database. I have the following code to get the names of each item in my database.
$plat_options = $this->db->get('tblplatform_options')->select('name')->result();
How do I go about accessing the name from the array $plat_options? Typically I would do $plat_options[0] for the first element in C#, how is this done in php/codeigniter?
In PHP/Codeigniter, can be done in the same way:
$plat_options[0] //if you have this element, usually is better to check if exists.
You can retrieve all the elements with foreach($plat_options as $option){...}
You can cast to object: https://www.kathirvel.com/php-convert-or-cast-array-to-object-object-to-array/
Or use a Codeigniter Helper (assuming you are using CI3): http://www.codeigniter.com/user_guide/helpers/array_helper.html
I recomend to know which is your array format and retrieve that way (if you don't know, you can do a: var_dump($plat_options) ) to know if is an associative array.
You can use the result_array() function:
$data = $plat_options->result_array();
echo($data[0]['name']);
or:
$data = array_shift($q->result_array());
echo($data['name']);
I extracted this last part from: Codeigniter $this->db->get(), how do I return values for a specific row? that you could check too.
If you don't know a lof of CI, the best you can do is do a simple tutorial to understand how the data + ActiveRecord works.
Hope it helps!
I have a Word template, where I go through a TBS block and dynamically display the values. Now I'd like to compare the actual value with the last value displayed. Is there any possibility to solve this in word?
I was thinking of setting a variable and save the last value in this variable. So I only have to compare my own variable with the actual value. But I cant figure out, whether this is possible or not. Any help or other suggestions?
Example
*[myblock;block=begin]
[myblock.entry] // here I want to check if its the same as the last entry
[myblock;block=end]*
TinyButStrong cannot do that in native.
But you can use parameter « ondata » and a PHP user function in order to add the previous value in the current record.
You can also use a method of an object (see TBS documentation)
PHP :
function f_ondata_user($BlockName, &$CurrRec, $RecNum) {
static $entry_prev = '';
$CurrRec['entry_prev'] = $entry_prev;
$entry_prev = $CurrRec['entry'];
}
Template :
*[myblock;block=begin;ondata=f_ondata_user]
[myblock.entry]
[myblock.entry_prev] // here I want to check if its the same as the last entry
[myblock;block=end]*
I´m still new in php and I´m having trouble in sending a form vars through POST.
I use a jquery to dynamically add new fields with the same name plus a auto increment ID in a form.
In the _POST I need to have a loop to get those vars, but how am I going to get the vars count for the loop?
I have something like this:
$ID1_name = $_POST["ID1_name"];
$ID2_name = $_POST["ID2_name"];
$ID3_name = $_POST["ID3_name"];
$IDX_name = $_POST["IDX_name"];
$ID1_place = $_POST["ID1_place"];
$ID2_place = $_POST["ID2_place"];
$ID3_place = $_POST["ID3_place"];
$IDX_place = $_POST["IDX_place"];
There can be unlimited variables with same name with and AI, and I have around 10 vars like that.
How can I count by the var partial name?
or
Is there a better way to get those _POST? I´m using a For loop.
I was using the same name and counting in the array. It worked, but I have 3 fields with radio and 2 with checkbox, and in the array all the vars of the same name merge. So the array for food have entries from all food checkbox from the field. Does that make sense?
So in one $ID1_place = string I might have $ID1_check = array
Thanks for the help. I´know there is a easy way, but I think I´m searching the wrong way since I found nothing.
Thanks a lot.
regards
There's two ways I can think for this. Either you can pass through a hidden element with the number of elements with your name, or you can use the preg_grep function to match all POST vars matching a regex pattern:
// Search keys:
$ID_place_keys = preg_grep('/ID[0-9]+_place/', array_keys($_POST));
// Search values:
$ID_places = preg_grep('/ID[0-9]+_place/', $_POST);
// Get array of values matching keys:
$ID_places = array_intersect_key($_POST, array_flip(preg_grep('/ID[0-9]+_place/', array_keys($_POST))));
// Gets a bit messy with all the array functions...
And here's a URL to a simple working example:
http://ideone.com/a9AhL6
Have you considered instead of sending a lot of post variables, to send only two; where all the fields are json encoded?
Example: names: ["ID1", "ID2", "ID3"] and places: ["ID1", "ID2", "ID3"]. To get the AJAX parameters like this you can stringify the arrays you have using JSON.stringify(). Ref: JSON.stringify()
Then on the back-end just do $names = json_decode($_POST["names"]) and $places = json_decode($_POST["places"])
This way you have an array with the id-s of the names and places. You can count them, loop over them, do whatever you need to.
I am passing a jquery array called 'selected' full of ids along with the opening of an ajax modal.
$('#dtDelete').on('click', function () {
$('#modal-ajax').load('/modals/m__delete.php?selected='+selected);
$('#modal-ajax').modal('show');
});
On the modal php page count($_GET['selected']); always returns 1 no matter what. I am trying to get an actual count of the number of values in the array. Turns out this is because the array is a string as noted below.
var_dump($_GET['selected']); returns something along the lines of string(69) "187419,187420,187413,187414,187415,187416,187417,187418,187421,187422" which is something I am not accustomed to (sort of new to jquery). I need to do processing in php using foreach on this array. Can I 'convert' this to a 'normal' php array so count() will work as expected and I can process it normally in php?
Lastly, this array may or may not be extremely large at times. The jquery function above opens an ajax modal (I am using the modal as a confirmation box for the user whether they really want to delete the entries in the selected array) and I know the $_GET method has limits to the amount of data it can pass. I can't do $_POST because this is a modal and I need to load it then show it... how else can I pass this data?
$_GET['selected']
returns the STRING after the attribute 'selected', and count(string) is 1 not matter what ( it's not a multi-dimension array to be greater than 1).
As for the comma separated string example you gave, you may use the following :
$selected = $_GET['selected'];
//test wether the string has contents
if(strlen($selected)!=0) {
$selected_array = explode(',',$selected); //this is the new array that you want
}
else {
//the string is empty
}
There are many string functions you may check at : http://www.php.net/manual/en/ref.strings.php
I'm getting a few rows from the DB with an ajax call in PHP and I save the result to an array with javascript.
Then I make some changes in the data and wish to update the DB.
So I use another ajax call for that but I can't manage to access the fields inside the rows correctly.
When I try this I get nothing:
echo $bArray[$i].branchId;
When I try this:
echo json_encode($bArray[$i].branchId);
I get ArraybranchId instead of the field value.
What's the correct way to access the field with php?
Try either for array:
$bArray[$i]['branchId']
or for object:
$bArray[$i]->branchId
depending which type $bArray[$i] is (array or object). You have not written in your question, so I showed both ways.
I take it branchId is the name of the field, and you want the value for that field?
If so, it's:
echo $bArray['branchId']; or
echo $bArray[$i]['branchId']
Edit: Also, you'll need to make sure you're using mysql_fetch_assoc not mysql_fetch_array!