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 8649503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:34:40+00:00 2026-06-12T13:34:40+00:00

I think that my code can be easier, but I’m not sure. Look and

  • 0

I think that my code can be easier, but I’m not sure. Look and tell me please some alternative if you have. This code i using to show informations about movies

$sql='SELECT DISTINCT id,title,img,description,adder,added,
GROUP_CONCAT(DISTINCT cid,"-",caty ) AS caty,
GROUP_CONCAT(DISTINCT oid,"-",obs,"-",face,"-",rola,"-",typ) AS obs
FROM film

LEFT JOIN f_o ON f_o.f_id = film.id
LEFT JOIN obs ON f_o.o_id = obs.oid

WHERE film.id ='.$fid;

$wynik=mysql_fetch_assoc(mysql_query($sql));
if(isset($wynik['id'])){
echo '<pre>';
print_r($wynik);
echo '</pre>';
////
$array  = explode(',', $wynik['obs']);

$r=array();//director - 0
$s=array();//Screenwriter - 1
$ak=array();//actors - 2
$akn=array();//actors 2 plan - 3
$np=array();//From Idea By - 4
$p=array();//producers - 5
$m=array();//music - 6

foreach ($array as $item)
{
    $a = explode('-', $item);
    if( $a[4] == 0 )
    {
        $r[] = $a[0].','.$a[1].','.$a[2].','.$a[3];
    }
    elseif($a[4] == 1 )
    {
        $s[] = $a[0].','.$a[1].','.$a[2].','.$a[3];
    }
    elseif($a[4] == 2 )
    {
        $ak[] = $a[0].','.$a[1].','.$a[2].','.$a[3];
    }
    elseif($a[4] == 3 )
    {
        $akn[] = $a[0].','.$a[1].','.$a[2].','.$a[3];
    }
    elseif($a[4] == 4 )
    {
        $np[] = $a[0].','.$a[1].','.$a[2].','.$a[3];
    }
    elseif($a[4] == 5 )
    {
        $p[] = $a[0].','.$a[1].','.$a[2].','.$a[3];
    }
    elseif($a[4] == 6 )
    {
        $m[] = $a[0].','.$a[1].','.$a[2].','.$a[3];
    }
}

function dzielperson($data){    
    $i = 0;
    $ile=count($data);
    while ($i < $ile) {
        $a  = explode(",", $data[$i]);
        $caty='<a href="/person/'.dolink($a[1]).'-'.$a[0].'" class="link1">'.$a[1].'</a>'.($i==($ile-1) ? '':', ');
        $i++;
    }
    return $caty;
}
echo '<br>Title: '.$wynik[title];
echo '<br>Desription: '.$wynik[description];
echo '<br>directors: '.dzielperson($r);
echo '<br>screenwriters: '.dzielperson($s);
echo '<br>actors: '.dzielperson($ak);
echo '<br>actors 2 plan: '.dzielperson($akn);
echo '<br>From Idea By '.dzielperson($np);
echo '<br>Producers: '.dzielperson($p);
echo '<br>Music: '.dzielperson($m);

}

Here is mysql output:

Array
(
    [id] => 1
    [title] => Pirates
    [img] => /images/1page_img1.jpg
    [description] => 
    [adder] => baambaam
    [added] => 1349387322
    [obs] => 1-aktor1-foto.jpg-shrek-3,2-aktor2-foto2.jpg-batman-0,3-aktor3-f1.png-Pirat-1,4-aktorzyna4-f2.png-Pirat 3-1
)

Tables structure:

film:id,title img,description,adder,added
obs:oid,obs,face,rola,typ
f_o:f_id,o_id

in column obs i have names of actors,directors….

It’s not completly code but i wish that you understand

  • 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-06-12T13:34:41+00:00Added an answer on June 12, 2026 at 1:34 pm
    $sql='SELECT DISTINCT id,title,img,description,adder,added
    FROM film
    WHERE film.id ='.$fid;
    $wynik=mysql_fetch_assoc(mysql_query($sql));
    if(isset($wynik['id'])){
    
    // you should use constants instead of just if(type==_some_meaningless_number_):
    $personTypeMap = array(
        'r'    //director - 0
        ,'s'   //Screenwriter - 1
        ,'ak'  //actors - 2
        ,'akn' //actors 2 plan - 3
        ,'np'  //From Idea By - 4
        ,'p'   //producers - 5
        ,'m'   //music - 6
    );
    // so above should be something like:
    // define('PERSON_TYPE_DIRECTOR', 0);
    // define...
    // then you wouldn't need that array-map above as well as would be easier to understand who is what
    
    // init all people subarrays:
    $people = array_fill_keys($personTypeMap, array());
    
    $sql='SELECT oid, obs, typ   #add other fields if you actually use them
    FROM f_o
    INNER JOIN obs ON f_o.o_id = obs.oid
    WHERE f_o.f_id ='.$fid;
    $peopleResult = mysql_query($sql);
    while ($person=mysql_fetch_object($peopleResult)) {
        $people[$personTypeMap[$person->typ]][] = dzielperson($person);
    }
    
    function dzielperson($person){    
       return '<a href="/person/'.dolink($person->obs)."-{$person->oid}\" class=\"link1\">{$person->obs}</a>";
    }
    
    // join people in all categories through comma:
    foreach ($people as &$category) {
        $category = implode(', ', $category);
    }
    
    echo '<br>Title: '.$wynik['title'];
    echo '<br>Desription: '.$wynik['description'];
    echo '<br>directors: '.$people['r'];
    echo '<br>screenwriters: '.$people['s'];
    echo '<br>actors: '.$people['ak'];
    echo '<br>actors 2 plan: '.$people['akn'];
    echo '<br>From Idea By '.$people['np'];
    echo '<br>Producers: '.$people['p'];
    echo '<br>Music: '.$people['m'];
    

    P.S. I’m fixing your code for you for one reason: your original made me laugh for 10 minutes non-stop 🙂 Thank you.

    P.P.S. I left some of the original mess behind, but take it as an opportunity to learn what was wrong with your code and try to simplify that yourself.

    P.P.P.S. Yes, multiple queries in this case is better than single monster collecting unrelated stuff in single row.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i know that there isn't code inside my question, but i think this is
I think that looking at others' code is a good way to learn. I'm
The code below shows me (I think) that the for each loop is about
Here is a simple code that shows what I think is a bug when
I'm think something like Facebook apps here. User generated pieces of code that people
So i have been creating this framework thing that basically puts together source code
I have a cool snippet of code that works well, except one thing. The
Say you have an API that is not accessible to change: List<LegacyObject> getImportantThingFromDatabase(Criteria c);
Whilst learning events and delegates I can't help but think about the Observer design
I need to write some code in PHP that performs an action only on

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.