Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6602069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:49:59+00:00 2026-05-25T18:49:59+00:00

I’m querying against a database to retrieve some information that includes a one to

  • 0

I’m querying against a database to retrieve some information that includes a one to many relationship. Consider:

CREATE TABLE `movies` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `title` varchar(50) NOT NULL,
  `desc` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
INSERT INTO `movies` VALUES ('1', 'The Princess Bride', 'A fantastic film of epic adventure, action, and love.');


CREATE TABLE `showtimes` (
  `movie_id` int(10) unsigned NOT NULL,
  `showtime_id` int(10) unsigned NOT NULL auto_increment,
  `starttime` timestamp NOT NULL,
  PRIMARY KEY  (`movie_id`,`showtime_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `showtimes` VALUES ('1', '1', '2011-09-19 20:00:00'), ('1', '2', '2011-09-19 23:00:00'), ('1', '3', '2011-09-20 13:00:00');

The way I’d like to receive the information is something like this.

$movies[1] = array(
    'id' => 1, 
    'title' => 'The Princess Bride', 
    'desc' => 'A fantastic film of epic adventure, action, and love.', 
    'showtimes' => array(
        '1' => '2011-09-19 20:00:00', 
        '2' => '2011-09-19 23:00:00', 
        '3' => '2011-09-20 13:00:00'));

This seems to be the most sensible way for me to go through the data. If I’m printing all the showtimes for the theatre, I can do something simple like:

foreach($movies as $movie)
{
    //pretend there's style stuff here
    echo $movie['title'] . "&mdash" . $movie['desc'];
    foreach($movie['showtime'] as $time)
    {
        if ($time > $_SERVER['REQUEST_TIME'])
        {
            echo $time;
        }
    }
}

Unfortunately, that’s not what I get back from a standard query. Something like:

SELECT * FROM `movies` INNER JOIN `showtimes` ON `movies`.`id` = `showtimes`.`movie_id`;

Yields:

$movies[1] = array('id' => 1, 'title' => 'The Princess Bride', 'desc' => 'A fantastic film of epic adventure, action, and love.', 'starttime' => '2011-09-19 20:00:00');
$movies[2] = array('id' => 1, 'title' => 'The Princess Bride', 'desc' => 'A fantastic film of epic adventure, action, and love.', 'starttime' => '2011-09-19 23:00:00');
$movies[3] = array('id' => 1, 'title' => 'The Princess Bride', 'desc' => 'A fantastic film of epic adventure, action, and love.', 'starttime' => '2011-09-20 13:00:00');

which isn’t quite what I’m going for. In larger result sets with more data I’m also mildly curious about the effect of returning so much duplicated data (consider wider rows with tens of joins).

I know I can use a construct like GROUP_CONCAT() to append those showtimes together, but I’m left splitting them apart later. For simple things like timestamps it’s easy as I can choose a delimiter that wont appear in that format, if I was splitting reviews for example it would be a bit tougher.

I can do something lame like iterate over all the movies, querying showtimes from within that loop, but there’s no way that will ever be web scale.

I can execute the join in-query, then iterate over those results and append on duplicate primary key, but that seems to lack elegance as well.

The Question

Is there an elegant way to get what I’m looking for? On this particular application I’m using Zend Framework, if it’s built into that (or another framework) that would be pretty keen.

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:49:59+00:00Added an answer on May 25, 2026 at 6:49 pm

    Do some kind of join (your choice) on the two tables and then loop through the results.

    $query = "SELECT * FROM movies LEFT JOIN showtimes ON showtimes.movie_id = movies.id";
    $result = $db->query($query);
    while($row = $result->fetch()){
      if(isset($movies[$row['id']])
        $movies[$row['id']]['showtimes'][] = $row['starttime'];
      else{
        $movies[$row['id']] = array(
          'id'=>$row['id'],
          'title'=>$row['title'],
          'desc'=>$row['desc'],
          'showtimes'=>array($row['starttime'])
        );
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.