Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Feb 2013
    Posts
    32
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Actor ID and Name

    I want to create link to every actor. But with code above i get only one actor ( Bradley Cooper ). Movie have five actors ( Bradley Cooper Ed Helms Zach Galifianakis Justin Bartha Heather Graham ).
    PHP Code:
    function PageMain()
    {
        global 
    $TMPL;

        
    $query         "...";
        
    $result     mysql_query($query);
        
    $TMPL_old     $TMPL;
        
    $TMPL         = array();
        
    $template     = new template('lists/movie');
        
    $all         '';
        while(
    $row mysql_fetch_assoc($result))
        {
            
    $TMPL['id']     = $row['id'];
            
    $TMPL['title']     = $row['title'];
            
    $TMPL['year']     = $row['year'];
            
            
    $actors explode","$row["actors"] );
            
    $actorids explode","$row["actorids"] );
            for ( 
    $a 0$a count($actors); ++$a )
            {
                
    $actor $actors[$a];
                
    $id $actorids[$a];
                
    $TMPL['movieactors'] = '<a href="/actor/'.$id.'">'.$actor.'</a>';
            }
            
            
    $all .= $template->make();
        }
        
        
    $TMPL                     $TMPL_old; unset($TMPL_old);
        
    $TMPL['movies']         = $all;

    Old one. With this i get all actors.
    PHP Code:
    $TMPL['actor'] = '';
    $actors array_map('trim'explode(','$row['actors']));
    foreach (
    $actors as $v)
    {
        
    $TMPL['actor'] .= ' <a href="/actor/'.urlencode($v).'">'.$v.'</a>,';
    }
    $TMPL['actor'] = trim($TMPL['actor'], ',');
    $TMPL['movieactors'] = $TMPL['actor']; 

    Ok i make it. But i want to ask this code is right?

    PHP Code:
                    $TMPL['movieactors']             = array();
                    
    $actors explode","$row["actors"] );
                    
    $actorids explode","$row["actorids"] );
                    for ( 
    $a 0$a count($actors); ++$a )
                    {
                        
    $actor $actors[$a];
                        
    $id $actorids[$a];
                        
    $TMPL['movieactors'][] = '<a href="/actor/nm'.$id.'">'.$actor.'</a>';
                    }
                    
    $TMPL['movieactors'] = join(', '$TMPL['movieactors']); 
    Last edited by destas; 03-25-2013 at 03:44 AM.
    Sorry for my poor english.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    If it works, than you can do that.
    This leads question to your database design though:
    PHP Code:
            $actors explode","$row["actors"] );
            
    $actorids explode","$row["actorids"] ); 
    Since we cannot see the query used, unless you've designed that to work off of mysql's GROUP_CONCAT function, than that would be a normalization error with your dbms. Given the purpose of the program, I'd suggest that if it is a normalization issue that it should instead be normalized. Without it, finding all movies starring two actors together for example would be a very long and painful query (and a trivial join with normalized records).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    your assuming the actor id's are sequential,

    change to this:
    PHP Code:
            $actors explode","$row["actors"] );
            
    $actorids explode","$row["actorids"] );
            foreach ( 
    $actors as $a => $v){
                
    $id $actorids[$a];
                
    $TMPL['movieactors'] = '<a href="/actor/'.$id.'">'.$v.'</a>';
            } 
    but yes you should have a table for them

    if this were a table called actors then

    ActorID | ActorName | MovieID

    PHP Code:
    $sql "Select * from actors where MovieID = $row['id']";
    $actorlist mysql_query($sql); 

    while(
    $actorrow mysql_fetch_assoc($actorlist )) {

        
    $TMPL['movieactors'] = '<a href="/actor/'.$actorrow['ActorID '].'">'.$actorrow['ActorName '].'</a>';


    Last edited by Arcticwarrio; 03-25-2013 at 09:13 PM. Reason: but yes you should have a table for them if this were a table called actors then ActorID | ActorName | MovieID
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #4
    New Coder
    Join Date
    Feb 2013
    Posts
    32
    Thanks
    10
    Thanked 0 Times in 0 Posts
    My SQL :

    movies table...
    PHP Code:
    idtitleyear.. 
    actors table
    PHP Code:
    `actor_id`, `actor_name
    movieactors table
    PHP Code:
    `movie_id`, `actor_id
    I think with this table i will need to write two times same actor name? Did my mysql tables wrong?
    PHP Code:
    ActorID ActorName MovieID 
    I will give you my query to understand everything
    PHP Code:
    SELECT m.idm.titlem.year
    GROUP_CONCAT(DISTINCT g.genre_name ORDER BY g.genre_name) AS genres
    GROUP_CONCAT(DISTINCT a.actor_name ORDER BY a.actor_name) AS actors,  
    GROUP_CONCAT(DISTINCT a.actor_id ORDER BY a.actor_name) AS actorids 
    FROM movies m 
    INNER JOIN moviegenres mg ON 
    (mg.movie_id m.id
    INNER JOIN genres g ON (g.genre_id mg.genre_id
    INNER JOIN movieactors ma ON (ma.movie_id m.id
    INNER JOIN actors a ON (a.actor_id ma.actor_id
    WHERE `year` = '%s' 
    GROUP BY m.idm.titlem.year
    ORDER BY m
    .title
    DESC LIMIT 
    %d, %
    With this code i get only one actor name for every movie..
    Quote Originally Posted by Arcticwarrio View Post
    your assuming the actor id's are sequential,

    change to this:
    PHP Code:
            $actors explode","$row["actors"] );
            
    $actorids explode","$row["actorids"] );
            foreach ( 
    $actors as $a => $v){
                
    $id $actorids[$a];
                
    $TMPL['movieactors'] = '<a href="/actor/'.$id.'">'.$v.'</a>';
            } 
    Same like my code without
    PHP Code:
    $TMPL['movieactors'] = join(', '$TMPL['movieactors']); 
    Last edited by destas; 03-26-2013 at 12:46 PM.
    Sorry for my poor english.

  • #5
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    no i didnt realise you had an actors table

    in your movieactors table
    you would have multiple rows for 1 movie

    PHP Code:
    movie_id actor_id
    1
    2

    Last edited by Arcticwarrio; 03-26-2013 at 01:42 PM.
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Okay this design is fine.
    There are two things you could do. The first is to keep what query you have, and use the information between the actorids and actors fields together. Since these should be in the proper order and equate to a 1 to 1 relationship, this can be joined for key value:
    PHP Code:
    $aActors array_combine($row['actorids'], $row['actors']);
    foerach ($aActors AS $id => $v)
    {
        
    $TMPL['movieactors'] = '<a href="/actor/'.$id.'">'.$v.'</a>'

    The alternative is to not use the group_concat, and just pull all the records for the actor and loop that resultset. Personally I'd do that, but you can choose whichever.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    destas (03-26-2013)

  • #7
    New Coder
    Join Date
    Feb 2013
    Posts
    32
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    PHP Code:
    $aActors array_combine($row['actorids'], $row['actors']);
    foerach ($aActors AS $id => $v)
    {
        
    $TMPL['movieactors'] = '<a href="/actor/'.$id.'">'.$v.'</a>'

    I don't understand what i must change in my query to work with this code..

    PHP Code:
    GROUP_CONCAT(DISTINCT a.actor_name ORDER BY a.actor_name) AS actors,  
    GROUP_CONCAT(DISTINCT a.actor_id ORDER BY a.actor_name) AS actorids 
    Change it with: ?
    PHP Code:
    inner join (select from actors order by actor_name ) AS actors 
    Sorry for my poor english.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Oh sorry no, you'll need to explode them first, the one I have is just the properties, but that won't work since they are strings and not arrays. The idea was to not have to rewrite the existing query, although if you want to do that then you can. It changes how you handle your data since you get records of actors and the movies their related to instead of movies with a single property of the actors involved.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #9
    New Coder
    Join Date
    Feb 2013
    Posts
    32
    Thanks
    10
    Thanked 0 Times in 0 Posts
    I have one more question. If I click on
    PHP Code:
    http://localhost/actor/107 
    107 is actor ID and on this page i want to get actor name of this ID.

    Now show for me:
    PHP Code:
    Movies where actor 107 was acted
    Thats how i get actor ID.
    PHP Code:
    $currentactorname $_GET['q']; 
    Thats how i print his ID.
    PHP Code:
    $TMPL['actor'] = $currentactorname
    And finally my question: Do i need query to do this?
    Sorry for my poor english.

  • #10
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    you can explode the address by "/" thn get the last occurance?
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #11
    New Coder
    Join Date
    Feb 2013
    Posts
    32
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Did you know what this code does?

    $_GET['q'];
    Sorry for my poor english.

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    $_GET['q'] would mean that you have a script.php with a ?q=107. This address itself is invalid: http://localhost/actor/107 , but rewrite I assumed was in use to write that to actor.php?q=107.
    To query for all movies that actor id 107 has acted in, you would query as:
    Code:
    SELECT m.id, m.title, m.year, a.actor_name
    FROM movie m
    INNER JOIN movieActors ma ON ma.movie_id = m.id
    INNER JOIN actor a ON a.actor_id = ma.actor_id
    WHERE a.actor_id = $currentactorname
    I don't like the name of that variable, I would call it $currentactorid since its a number and not a name.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    destas (03-27-2013)

  • #13
    New Coder
    Join Date
    Feb 2013
    Posts
    32
    Thanks
    10
    Thanked 0 Times in 0 Posts
    yes i have script ohh i forget to change &currentactorname to &currentactorid. I use &currentactorname in my previous code. I was join the actor using their names (localhost/actor/john+doe) but i have problem with russian actors.
    Sorry for my poor english.

  • #14
    New Coder
    Join Date
    Feb 2013
    Posts
    32
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Code doesn't show genre_name if I have genre_name in genres table but i don't have movie with this genre. How to fix that?
    PHP Code:
    SELECT m.idg.genre_name 
    FROM movies m 
    INNER JOIN moviegenres mg ON 
    (mg.movie_id m.id
    INNER JOIN genres g ON (g.genre_id mg.genre_id
    WHERE g.genre_id '$currentgenreid' 
    Last edited by destas; 03-27-2013 at 09:02 PM.
    Sorry for my poor english.


  •  

    Posting Permissions

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