Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am working on a code to see if a video already exists. The problem is that I have to check multiple sections. The code is working but it should not be this big, I think, and it takes a long time to load.

Is there an easier way to do this?

$xmlurl7b = 'http://mysite.me/library/sections/2/all?title=' . $movie->title() . '&HndJk7Sd'; 
$xmlurl1b = 'http://mysite.me/library/sections/5/all?title=' . $movie->title() . '&HndJk7Sd'; 
$xmlurl2b = 'http://mysite.me/library/sections/7/all?title=' . $movie->title() . '&HndJk7Sd'; 
$xmlurl3b = 'http://mysite.me/library/sections/9/all?title=' . $movie->title() . '&HndJk7Sd'; 
$xmlurl4b = 'http://mysite.me/library/sections/10/all?title=' . $movie->title() . '&HndJk7Sd'; 
$xmlurl5b = 'http://mysite.me/library/sections/11/all?title=' . $movie->title() . '&HndJk7Sd'; 
$xmlurl6b = 'http://mysite.me/library/sections/13/all?title=' . $movie->title() . '&HndJk7Sd'; 
$xmlurl8 = 'http://mysite.me/library/sections/3/all?title=' . $movie->title() . '&HndJk7Sd'; 
$xmlurl9 = 'http://mysite.me/library/sections/8/all?title=' . $movie->title() . '&HndJk7Sd';
$xmlurl7 = 'http://mysite.me/library/sections/2/all?guid=movieid' . $movieid . '%3Flang%3Dnl&HndJk7Sd';
$xmlurl1 = 'http://mysite.me/library/sections/5/all?guid=movieid' . $movieid . '%3Flang%3Dnl&HndJk7Sd';
$xmlurl2 = 'http://mysite.me/library/sections/7/all?guid=movieid' . $movieid . '%3Flang%3Dnl&HndJk7Sd';
$xmlurl3 = 'http://mysite.me/library/sections/9/all?guid=movieid' . $movieid . '%3Flang%3Dnl&HndJk7Sd';
$xmlurl4 = 'http://mysite.me/library/sections/10/all?guid=movieid' . $movieid . '%3Flang%3Dnl&HndJk7Sd';
$xmlurl5 = 'http://mysite.me/library/sections/11/all?guid=movieid' . $movieid . '%3Flang%3Dnl&HndJk7Sd';
$xmlurl6 = 'http://mysite.me/library/sections/13/all?guid=movieid' . $movieid . '%3Flang%3Dnl&HndJk7Sd';
$achxml8 = simplexml_load_file($xmlurl8);
$achxml9 = simplexml_load_file($xmlurl9);
$achxml1 = simplexml_load_file($xmlurl1);
$achxml2 = simplexml_load_file($xmlurl2);
$achxml3 = simplexml_load_file($xmlurl3);
$achxml4 = simplexml_load_file($xmlurl4);
$achxml5 = simplexml_load_file($xmlurl5);
$achxml6 = simplexml_load_file($xmlurl6);
$achxml7 = simplexml_load_file($xmlurl7);
$achxml1b = simplexml_load_file($xmlurl1b);
$achxml2b = simplexml_load_file($xmlurl2b);
$achxml3b = simplexml_load_file($xmlurl3b);
$achxml4b = simplexml_load_file($xmlurl4b);
$achxml5b = simplexml_load_file($xmlurl5b);
$achxml6b = simplexml_load_file($xmlurl6b);
$achxml7b = simplexml_load_file($xmlurl7b);
if( $achxml1->Video && ($achxml1->Video != '') 
OR $achxml2->Video && ($achxml2->Video != '') 
OR $achxml3->Video && ($achxml3->Video != '') 
OR $achxml4->Video && ($achxml4->Video != '') 
OR $achxml5->Video && ($achxml5->Video != '') 
OR $achxml6->Video && ($achxml6->Video != '') 
OR $achxml7->Video && ($achxml7->Video != '')
OR $achxml8->Directory && ($achxml8->Directory != '')
OR $achxml9->Directory && ($achxml9->Directory != '')
OR $achxml1b->Video && ($achxml1b->Video != '') 
OR $achxml2b->Video && ($achxml2b->Video != '') 
OR $achxml3b->Video && ($achxml3b->Video != '') 
OR $achxml4b->Video && ($achxml4b->Video != '') 
OR $achxml5b->Video && ($achxml5b->Video != '') 
OR $achxml6b->Video && ($achxml6b->Video != '') 
OR $achxml7b->Video && ($achxml7b->Video != '')
 ) {
echo 'Exists';
} else {
echo 'New'; 
}
share|improve this question
1  
Thank you for posting your code. It smells. It can definitely use a Code Review! –  Mast Jun 4 at 12:06
2  
The code needs improvement, but it's hard to show you how to generalize the code since it's not obvious what it's doing, or why it's doing it. For example, where do those URLs come from? –  200_success Jun 4 at 12:09
    
