Jump to content

Recommended Posts

i got 2 table (please see attached files)

  1. table trainingen
  2. table oefeningen (dutch for excercices)

 

I'm trying to show on a page:

 

training 1

deadlift : 10, 8, 6

squat : 5, 5, 3

 

that why i try to query the results like this:

 

Array
(
[training_id] => 1
[training_naam] => TRAINING 1
[excercices] => Array
	(
		[deadlift] => 10, 8, 6
		[squat] => 5,  5, 5
	)

)

OR

Array
(
[training_id] => 1
[training_naam] => TRAINING 1
[excercices] => Array
	(
		[deadlift] => Array
			(
				[0] => 10
				[1] => 8
				[2] => 6
			)
		[squat] => Array
			(
				[0] => 5
				[1] => 5
				[2] => 5
			)
	)
)

 

and a another array with the same structure for another training:

 

I only managed to get something like this:

 

$sql = "SELECT t.training_id, t.naam AS training_naam, o.naam, GROUP_CONCAT(reps) AS excercices FROM trainingen t
inner JOIN oefeningen o ON t.training_id = o.training_id
WHERE user_id = $id";

 

which gives:

 

Array
(
[training_id] => 1
[training_naam] => TRAINING 1
[naam] => deadlift
[excercices] => 10,8,6,12,12,10,10,6,5,5,10,3,10,10,5,10,8,6,10,8,6,10,8,6,10,6,4
)

 

Can somebody help me out the right query? thanks!

trainingen.pdf

oefeningen.pdf

Link to comment
https://forums.phpfreaks.com/topic/274267-query-help/
Share on other sites

thanks for the GROUP BY advice. i looked a bit further to that statement what it does and it seems logic that now i get arrays where the reps are comma-seperated and grouped for each combination of a training name and excercice name. the thing is that i now have a but duplicate values in my result set arrays, like this:

 

Array
(
   [training_id] => 1
   [training_naam] => TRAINING 1
   [naam] => deadlift
   [excercices] => 10,8,6
)
Array
(
   [training_id] => 1
   [training_naam] => TRAINING 1
   [naam] => squat
   [excercices] => 12,12,10,10,6,5,5
)

 

It would be nicer if it where possible to make the excercice another layer deeper with the name of the excercice and the reps as values, like:

 

Array
(
    [training_id] => 1
    [training_naam] => TRAINING 1
    [excercices] => Array
		    (
				    [deadlift] => 10, 8, 6
				    [squat] => 5,  5, 5
		    )
)

 

Is that possible, or do i better do this with php after having retrieved all the different record sets? thanks already for the help

Link to comment
https://forums.phpfreaks.com/topic/274267-query-help/#findComment-1411363
Share on other sites

probably a messy solution, but ill share it if someone might find it usefull:

 

<?php

$id = $_SESSION['user_id'];

$sql = "SELECT training_id, naam FROM trainingen WHERE user_id = $id";
$rs = mysql_query($sql);


while ($row = mysql_fetch_assoc($rs)){

 $t_id = $row['training_id'];
 $trainingen[$row['training_id']]['t_naam'] = $row['naam'];

 $sql2 = "SELECT o.naam AS o_naam, GROUP_CONCAT(reps) AS reps FROM oefeningen o WHERE training_id = $t_id GROUP BY training_id, o.naam";
 $rs2 = mysql_query($sql2);

  while ($row2 = mysql_fetch_assoc($rs2)){
   $trainingen[$row['training_id']]['oefeningen'][$row2['o_naam']] = $row2['reps'];
  }
}
?>

<?php
$content = '<ul>';
foreach ($trainingen as $training_id => $arr){

 $t_naam = $trainingen[$training_id]['t_naam'];
 $content .= '<li>';
 $content .= "$t_naam";
 $content .= '</li>';

  foreach($trainingen[$training_id]['oefeningen'] as $o_naam => $reps){

   $content .= '<ul>';
   $content .= '<li>';
   $content .= "$o_naam" . "($reps)";
   $content .= '</li>';
   $content .= '</ul>';
  }

}
$content .= '<ul>';
echo $content;
?>

Link to comment
https://forums.phpfreaks.com/topic/274267-query-help/#findComment-1411374
Share on other sites

$sql = "SELECT t.training_id, t.naam AS training_naam, o.naam,
   GROUP_CONCAT(reps) AS excercices
FROM trainingen t
   INNER JOIN oefeningen o ON t.training_id = o.training_id
WHERE user_id = 1
GROUP BY t.naam,o.naam";

$res = $mysqli->query($sql);
$results = array();

while (list($tid, $tnm, $onm, $reps) = $res->fetch_row()) {
   if (!isset($results[$tid]))
    $results[$tid] = array('training_id'=>$tid, 'naam'=>$tnm, 'excercises'=>array());
   $results[$tid]['excercises'][$onm] = $reps;
}

echo '<pre>',print_r($results, true),'</pre>';

Link to comment
https://forums.phpfreaks.com/topic/274267-query-help/#findComment-1411378
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.