0

I need to create an dynamic array which will be sent via $("#ratings").load("ratingscalculator.php?Myarray="+myarray) and then used in a select query

$result = $database->connection->prepare("SELECT *, (SELECT COUNT(*)+1 FROM TABLE WHERE kampanj > t.kampanj and (kampanjnamn = 'Kampanjnamn_1' or kampanjnamn = 'Kampanjnamn_7' or kampanjnamn = 'Kampanjnamn_3' or kampanjnamn = 'Kampanjnamn_4' )) as 'rank', 
(SELECT COUNT(*) FROM TABLE where (kampanjnamn = 'Kampanjnamn_1' or kampanjnamn = 'Kampanjnamn_7' or kampanjnamn = 'Kampanjnamn_3' or kampanjnamn = 'Kampanjnamn_4') ) as 'counting'
FROM TABLE t
where kampanjnamn='$valtkampanjnamn'"); 

So it's this bit that I need to dynamically create somehow (kampanjnamn = 'Kampanjnamn_1' or kampanjnamn = 'Kampanjnamn_7' or kampanjnamn = 'Kampanjnamn_3' or kampanjnamn = 'Kampanjnamn_4' ) (this is just an example on what it would contain as it is user selectable)

So I want to create an array of all elements with the class "focused" and get their data-attribute "kampanjnamn".

My best guess would be something like this inside a click function:

$('.focused').each(function () {

    var Myarray = [];
    Myarray = $(this).data('kampanjnamn');
}

this gives me the error "Myarray is not defined". If I put window. infront instead of var it send the variable as a single object and not an array...

So my first question maybe should have been: Is it even possible to send an array with .load?

2
Contributors
2
Replies
9
Views
4 Years
Discussion Span
Last Post by adishardis
1

To add to an array use .push

Also, you have scoped the variable inside the function so it's not available outside if that's where you need it. Just place the var Myarray = [] outside in that case.

0

Nice, thank you!

This was the last part to get the where clause to work

$whereClauses = explode(",", $_GET["jmfkampanjer"]);
$where = '';
if (count($whereClauses) > 0) {
    $where = 'WHERE kampanj > t.kampanj and (kampanjnamn = "'.implode('" or kampanjnamn = "',$whereClauses).'")';
}                       

Cheers!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.