Sql meerdere results verweven
Ik heb nu een query waarmee ik dmv een foreach de resultaten ophaal. De query voor de advertenties is daarentegen nogal anders t.o.v. de eerste. Anders kon ik deze misschien samenvoegen.
Hoe verweef ik nu de query voor de advertenties met de foreach loop?
De beide query's samenvoegen in een union. Even smart het aantal velden gelijk maken.
Nu vraag je ik wil van A naar B, zonder te laten weten wat A en B inhouden!!!
Event query:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$sql_get_events = "SELECT v.name,v.venue_id, v.city,v.street, v.number,v.zipcode,u.username,u.org_name,u.user_id,
e.startdate,e.title,e.description,e.starttime, e.endtime,e.event_id, GROUP_CONCAT(cat.category_descr SEPARATOR ', ')as cat_descr, GROUP_CONCAT(ec.category_id) as cat_id,
GetDistance(('" . mysqli_real_escape_string($con,$_POST['lat']) . "'), ('" . mysqli_real_escape_string($con,$_POST['lon']) . "'), zc.lat, zc.lon) AS dist
FROM venue AS v
JOIN event AS e USING (venue_id)
JOIN zipcode AS zc ON v.zipcode = zc.zipcode
JOIN users AS u ON e.user_id = u.user_id
JOIN event_cat AS ec USING (event_id)
JOIN category as cat USING (category_id)
WHERE
ec.category_id IN ('".implode("','",$_POST['category']) . "')
AND
e.startdate >= ('" . mysqli_real_escape_string($con,date("Y-m-d", strtotime($_POST['start_submit']))) . "')
AND
v.city <> ('" . mysqli_real_escape_string($con,$_SESSION['city']) . "')
GROUP BY
e.event_id
ORDER BY
dist,e.startdate,e.starttime LIMIT $from,$num";
e.startdate,e.title,e.description,e.starttime, e.endtime,e.event_id, GROUP_CONCAT(cat.category_descr SEPARATOR ', ')as cat_descr, GROUP_CONCAT(ec.category_id) as cat_id,
GetDistance(('" . mysqli_real_escape_string($con,$_POST['lat']) . "'), ('" . mysqli_real_escape_string($con,$_POST['lon']) . "'), zc.lat, zc.lon) AS dist
FROM venue AS v
JOIN event AS e USING (venue_id)
JOIN zipcode AS zc ON v.zipcode = zc.zipcode
JOIN users AS u ON e.user_id = u.user_id
JOIN event_cat AS ec USING (event_id)
JOIN category as cat USING (category_id)
WHERE
ec.category_id IN ('".implode("','",$_POST['category']) . "')
AND
e.startdate >= ('" . mysqli_real_escape_string($con,date("Y-m-d", strtotime($_POST['start_submit']))) . "')
AND
v.city <> ('" . mysqli_real_escape_string($con,$_SESSION['city']) . "')
GROUP BY
e.event_id
ORDER BY
dist,e.startdate,e.starttime LIMIT $from,$num";
Advertentie query:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
$sql_get_ads = "
SELECT
l.image,l.zipcode,
GetDistance(('" . mysqli_real_escape_string($con,$_POST['lat']) . "'), ('" . mysqli_real_escape_string($con,$_POST['lon']) . "'), zc.lat, zc.lon) AS dist
FROM local_ads AS l
JOIN zipcode AS zc ON l.zipcode = zc.zipcode
WHERE
GetDistance(('" . mysqli_real_escape_string($con,$_POST['lat']) . "'), ('" . mysqli_real_escape_string($con,$_POST['lon']) . "'), zc.lat, zc.lon) <= ('30')
AND CURDATE() > l.local_ad_start_date
AND CURDATE() < l.local_ad_end_date
ORDER BY l.local_ad_start_date";
SELECT
l.image,l.zipcode,
GetDistance(('" . mysqli_real_escape_string($con,$_POST['lat']) . "'), ('" . mysqli_real_escape_string($con,$_POST['lon']) . "'), zc.lat, zc.lon) AS dist
FROM local_ads AS l
JOIN zipcode AS zc ON l.zipcode = zc.zipcode
WHERE
GetDistance(('" . mysqli_real_escape_string($con,$_POST['lat']) . "'), ('" . mysqli_real_escape_string($con,$_POST['lon']) . "'), zc.lat, zc.lon) <= ('30')
AND CURDATE() > l.local_ad_start_date
AND CURDATE() < l.local_ad_end_date
ORDER BY l.local_ad_start_date";
Toevoeging op 14/08/2013 09:58:01:
Voordat sommige mensen moeite gaan doen.
Heb wellicht een oplossing d.m.v. javascript gevonden.
Ik ga dit eerst maar eens uitproberen.
http://jsfiddle.net/mattball/9M868/
Code (php)
Heb het je gelijk in PHP opgelost;
thanx Ger, die ga ik ook even bekijken