objects ignores other array with same values.
for example
data[2018][2][25] <-- this ones gets ignored to the object
data[2018][2][22]
Code:
var date = new Date();
var data = {};
<?php $eventsNum = 3>
<?php for ($r =1; $r <= 3; $r++):?>
data[<?php echo $calendarYear[$r]?>] = {};
<?php for ($s =1; $s <= 3; $s++):?>
data[<?php echo $calendarYear[$r]?>][<?php echo $calendarMonth[$s]?>] = {};
<?php for ($t =1; $t <= 2; $t++):?>
data[<?php echo $calendarYear[$r]?>][<?php echo $calendarMonth[$s]?>][<?php echo $calendarDay[$s] ?>] = {};
//$num = $calendarDay[$s];
try {
data[<?php echo $calendarYear[$r]?>][<?php echo $calendarMonth[$s]?>][<?php echo $calendarDay[$s] ?>].push({
startTime: "<?php echo $calendarStart_time[1]?>",
endTime: "<?php echo $calendarEnd_time[1] ?>",
text: "<?php echo $calendar_description[1] ?>"
The problem is that each time through the loops you completely replace the existing object in that property. Change:
data[<?php echo $calendarYear[$r]?>] = {};
to:
if (!data[<?php echo $calendarYear[$r]?>]) {
data[<?php echo $calendarYear[$r]?>] = {};
}
and similarly for all the other initializations.
Related
<?php>
if (isset($_POST['search']))
{
$startDate = $_POST['start'];
$startDate = str_replace('/', '-', $startDate );
$startDate = date("Y-m-d", strtotime($startDate));
echo $startDate;
$endDate = $_POST['end'];
$endDate = str_replace('/', '-', $endDate );
$endDate = date("Y-m-d", strtotime($endDate));
echo $endDate;
$model = $_POST['model'];
echo $model;
$dates = getDatesStartToLast($startDate, $endDate);
for ($i=0; $i < count($dates); $i++){
echo "<tr>";
echo "<td><text class = 'dateselect'>$dates[$i]</text></td>";
$query = mysqli_query($conn, "SELECT * from electec_db where DateInputTime >= '$dates[$i]' and DateInputTime <= '$dates[$i] 23:59:59' and Model = '$model'");
$count = mysqli_num_rows($query);
// echo $count;
echo "<td>$count</td>";
$arrays = array("Result = 'NG'", "Species = 'Burr'", "Species = 'Dirt'", "Species = 'Scratch'", "Species = 'Cracked'", "Species = 'Gas'" );
for ($j=0; $j < count($arrays); $j ++){
$query = mysqli_query($conn, "SELECT * from electec_db where DateInputTime >= '$dates[$i]' and DateInputTime <= '$dates[$i] 23:59:59' and Model = '$model' and $arrays[$j]");
$count = mysqli_num_rows($query);
echo "<td>$count</td>";
// And this is jsp code
<script>
$('.dateselect').click(function() {
var dateSelect = $(this).text();
alert(dataSelect)
}
</script>
This is php code and javascript code.
When I click echo "$dates[$i]"; part,
I want to make react in script like alert dateSelect variable. But it doesn't make any reaction.
How to make a table react in javascript when clicked in php?
Do you have jQuery installed in your project? That's what the $ indicates.. Not sure what the jsp comment is about.
In straight javascript you could do something like this:
document.querySelectorAll('.dateSelect').forEach(item => {
item.addEventListener('click', event => {
alert(event.target.innerHTML)
})
})
<p class="dateSelect">thing1</p>
<p class="dateSelect">thing2</p>
<p class="dateSelect">thing3</p>
I have 2 php files. action.php and main.php
main.php is included in action.php
In action.php i have button and input.
<input type="text" class="form-control" name="multip" id="multip" value="" placeholder="MULTIPLY"/>
<button type="button" class="btn btn-primary" name="distribute" onclick="multiply()">Submit</button>
In main.php
$i = 1;
$sum1=0;
$distrib = array();
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
$count = $row["count"];
$price = floor($row["currency"]*$row["buyPrice"]);
$iid = $row["iid"];
$distrib[]=$count;
$sum1 += $row["count"]*$row["buyPrice"];
echo '';
echo '<tr><td><input type="checkbox" name="check[]" id="checked_'.($i-1)
.'" value="'.$row["count"].'"> </td>
<th scope="row">'.$i++.'</th>
<td>'.$row["insert_version"].'</td>
<td>'.$row["code"].'</td></tr>';
}
$result->free();
}
$mysqli->close();
and script part of main.php
<script>
let dist = <?php echo json_encode($distrib); ?>;
let mult;
let count = <?php echo json_encode($count); ?>;
let i = +"<?php echo $i; ?>";
function multiply(){
let bashxel = parseInt(document.getElementById("multip").value);
mult = 0;
for (let k = 0; k<i; k++) {
let checkBox = document.getElementById("checked_"+k);
if (checkBox.checked === true) {
mult+=parseInt(dist[k]);
console.log(mult); //this works
}}
console.log(mult+100); //this doesn't work
//Error from here
};
</script>
I don't know why this problem shows. I searched about it but couldn't found solution.
Help me to solve this problem.
Better code
const mult = [...document.querySelectorAll('[name^=check]:checked')]
.map((_,k) => +dist[k])
.reduce((a,b) => a+b)
but answering your question
You error is likely that you count from 1 here $i = 1;
Change $i = 1; to $i = 0;
Change <th scope="row">'.$i++.'</th> to <th scope="row">'.($i+1).'</th>
move $i++; to the end of the loop
Alternatively change
let i = +"<?php echo $i; ?>";
to
let i = document.querySelectorAll('[name^=check]:checked').length
I wanna make statistics in my website for the last 3 years
I want to show result like this
2016 : 159
2015 : 132
2014 : 200
I try my code (this's)
$date2 = date('Y');
$n = $date2;
for($i=$n-2;$i<=$n;$i++) {
$sql = "SELECT sum(count) AS value_sum FROM statistics where YEAR(st_date) = $i ";
$sql_sel = mysqli_query($conn,$sql);
echo '
<script>
var pieData = [
';
while($rows = mysqli_fetch_assoc($sql_sel)) {
if($i == $n-2) {
echo '{
value: '.$rows['value_sum'].',
color:"#337AB7"
},';
}
else if($i == $n-1) {
echo '{
value: '.$rows['value_sum'].',
color:"#FC8213"
},';
}
else if($i == $n) {
echo '{
value: '.$rows['value_sum'].',
color:"#8BC34A"
},';
}
echo'];
new Chart(document.getElementById("pie").getContext("2d")).Pie(pieData);
</script>';
}}
?>
but this code give me just 1 one row like this
2016 : 159
I wanna see all result, any help ?
You're creating 3 charts within your for loop. If you allow php to encode the data for you, you can echo the script outside of the PHP like so:
$date2 = date('Y');
$n = $date2;
$data = array();
for($i=$n-2;$i<=$n;$i++) {
$sql = "SELECT sum(count) AS value_sum FROM statistics where YEAR(st_date) = $i ";
$sql_sel = mysqli_query($conn,$sql);
while($rows = mysqli_fetch_assoc($sql_sel)) {
if($i == $n-2) {
$data[] = array('value'=> $rows['value_sum'], 'color'=>'#337AB7');
}
else if($i == $n-1) {
$data[] = array('value'=> $rows['value_sum'], 'color'=>'#FC8213');
}
else if($i == $n) {
$data[] = array('value'=> $rows['value_sum'], 'color'=>'#8BC34A');
}
}
}
?>
<script>
var pieData = <?php json_encode($data) ?>;
new Chart(document.getElementById("pie").getContext("2d")).Pie(pieData);
</script>
This should help you out:
<?php
// Store the array for the pie data within PHP at first
$js_pie_data = [];
// Define colors
$colors = [
date('Y') => '8BC34A', // current year
(date('Y') - 1) => 'FC8213', // last year
(date('Y') - 2) => '337AB7' // 2 years ago
];
$sql = "SELECT
YEAR(st_date) as `year`, -- get year from DB, makes life simpler :)
sum(count) AS value_sum
FROM
statistics
WHERE
YEAR(st_date) >= (YEAR(CURDATE())-2) -- get all records from 2 years ago to today
GROUP BY
YEAR(st_date) -- group by year so that our sum() above will work";
$sql_sel = mysqli_query($conn,$sql);
while($rows = mysqli_fetch_assoc($sql_sel))
{
// keep adding entries to the pie data
$js_pie_data[] = [
'value' => $rows['value_sum'],
'color' => '#'.$colors[$rows['year']] // based on year from DB, pick a color
];
}
echo '<script>
var pieData = '.json_encode($js_pie_data).'; // this will output a properly formatted JS array which will be understood by JS with no problem
new Chart(document.getElementById("pie").getContext("2d")).Pie(pieData);
</script>';
1)You can create array in php side.
2)Then convert it as json string.
3)Define javascript variable with that json string
4)Convert it as object with JSON.parse
5)Insert it in option
<?php
$db = Array("1","2","3");
$out = Array();
$x = 0;
foreach($db as $vals){
if($x == 1){
$out[] = Array("value"=>$vals,"color"=>"#bfbfbf");
}
if($x == 2){
$out[] = Array("value"=>$vals,"color"=>"#00ffff");
}
if($x == 3){
$out[] = Array("value"=>$vals,"color"=>"#fff00");
}
if($x == 3){$x = 0;}
$x++;
}
echo "
<script language='javascript'>
var stat_str = '".json_encode($out)."';
var stat_obj = JSON.parse(stat_str);
// then you can insert stat_obj if you need object into stats
</script>";
?>
in the model alert is not working. if condition is working,the only problem with the alert box, its not showing the dialog box.Please help..
public function setJumlahPenumpang ($idJadwal,$idPemesanan,$jml,$booked,$selected){
$data1 = $this->db->query('select p.jumlah_kursi, j.jumlah_penumpang, p.harga from tb_po p JOIN tb_jadwal j ON j.id_po = p.id_bus WHERE j.id_jadwal ='. $idJadwal);
foreach ($data1->result_array() as $dataa1) {
$tersedia = $dataa1['jumlah_kursi'] - $dataa1['jumlah_penumpang'];
if($tersedia < $jml){
?>
<script type="text/javascript">
document.location = '<?php echo base_url(); ?>proses/cekKode1/<?php echo $idPemesanan ?>';
alert("Tidak ada Bus Beroperasi");
</script>
<?php
}else{
$data3 = $dataa1['harga'] * $jml;
$this->db->query('update tb_pemesanan set harga = '.$data3.' where id_pemesanan = '.$idPemesanan);
$data = $this->db->query('select jumlah_penumpang from tb_jadwal where id_jadwal ='. $idJadwal);
$this->db->query("update tb_jadwal set booked = '".$booked."' where id_jadwal = ".$idJadwal);
$this->db->query("update tb_pemesanan set kursi = '".$selected."' where id_pemesanan = ".$idPemesanan);
foreach ($data->result_array() as $dataa) {
$data2 = $dataa['jumlah_penumpang'] + $jml;
$this->db->query('update tb_jadwal set jumlah_penumpang = '.$data2.' where id_jadwal = '.$idJadwal);
}
}
}
}
Try Echo Before Script Tag I hope So it 'll work....
if($package_id == NULL) {
echo '<script type="text/javascript">
window.location.href = "'.base_url().'"
</script>';
return;
die();
}
Here's my view :
$(".qty").keyup(function(){
var qty = $(this).val();
var bt = (this.id);
var bts = $('#bts'+bt).val();
var edge = $('#edge'+bt).val();
for (var i = 1; i<edge; ++i) {
var batas = $('#bts'+i).val();console.log(batas);
}
});
<input type="hidden" id="<?php echo 'edge'.$items['id']?>" value="<?php echo $items['options']['jum']?>"/>
<?php
foreach ($items['options']['stok'] as $stok) {
$er = $stok['stokbagus'];
$length = $items['options']['jum'];
for ($i=1; $i< $length; $i++) {
echo '<input type="text" rel="'.$items['rowid'].'" id="bts'.$i.'" value="'.$er.'"/>';
}
}
?>
$items['options']['jum'] contains = 2.
$stok['stokbagus'] contains = 30 and 21.
It'll display the first one (30). How to display all $stok['stokbagus'] in javascript?
Because i want to compare $(".qty").val() with all of $stok['stokbagus']
Okay. Here is what you should do.
If your $stok['stokbagus'] variable is array, then you should select the first and the second variable like this:
$first = $stok['stokbagus'][0];
$second = $stok['stokbagus'][1];
Else if, your $stok['stokbagus'] variable is a string and has 30,21 like this;
$vars = explode(",", $stok['stokbagus']);
$first = $vars[0];
$second = $vars[0];
You are saying that $stok['stokbagus'] variable has 30 and 21 values then it must be an array.
To show all values in your $stok['stokbagus'];
implode(', ', $stok['stokbagus']; // or just use $er.
Full:
echo '<input type="text" rel="'.$items['rowid'].'" id="bts'.$i.'" value="'.implode(', ', $er).'"/>';
Update:
<?php
foreach ($items['options']['stok'] as $stok) {
$er = $stok['stokbagus'];
$length = $items['options']['jum'];
for ($i=1; $i < $length; $i++) {
if(is_array($er)) {
foreach($er as $key => $value) {
echo '<input type="text" rel="'.$items['rowid'].'" class="bts" data-id="'.$i.'" value="'.$value.'"/>';
}
} else {
echo '<input type="text" rel="'.$items['rowid'].'" id="bts'.$i.'" value="'.$er.'"/>';
}
}
}
Js:
$(".qty").keyup(function(){
var qty = $(this).val();
var bt = (this.id);
var bts = $('#bts'+bt).val();
var edge = $('#edge'+bt).val();
for (var i = 1; i<edge; ++i) {
// I don't know what do you want to do with batas variable...
if($('.bts').length > 1) {
$('.bts').each(function(){
console.log($(this).val());
});
} else {
var batas = $('#bts'+i).val();console.log(batas);
}
}
});