SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Php or SQL

  1. #1
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php or SQL

    Quick question. I am searching the forums as well. But I am trying to output onto a webpage all the categories and then the titles that belong to the category. But when I do the query I come up with a while statement in a while statement to get the results I want. Im sure Im doing this wrong. Trying to figure out if its db setup or just lack of php knowledge.
    Basically a quick example is movie theatres. Each theatre has a id, based on that id I want to grab all the movies then all the times for that movie. Right now if I would do it I would find all the movies belonging to that theatre, then in a seperate while statement I would find all times based on that movie found.
    So:

    PHP Code:
    $sql "SELECT movieid FROM table WHERE theatre = 1";
    $result mysql_query($sql);
    while (
    $this mysql_fetch_row($result)) {
    $sql2 "SELECT times FROM table2 WHERE movieid = $this[0]";
    $result2 mysql_query($sql2);
    while (
    $this2 mysql_fetch_row($result2)) {
    //then echo out the movies then the showtimes beneathe the movies
    }


    Sorry if the code above is crude. Just a quick example. Now perhaps my table structure is wrong or there is some php code I should be using or even if my sql statement lacks.
    Silly

  2. #2
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sql "SELECT table.movieid,table.name,table2.times FROM table,table2 WHERE table.theatre=1 AND table.movieid=table2.movieid"
    if ($result = @mysql_query($sql))
    {
     
    $movie_results = array();
     while ((
    $movie mysql_fetch_array($result)) and ($movie_results[$movie['name']][] = $movie['times']));
     if (!empty(
    $movie_results))
     {
      foreach (
    $movie_results as $name => $times)
      {
       echo 
    $name '<hr />';
       foreach (
    $times as $time)
        echo 
    $time '<br />';
      }
     }

    That's about the best thing I can think of right now... lol. You could do 2 while loops on it, or something else... up to you

    (There might be an easier way, i'm too lazy to think more than that.)
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •