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?
Related
I currently have a PHP script that works well, but when it comes to adding JavaScript I'm having a small issue.
What I'm trying to achieve is display the results in a php foreach loop which works well, but I have a onclick JavaScript function that is suppose to display results in the same row as the results of the foreach loop, but instead it changes the current value of only one results.
So I currently have four car types with a price. When one is selected I would like the name and price to display on top of the [echo "Select:" . $name] line.
My current code is this:
<?php
$cars = array(
array("Volvo",40.000),
array("BMW",45.000),
array("Saab",50.000),
array("Land Rover",60.000)
);
foreach($cars as $key){
$name = $key[0];
$price = $key[1];
echo "<span id='$name'></span> <span id='$price'></span><br />";
echo "Select: " . $name;
?>
<script type="text/javascript">
function grabData(d){
document.getElementById("<?php echo $price; ?>").innerHTML = d.getAttribute("data-price");
document.getElementById("<?php echo $name; ?>").innerHTML = d.getAttribute("data-title");
}
</script>
<input onclick="grabData(this);" type="radio" name="rate" data-price="<?php echo $key[1]; ?>" data-title="<?php echo $key[0]; ?>"><br />
<?php
}
?>
So if all options are selected the end results should look like this:
Volvo 40
Volvo:
BMW 45
BMW:
Saab 50
Saab:
Land Rover 60
Land Rover:
Any help or direction is highly appreciated. Thanks!
The loop is simply defining the same function over and over. When it gets called, it will call the last definition, not the one for that element in the loop.
You should just define the function once, and add additional parameters. Then you can call it with different parameters for each element.
<script type="text/javascript">
function grabData(d, priceid, nameid){
document.getElementById(priceid).innerHTML = d.getAttribute("data-price");
document.getElementById(nameid).innerHTML = d.getAttribute("data-title");
}
</script>
<?php
$cars = array(
array("Volvo",40.000),
array("BMW",45.000),
array("Saab",50.000),
array("Land Rover",60.000)
);
foreach($cars as $key){
$name = $key[0];
$price = $key[1];
echo "<span id='$name'></span> <span id='$price'></span><br />";
echo "Select: " . $name;
?>
<input onclick="grabData(this, '<?php echo $price; ?>', '<?php echo $name; ?>');" type="radio" name="rate" data-price="<?php echo $key[1]; ?>" data-title="<?php echo $key[0]; ?>"><br />
<?php
}
?>
set the script part as a string in a variable and echo it using php, php sees every <?php ?> as a different coding block.
I am fetching an array from a MySQL result into a array variable. I can successfully use a simple php echo line in the javascript beneath to grab the first $row element, but I want to use json_encode to get the whole array at once.
When I do this and try to set a javascript var to the first array element something goes wrong and even the single var method stops working.
<?php
.
.
.
while($row = $result->fetch_array(MYSQLI_NUM)) {
$row1 = $row[0];
}
?>
<script type="text/javascript">
var RowArray = <?php echo json_encode($row); ?>;
var RA1 = RowArray[0];
window.alert(RA1);
var Row1 = '<?php echo $row1; ?>';
window.alert(Row1);
</script>
Make an array containing all the records:
$rows = [];
while ($row = $result->fetch_array(MYSQLI_NUM))
{
// do custom modifications to $row if neededed
$rows[] = $row; // push to array
}
Or just:
$rows = $result->fetch_all(MYSQLI_NUM);
And then use json_encode() with $rows.
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;
?>
not pretty sure how you do this. But I want to save my javascript variables as data(1,2,3,4,...) in my while loop. Happy for any help! Thanks
$id = 0;
while($rows = mysql_fetch_array($data)){
$id = $id + 1;
$data = $rows['data'];
?>
<script>
var id = '<?php echo $id; ?>'; //1,2,3,4,5,6,...
var data = '<?php echo $id; ?>'; // Example - Data 1 = Frog, Data 2 = Bird
// So here I want to sava variables so the name will be
// var "data+'id'";
// And the output is data for that row
</script>
<?php
}
?>
//And here outside I want to use the variables like this:
document.write(data1); writes out "Frog"
document.write(data2); writes out "Pig"
<script>
var jsData = [];
<?php
$id = 0;
while($rows = mysql_fetch_array($data)){
$data = $rows['data'];
?>
jsData[<?php echo $id; ?>] = '<?php echo $data; ?>';
<?php
$id++;
}
?>
</script>
This would yield a JS Array called jsData where your ID is the index and which is filled with the PHP $data values.
Declare $data = new Array(); just before the WHILE LOOP then in the WHILE LOOP you could do $data['id'][$id]= $id which will collect all the IDS.
Add $data['data'][$id]= $row['data'] below it so as also to collect each data that you want.
in the script tag do this:
var id = <?php echo '['.join(',', $data['id']).']' ?> //will create --> [1,2,3,4,5,6,7]
var data = <?php echo '['.join(',', $data['data']).']' ?> //--> ['cat', 'dog', 'cow', 'sheep']
now you can access the variable in the Javascript array created eg. $data[0] // 'cat'