PHP Code
$sql = "SELECT location FROM data";
$result=$conn->query($sql);
$data = array();
while($row = $result->fetch_assoc()){
$data[] = $row["location"];
}
Javascript
function getPoints(){
return [
var coordinateArray = <?php echo json_encode($data); ?>;
for(i-0;i<coordinateArray.length; i++({
var sep = coordinateArrays.split(',');
new google.maps.LatLng(sep[0], sep[1]);
}
];
}
Sample data in table:
location = 12.121212,13.131313 -> No quotes
data = "Hello"
author="Me"
Currently it breaks declaring the Array variable in javascript.Something is breaking there. Any help is appreciated
seems you have a name mismatch coordinateArray / coordinateArrays and and index problem try
var coordinateArray = <?php echo json_encode($data); ?>;
for(i=0; i<coordinateArray.length; i++) {
var sep = coordinateArray[i].split(',');
new google.maps.LatLng(sep[0], sep[1]);
}
and also problem with parentesis i++( and assignment i-1 instead of i++) and i=1
Related
Frist send data view page than json_encoded array data by Laravel. json_encode after var livetrde = '';
livetrdeObj = jQuery.parseJSON(livetrde);
Data formate almost done. I can't understand why does not show my chart data.
enter image description here
`for($i=0; $i<$different_days; $i=$i+$day){
$data[$i] = TradingProduct::where('group_product_id', $group_product_id)
->whereBetween(TradingProduct::raw('date(created_at)'), [Carbon::now()->subDays($i+$day), Carbon::now()->subDays($i)])
->orderBy('created_at', 'ASC')
->get();
$liveTrade = [];
$arrData = [];
foreach ($data as $key => $value) {
$arrData[$key] = $value;
$open = [];
$high = [];
$low = [];
$close = [];
$arrPrice = [];
$time = [];
$dd = 15;
$j = 0;
foreach ($arrData[$key] as $arrDatakey => $arr) {
if(!empty($arr['price'])){
$arrPrice[$arrDatakey] = $arr['price'];
$time[] = $arr['created_at'];
$open = reset($arrPrice);
$high = max($arrPrice);
$low = min($arrPrice);
$close = last($arrPrice);
$start_date_time = Carbon::now()->subDays($j);
$end_date_time = Carbon::now()->subDays($j+$dd);
$different_days_time = $start_date_time->diffInDays($end_date_time);
$currentTime = $start_date_time->subDays($different_days_time)->timestamp;
$j = $j+$dd;
}
}
$liveTrade[] = '{x: new Date('.$currentTime.'), y: ['.$open.','.$high.','.$low.','.$close.']}';
}
}`
The problem is here in creating the $liveTrade[] data. You are using Date() as string where it should create an object (Note that I'm already formatting the date in php itself in the format 'Y-m-d' which can be changed as per your needs). Update it to be like this:
$liveTrade[] = '{x: ' . (date('c', $currentTime)) .', y: ['.$open.','.$high.','.$low.','.$close.']}';
The better solution would be to push and array into $liveTrade[] and later in the end convert it into json like below:
// within the foreach loop you need to convert timestamp to date
$liveTrade[] = array('x' => date('c', $currentTime), 'y' => [$open, $high, $low, $close]);
// use date('D M d Y H:i:s O', $currentTime) to mimic JS date object
// ... Before returning response
$response = json_encode($liveTrade);
echo $response;
When i put
$link15 = $link15.$U;
$newpdf1 = $newpdf1.$_SESSION['arrayvalue']."\r\n\r\n".$link15."\r\n\r\n";
$link15 = "";
this line of code in foreach/for loop the the script is stop after displaying one result. and when i remove this line of code it runs.
Below is the complete script:
for ($i = 0, $count = count($arr1); $i < $count; $i++) {
print $arr1[$i]."\r\n\r\n";
$_SESSION['arrayvalue'] = "$arr1[$i]";
$in = $arr1[$i];
$in = str_replace(' ','+',$in); // space is a +
$result15 = httpGet("https://www.google.com/cse?cx=003255331468891741234:xxxxxxxxxx&client=google-csbe&output=xml_no_dtd&q='.$in.'&oq='.$in.'");
//echo $result15;
//this is to get perticular tag/node value
$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($result15);
$N = $dom->getElementsByTagName('U');
foreach ($N as $U) {
echo $U->nodeValue, PHP_EOL."<br/>";
$link15 = $link15.$U;
}
$newpdf1 = $newpdf1.$_SESSION['arrayvalue']."\r\n\r\n".$link15."\r\n\r\n";
$link15 = "";
}
Where i am doing error in concatenation or any other error.
Thank You!
Based on your code, $U is an object, yet you're trying to concatenate it to $link15.
Try changing this:
$link15 = $link15.$U;
To this:
$link15 = $link15.$U->nodeValue;
You cannot concatenate an object with a string
try the below code (what I have changed is I am accessing the particular key named as "nodeValue" in your $U object. If you want to add another change it accordingly, you need to access the particular key or bunch of keys depending on your requirement)
for ($i = 0, $count = count($arr1); $i < $count; $i++)
{
print $arr1[$i]."\r\n\r\n";
$_SESSION['arrayvalue'] = "$arr1[$i]";
$in = $arr1[$i];
$in = str_replace(' ','+',$in); // space is a +
$result15 = httpGet("https://www.google.com/cse?cx=0032553314688917412345:xxxxxxxxx&client=google-csbe&output=xml_no_dtd&q='.$in.'&oq='.$in.'");
//echo $result15;
//this is to get perticular tag/node value
$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($result15);
$N = $dom->getElementsByTagName('U');
foreach ($N as $U) {
echo $U->nodeValue, PHP_EOL."<br/>";
// here you need to access the particular key or bunch of keys depending on your requirement
$link15 = $link15.$U->nodeValue;
}
$newpdf1 = $newpdf1.$_SESSION['arrayvalue']."\r\n\r\n".$link15."\r\n\r\n";
$link15 = "";
}
Hope this helps.
I wanted to display a graph in javascript using data from php. My code is:
<?php $i = 20; ?>
while (data.length < totalPoints) {
array_js.push(<?php echo json_encode($array_php[++$i][1]);?>);
}
The problem is that even though $i is declared before the while loop, it gets back to 20 all the time, so the data pushed in array_js is always $array_php[21][1].
Edit: here is more code, (not same variable name...)
startConn(); //connect to database
$query = "SELECT * FROM Reading";
$data = getAllDatas($query);
?>
<script type="text/javascript">
$(function() {
var data = [], totalPoints = 300;
function getRandomData() {
<?php $i = 20; ?>
while (data.length < totalPoints) {
data.push(<?php echo json_encode($data[--$i][1]);?>);
//data.push(<?php echo ++$i;?>);
}
} // END getRandomData()
You have a misunderstanding on how server side and client side code work.
I think this may help you
$(function() {
var data = [], totalPoints = 3;
var i = <?php $i = 1;echo $i; ?>;
var j = parseInt(i);
var arr = <?php echo json_encode($data); ?>;
function getRandomData() {
while (data.length < totalPoints) {
data.push(arr[j][1]);
j++;
}
return data;
}
var data1 = getRandomData();
console.log(data1);
});
Try this one. You can pass the value of the php variable using echo while declaring another variable on the java script.
<?php $i = 20; ?>
var i = <?php echo $i;?>;
while (data.length < totalPoints) {
array_js.push(<?php echo json_encode($array_php[++i][1]);?>);
}
There are plenty of examples of using database records to disable certain dates (unavailable) using JSON and AJAX however for some reason, these are done in PHP. Is there reason why it isn't done in JAVA and how can I replace the PHP file with Java for the exact same result?
checkDates.php
<? php
$sql = "SELECT start from gbh_rooster_afwijkend WHERE dlnmrID = '".$_GET['dld'].
"'";
$res = mysql_query($sql) or die(mysql_error());
$checkDates = array();
while ($row = mysql_fetch_assoc($res)) {
$checkDate['start'] = $row['start'];
$checkDates[] = $checkDate;
}
echo json_encode($checkDates);
?>
Javascript
$.getJSON('script/php/afwijkendrooster/checkDates.php?dld='+ id, function(json){dates=json;});
function checkAvailability(mydate){
var myBadDates = dates;
var $return=true;
var $returnclass ="available";
$checkdate = $.datepicker.formatDate('dd-mm-yy', mydate);
// start loop
for(var x in myBadDates)
{
$myBadDates = new Array( myBadDates[x]['start']);
for(var i = 0; i < $myBadDates.length; i++)
if($myBadDates[i] == $checkdate)
{
$return = false;
$returnclass= "unavailable";
}
}
//end loop
return [$return,$returnclass];
}
I have the following data in an SQL database
cname, ccode, colour
Great Britain, GB, 1
Italy, IT, 1
France, FR, 1
Spain, ES, 1
How can I create a JavaScript object like below?
var countries = {"GB":1, "IT":1, "FR":1, "ES":1}
So far, I have the following code;
PHP
$query2 = "SELECT ccode,colour FROM country_data WHERE 1";
$result2 = mysql_query($query2);
if (!$result2) {
die('Invalid query: ' . mysql_error());
}
$rows2 = array();
while($r2 = mysql_fetch_assoc($result2)) {
$rows2[] = $r2;
}
JS
var colours = '<?php print json_encode($rows2); ?>';
Which returns:
[{"ccode":"GB","colour":"1"},{"ccode":"IT","colour":"1"},{"ccode":"FR","colour":"1"},{"ccode":"ES","colour":"1"}]
change this code
while($r2 = mysql_fetch_assoc($result2)) {
$rows2[] = $r2;
}
to this one:
while($r2 = mysql_fetch_assoc($result2)) {
$rows2[$r2['ccode']] = $r2['colour'];
}
change php code as told in others answers too and it should be like
$rows2 = array();
while($r2 = mysql_fetch_assoc($result2)) {
$rows2[$r2['ccode']] = $r2['colour'];
}
if it does not work then try this:
$rows2 = array();
while($r2 = mysql_fetch_assoc($result2)) {
$rows2["'".$r2['ccode']."'"] = $r2['colour'];
}
if it also does not work then try this:
$rows2 = array();
while($r2 = mysql_fetch_assoc($result2)) {
$rows2[{$r2['ccode']}] = $r2['colour'];
}