Jump to content

mySQL group by value depending on another value


2260419
Go to solution Solved by Barand,

Recommended Posts

I have some videos I upload to server for online viewing. Sometimes they are too long and I have to cut them into pieces. I am a beginner in this and I'm a little lost.  :wacko:

the idea is this:

I have these table in MySQL:

k7ro.png

and this is my php code I have so far:

$sql="SELECT * FROM table_name WHERE type = 'tutorials' and type2='word' and video_id='01' ORDER BY server ASC,format ASC,resolution ASC";

$result = mysql_query($sql);

<table>
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
        switch ($row['server'])
        {
        case "youtube":
          echo '<td><a href="';
          echo $row['short_link'].'"><image youtube/></a></td>';
          break;
        case "vimeo":
          echo '<td><a href="';
          echo $row['short_link'].'"><image vimeo/></a></td>';
          break;
        case "dailymotion":
          echo '<td><a href="';
          echo $row['short_link'].'"><image dailymotion/></a></td>';
          break;
        default:
           echo '<td><a href="';
               echo $row['short_link'].'"></a></td>';
        }
  echo "<td>" . $row['server'] . "</td>";
  echo "<td>" . $row['format'] . "</td>";
  echo "<td>" . $row['resolution'] . "</td>";
  echo "<td>" . $row['size'] . "</td>";
  echo "<td>" . $row['user'] . "</td>";
  echo "</tr>";
  }
echo "</table>"; 

but I need that if the server has parts, put all in a div and appears like this:
w1pq.png

sometimes the video has more parts, I searched for "group by" but I don't know how to integrate it.

 

How I can do that or What do I need search in google to find examples?

Link to comment
Share on other sites

You'll want to do this grouping from within PHP, not as part of the query using GROUP BY. Create an array to hold the results and store the results in a multi-dimensional structure with the first level being the main video information, and a second level for the parts listing. Eg:

$results = array();
while ($row = mysql_fetch_array($result)){
   if (!isset($results[$row['server']]){
       $row['videos'] = array($row['short_link']);
       $results[$row['server']] = $row;
   }
   else {
       $results[$row['server']]['videos'][] = $row['short_link'];
   }
}

var_dump($results);
Then you'd output the results using a foreach loop over the $results array. For each row in the results array, the key 'videos' would be another array of all the videos for that server.
Link to comment
Share on other sites

  • Solution

You could use a query like this

SELECT user
, GROUP_CONCAT(short_link) as short_links
, server
, format
, SUM(size) as size
, resolution
FROM table_name 
WHERE type = 'tutorials' and type2='word' and video_id='01'
GROUP BY user, server

However, your sizes need to be stored as INT (eg value 100) and not as VARCHAR (eg value 100MB)

 

When processing, explode the "short_links" field.

Link to comment
Share on other sites

You'll want to do this grouping from within PHP, not as part of the query using GROUP BY. Create an array to hold the results and store the results in a multi-dimensional structure with the first level being the main video information, and a second level for the parts listing. Eg:

$results = array();
while ($row = mysql_fetch_array($result)){
   if (!isset($results[$row['server']]){
       $row['videos'] = array($row['short_link']);
       $results[$row['server']] = $row;
   }
   else {
       $results[$row['server']]['videos'][] = $row['short_link'];
   }
}

var_dump($results);
Then you'd output the results using a foreach loop over the $results array. For each row in the results array, the key 'videos' would be another array of all the videos for that server.

 

 

Thanks Kicken, but the truth, I try to understand the code, but I ended losing me more.  :wacko:

I try if watching the array could accommodate it, but could not. :mellow:

 

 

You could use a query like this

SELECT user
, GROUP_CONCAT(short_link) as short_links
, server
, format
, SUM(size) as size
, resolution
FROM table_name 
WHERE type = 'tutorials' and type2='word' and video_id='01'
GROUP BY user, server

However, your sizes need to be stored as INT (eg value 100) and not as VARCHAR (eg value 100MB)

 

When processing, explode the "short_links" field.

 

it was easier with this option. I could do it the first time.  :lol:

 

 

thanks guys. you are amazing.  O0

Link to comment
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.