I want to display data from the controller and throw it in the view as a dataset, but I have trouble how to write a loop on scriptjava.
This is the code that I have tried, and still fails
This my controller :
laravel
$query = DB::table('app_history_calls')
->SELECT(DB::raw("(MONTH(date)) as bulan"))
->GROUPBY('date',DB::raw("MONTH(date)"))
->ORDERBY('date','asc')->get();
return view('dashboard.index',compact('query'));
}
and this my view:
laravel
<script type="text/javascript">
var month= [
<?php foreach ($query as $bulan): ?>
<?php echo (string)$bulan ?>
<?php endforeach; ?>
];
</script>
I expected the result will be like this:
"var month= [1,2,3,4,5,6,7,8,9,10,11,12];"
So that it can be read on the dataset
$data['query'] = DB::table('app_history_calls')
->SELECT(DB::raw("(MONTH(date)) as bulan"))
->GROUPBY('date',DB::raw("MONTH(date)"))
->ORDERBY('date','asc')->get();
return view('dashboard.index')->withdata($data);
}
<script type="text/javascript">
var month= [
<?php foreach ($data['query'] as $bulan): ?>
<?php echo (string)$bulan ?>
<?php endforeach; ?>
];
</script>
Replace above code with your code i passed value in $data array and in for loop read $data['query'] in js
Related
Hi the code below is for my search page and its working properly and it will show the search item along with the map marker what im trying to do now is show all post from the category example "category1", "category2" how can i do that? thank you so much
<div id="posts_cont">
<?php
while ($search_query->have_posts()) : $search_query->the_post();
$post_id = get_the_ID ();
$location = get_field_object('gmap', $post_id)['value'];
$title = get_the_title($post_id);
$url = get_permalink($post_id);
$comments = get_comments(array('post_id' => $post_id));
//$ratings = rating_form();
$thumb_id = get_post_thumbnail_id($post_id);
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail', true);
$thumb_url = $thumb_url_array[0];
if( !empty($location) ):
?>
<script type="text/javascript">
$(".acf-map").append("<div class='marker' data-lat='<?php echo $location['lat']; ?>' data-lng='<?php echo $location['lng']; ?>' title='<?php echo the_title_attribute(); ?>' ></div>");
</script>
<?php endif; ?>
<?php endwhile; ?>
<?php if (!have_posts() || $search_query->post_count == 0) { ?>
<script type="text/javascript">
$("#search-title").hide();
$(".acf-map").hide();
$(".search-text").hide();
$(".container").append("<h6 class='title'>Oops! No results were found.</h6>");
</script>
<?php } ?>
I would like to keep values like below
var skus = [];
<?php
foreach($col as $Product)
{
?>
<script type="text/javascript">
skus['<?php echo $Product->id; ?>'] = '<?php echo $Product->sku; ?>','<?php echo $Product->color; ?>';
</script>
<?php
}
?>
But this is not working.
UPDATE
Later I would like to search values of the array skus using that index.Is it possible in JavaScript like below??
var my_array = [];
my_arry['abc'] = 'pqr','xyz';
Is it possible to store multiple values in an index of an array in JavaScript??
Thanks
It looks like the problem with your code is a typo in the JS. I assume you want to make an array out of the PHP values in JS, so it looks like all you're missing is the square brackets around your value list.
Like so:
skus['<?php echo $Product->id; ?>'] = ['<?php echo $Product->sku; ?>','<?php echo $Product->color; ?>'];
You can use some objects:
So:
skus['<?php echo $Product->id; ?>'] = {'sku':'<?php echo $Product->sku; ?>','color':'<?php echo $Product->color; ?>'};
So every element of the skus array is an object with two keys
My PHP is a little rusty but is this what you mean?
<script type="text/javascript">
var skus = [];
<?php foreach($col as $Product) { ?>
skus['<?php echo $Product->id; ?>'] = ['<?php echo $Product->sku; ?>','<?php echo $Product->color; ?>'];
<?php } ?>
</script>
You need [] on that middle line because you're trying to save two values into each "cell" of the array, right?
It seems like when i echo out the result in php, it will loop through the database and display it in the "echo" below but if I try to display it using javascript, it does not even loop. what's the problem in the javascript?
<?php
while($row = $result->fetch()){
$id = $row['id'];
echo $id; //loops through and displays the all the id's in order in the database
?>
//this only displays the first id and doesn't loop even though it is in the php while loop
<script>
document.getElementById("test").innerHTML = "<?= $id ?>";
</script>
<span id="test"></span>
<?php
}
?>
please try with this
<?php
while($row = $result->fetch()){
$id = $row['id'];
echo $id; //loops through and displays the all the id's in order in the database
?>
//this only displays the first id and doesn't loop even though it is in the php while loop
<!-- change in id name -->
<span id="test_<?=$id?>"></span>
<script type="text/javascript">
//change here
document.getElementById("test_<?=$id?>").innerHTML = "<?= $id ?>";
</script>
<?php
}
?>
I have found these two ways:
1st way (width incorrect)
<?php
$arr = new Array (...);
?>
<input id="arr" value="<?php echo json_encode($arr); ?>" hidden>
<script>
$(function (){
var arr = $("#arr").val(); // array with trash
});
</script>
2nd (we need control sequence)
<?php
$arrJS = json_encode($arr);
?>
<script>
var arrJS = <?php echo $arrJS; ?>; // good array
</script>
Question: Is there a better attempt?
Have you considered using AJAX? Here's an example:
Javascript:
$.ajax( "example.php" )
.done(function(json) {
var jsObject = json;
})
and in your 'example.php' file:
<?php
$arr = new Array ('element1'=>'value1', 'element2'=>'value2');
header("Content-type: text/json");
echo json_encode($arr);
exit;
?>
How to load the output buffer into a string using javascript ?
for example in php
<?php ob_start(); ?>
hello world !
<?php $string = ob_get_contents(); ?>
To be more specific, code is tested!
<?php ob_start(); ?>
hello world !
<?php $string = ob_get_contents(); ?>
<script type="text/javascript">
var MyJSStringVar = "<? echo trim($string)?>";
console.log(MyJSStringVar);
alert(MyJSStringVar);
</script>
If you are running PHP 5.2 or above, you can call json_encode() to automatically escape the string properly for JavaScript.
<?php ob_start(); ?>
hello world !
<?php $string = ob_get_contents(); ?>
<script type="text/javascript">
var theString = <?php echo json_encode($string); ?>;
</script>
For PHP 5.1 or below, you can use a compatibility library, like jsonwrapper, that adds the json_encode function for you.