If you're making requests to the server for specific sections, then logic would dictate that you should make a specific request for this type of thing. –  Neil Jun 4 at 12:14

1 Answer 1

up vote 0 down vote accepted

Try this approach:

$sources = [
    [
        'url'  => 'http://mysite.me/library/sections/2/all?title=' . $movie->title() . '&HndJk7Sd',
        'node' => 'Video'
    ],
    [
        'url'  => 'http://mysite.me/library/sections/5/all?title=' . $movie->title() . '&HndJk7Sd',
        'node' => 'Video'
    ],
    [
        'url'  => 'http://mysite.me/library/sections/7/all?title=' . $movie->title() . '&HndJk7Sd',
        'node' => 'Video'
    ],
    [
        'url'  => 'http://mysite.me/library/sections/9/all?title=' . $movie->title() . '&HndJk7Sd',
        'node' => 'Video'
    ],
    [
        'url'  => 'http://mysite.me/library/sections/10/all?title=' . $movie->title() . '&HndJk7Sd',
        'node' => 'Video'
    ],
    [
        'url'  => 'http://mysite.me/library/sections/11/all?title=' . $movie->title() . '&HndJk7Sd',
        'node' => 'Video'
    ],
    [
        'url'  => 'http://mysite.me/library/sections/13/all?title=' . $movie->title() . '&HndJk7Sd',
        'node' => 'Video'
    ],
    [
        'url'  => 'http://mysite.me/library/sections/3/all?title=' . $movie->title() . '&HndJk7Sd',
        'node' => 'Directory'
    ],
    [
        'url'  => 'http://mysite.me/library/sections/8/all?title=' . $movie->title() . '&HndJk7Sd',
        'node' => 'Directory'
    ],
    [
        'url'  => 'http://mysite.me/library/sections/2/all?guid=movieid' . $movieid . '%3Flang%3Dnl&HndJk7Sd',
        'node' => 'Video'
    ],
    [
        'url'  => 'http://mysite.me/library/sections/5/all?guid=movieid' . $movieid . '%3Flang%3Dnl&HndJk7Sd',
        'node' => 'Video'
    ],
    [
        'url'  => 'http://mysite.me/library/sections/7/all?guid=movieid' . $movieid . '%3Flang%3Dnl&HndJk7Sd',
        'node' => 'Video'
    ],
    [
        'url'  => 'http://mysite.me/library/sections/9/all?guid=movieid' . $movieid . '%3Flang%3Dnl&HndJk7Sd',
        'node' => 'Video'
    ],
    [
        'url'  => 'http://mysite.me/library/sections/10/all?guid=movieid' . $movieid . '%3Flang%3Dnl&HndJk7Sd',
        'node' => 'Video'
    ],
    [
        'url'  => 'http://mysite.me/library/sections/11/all?guid=movieid' . $movieid . '%3Flang%3Dnl&HndJk7Sd',
        'node' => 'Video'
    ],
    [
        'url'  => 'http://mysite.me/library/sections/13/all?guid=movieid' . $movieid . '%3Flang%3Dnl&HndJk7Sd',
        'node' => 'Video'
    ]
];

$exists = false;
foreach ($sources as $source) {
    $xml = simplexml_load_file($source['url']);

    if (isset($xml->{$source['node']}) && $xml->{$source['node']} != '') {
        $exists = true;
        break;
    }
}

if ($exists) {
    echo 'Exists';
} else {
    echo 'New';
}
share|improve this answer
    
That worked thanks, only 1 less ) at the end of if (isset($xml->{$source['node']}) && $xml->{$source['node']} != '')) –  MrKramer Jun 4 at 19:09
    
Oops. Apologies! Corrected. –  Nikolaos Dimopoulos Jun 4 at 21:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.