0

I've just made a 404 system that functions in two ways: ?404 - Will load a random meme. ?404=(a-u) - will specify a meme.

Currently I have to specify all of them in the selector part, is there a way get query string and echo variable from that?

<center>
    <br>
    <?php
        $a = '<img src="https://img-9gag-fun.9cache.com/photo/aAxQ9Bd_460s.jpg" style="max-height: 500px; min-height: 500px; max-width: 500px;">';
        $b = '<img src="https://img-9gag-fun.9cache.com/photo/aPjmGrB_460s.jpg" style="max-height: 500px; min-height: 500px; max-width: 500px;">';
        $c = '<img src="https://img-9gag-fun.9cache.com/photo/a2oMpY1_460s.jpg" style="max-height: 500px; min-height: 500px; max-width: 500px;">';
        $d = '<img src="https://img-9gag-fun.9cache.com/photo/anM7Djq_460s.jpg" style="max-height: 500px; min-height: 500px; max-width: 500px;">';
        $e = '<img src="https://img-9gag-fun.9cache.com/photo/awrmNLB_460s.jpg" style="max-height: 500px; min-height: 500px; max-width: 500px;">';
        $f = '<img src="https://img-9gag-fun.9cache.com/photo/aMAvmGM_460s.jpg" style="max-height: 500px; min-height: 500px; max-width: 500px;">';
        $g = '<video preload="auto" poster="https://img-9gag-fun.9cache.com/photo/avOWw3Z_460swp.webp" loop="loop" autoplay="autoplay" style="max-height: 500px; min-height: 500px; max-width: 500px;"><source src="https://img-9gag-fun.9cache.com/photo/avOWw3Z_460svvp9.webm" type="video/webm"><source src="https://img-9gag-fun.9cache.com/photo/avOWw3Z_460sv.mp4" type="video/mp4"><source src="https://img-9gag-fun.9cache.com/photo/avOWw3Z_460svwm.webm" type="video/webm"></video>';
        $h = '<video preload="auto" poster="https://img-9gag-fun.9cache.com/photo/a0Kognd_460swp.webp" loop="loop" autoplay="autoplay" style="max-height: 500px; min-height: 500px; max-width: 500px;"><source src="https://img-9gag-fun.9cache.com/photo/a0Kognd_460svvp9.webm" type="video/webm"><source src="https://img-9gag-fun.9cache.com/photo/a0Kognd_460sv.mp4" type="video/mp4"><source src="https://img-9gag-fun.9cache.com/photo/a0Kognd_460svwm.webm" type="video/webm"></video>';
        $i = '<video preload="auto" poster="https://img-9gag-fun.9cache.com/photo/agXQ9Wq_460swp.webp" loop="loop" autoplay="autoplay" style="max-height: 500px; min-height: 500px; max-width: 500px;"><source src="https://img-9gag-fun.9cache.com/photo/agXQ9Wq_460svvp9.webm" type="video/webm"><source src="https://img-9gag-fun.9cache.com/photo/agXQ9Wq_460sv.mp4" type="video/mp4"><source src="https://img-9gag-fun.9cache.com/photo/agXQ9Wq_460svwm.webm" type="video/webm"></video>';
        $j = '<video preload="auto" poster="https://img-9gag-fun.9cache.com/photo/aAxQ232_460swp.webp" loop="loop" autoplay="autoplay" style="max-height: 500px; min-height: 500px; max-width: 500px;"><source src="https://img-9gag-fun.9cache.com/photo/aAxQ232_460svvp9.webm" type="video/webm"><source src="https://img-9gag-fun.9cache.com/photo/aAxQ232_460sv.mp4" type="video/mp4"><source src="https://img-9gag-fun.9cache.com/photo/aAxQ232_460svwm.webm" type="video/webm"></video>';
        $k = '<video preload="auto" poster="https://img-9gag-fun.9cache.com/photo/anMOZZo_460swp.webp" loop="loop" autoplay="autoplay" style="max-height: 500px; min-height: 500px; max-width: 500px;"><source src="https://img-9gag-fun.9cache.com/photo/anMOZZo_460svvp9.webm" type="video/webm"><source src="https://img-9gag-fun.9cache.com/photo/anMOZZo_460sv.mp4" type="video/mp4"><source src="https://img-9gag-fun.9cache.com/photo/anMOZZo_460svwm.webm" type="video/webm"></video>';
        $l = '<video preload="auto" poster="https://img-9gag-fun.9cache.com/photo/aExMKDn_460swp.webp" loop="loop" autoplay="autoplay" style="max-height: 500px; min-height: 500px; max-width: 500px;"><source src="https://img-9gag-fun.9cache.com/photo/aExMKDn_460svvp9.webm" type="video/webm"><source src="https://img-9gag-fun.9cache.com/photo/aExMKDn_460sv.mp4" type="video/mp4"><source src="https://img-9gag-fun.9cache.com/photo/aExMKDn_460svwm.webm" type="video/webm"></video>';
        $m = '<video preload="auto" poster="https://img-9gag-fun.9cache.com/photo/aAxQAPZ_460swp.webp" loop="loop" autoplay="autoplay" style="max-height: 500px; min-height: 500px; max-width: 500px;"><source src="https://img-9gag-fun.9cache.com/photo/aAxQAPZ_460svvp9.webm" type="video/webm"><source src="https://img-9gag-fun.9cache.com/photo/aAxQAPZ_460sv.mp4" type="video/mp4"><source src="https://img-9gag-fun.9cache.com/photo/aAxQAPZ_460svwm.webm" type="video/webm"></video>';
        $n = '<video preload="auto" poster="https://img-9gag-fun.9cache.com/photo/aZ3n9zz_460swp.webp" loop="loop" autoplay="autoplay" style="max-height: 500px; min-height: 500px; max-width: 500px;"><source src="https://img-9gag-fun.9cache.com/photo/aZ3n9zz_460svvp9.webm" type="video/webm"><source src="https://img-9gag-fun.9cache.com/photo/aZ3n9zz_460sv.mp4" type="video/mp4"><source src="https://img-9gag-fun.9cache.com/photo/aZ3n9zz_460svwm.webm" type="video/webm"></video>';
        $o = '<video preload="auto" poster="https://img-9gag-fun.9cache.com/photo/agXQRo6_460swp.webp" loop="loop" autoplay="autoplay" style="max-height: 500px; min-height: 500px; max-width: 500px;"><source src="https://img-9gag-fun.9cache.com/photo/agXQRo6_460svvp9.webm" type="video/webm"><source src="https://img-9gag-fun.9cache.com/photo/agXQRo6_460sv.mp4" type="video/mp4"><source src="https://img-9gag-fun.9cache.com/photo/agXQRo6_460svwm.webm" type="video/webm"></video>';
        $p = '<video preload="auto" poster="https://img-9gag-fun.9cache.com/photo/a0KXb7Q_460swp.webp" loop="loop" autoplay="autoplay" style="max-height: 500px; min-height: 500px; max-width: 500px;"><source src="https://img-9gag-fun.9cache.com/photo/a0KXb7Q_460svvp9.webm" type="video/webm"><source src="https://img-9gag-fun.9cache.com/photo/a0KXb7Q_460sv.mp4" type="video/mp4"><source src="https://img-9gag-fun.9cache.com/photo/a0KXb7Q_460svwm.webm" type="video/webm"></video>';
        $r = '<video preload="auto" poster="https://img-9gag-fun.9cache.com/photo/aLjz2LV_460swp.webp" loop="loop" autoplay="autoplay" style="max-height: 500px; min-height: 500px; max-width: 500px;"><source src="https://img-9gag-fun.9cache.com/photo/aLjz2LV_460svvp9.webm" type="video/webm"><source src="https://img-9gag-fun.9cache.com/photo/aLjz2LV_460sv.mp4" type="video/mp4"><source src="https://img-9gag-fun.9cache.com/photo/aLjz2LV_460svwm.webm" type="video/webm"></video>';
        $r = '<video preload="auto" poster="https://img-9gag-fun.9cache.com/photo/a0KXYvZ_460swp.webp" loop="loop" autoplay="autoplay" style="max-height: 500px; min-height: 500px; max-width: 500px;"><source src="https://img-9gag-fun.9cache.com/photo/a0KXYvZ_460svvp9.webm" type="video/webm"><source src="https://img-9gag-fun.9cache.com/photo/a0KXYvZ_460sv.mp4" type="video/mp4"><source src="https://img-9gag-fun.9cache.com/photo/a0KXYvZ_460svwm.webm" type="video/webm"></video>';
        $s = '<video preload="auto" poster="https://img-9gag-fun.9cache.com/photo/arg34Yd_460swp.webp" loop="loop" autoplay="autoplay" style="max-height: 500px; min-height: 500px; max-width: 500px;"><source src="https://img-9gag-fun.9cache.com/photo/arg34Yd_460svvp9.webm" type="video/webm"><source src="https://img-9gag-fun.9cache.com/photo/arg34Yd_460sv.mp4" type="video/mp4"><source src="https://img-9gag-fun.9cache.com/photo/arg34Yd_460svwm.webm" type="video/webm"></video>';
        $t = '<iframe height="500" width="890" src="https://www.youtube.com/embed/a3nPG9JpoLg?rel=0&amp;controls=0&amp;showinfo=0&autoplay=1" frameborder="0" allow="autoplay encrypted-media"></iframe> ';
         if ($_GET['404'] == 'a') { echo $a; }
    else if ($_GET['404'] == 'b') { echo $b; }
    else if ($_GET['404'] == 'c') { echo $c; }
    else if ($_GET['404'] == 'd') { echo $d; }
    else if ($_GET['404'] == 'e') { echo $e; }
    else if ($_GET['404'] == 'f') { echo $f; }
    else if ($_GET['404'] == 'g') { echo $g; }
    else if ($_GET['404'] == 'h') { echo $h; }
    else if ($_GET['404'] == 'i') { echo $i; }
    else if ($_GET['404'] == 'j') { echo $j; }
    else if ($_GET['404'] == 'k') { echo $k; }
    else if ($_GET['404'] == 'k') { echo $l; }
    else if ($_GET['404'] == 'm') { echo $m; }
    else if ($_GET['404'] == 'n') { echo $n; }
    else if ($_GET['404'] == 'o') { echo $o; }
    else if ($_GET['404'] == 'p') { echo $p; }
    else if ($_GET['404'] == 'q') { echo $q; }
    else if ($_GET['404'] == 'r') { echo $r; }
    else if ($_GET['404'] == 's') { echo $s; }
    else if ($_GET['404'] == 't') { echo $t; }
    else {
        $error = range('a', 't');
        $random_error = array_rand($error);
        echo $$error[$random_error];
    }
    ?>
    <h1>You dun goofed</h1>
    <?php echo 'Error code: '. $_SERVER['QUERY_STRING']; ?>
</center>
<style>
    body { overflow-y: hidden; }
    #content { height: 1000px; }
</style>
4
  • Please try and keep your questions self-contained. Off-site links are strongly discouraged because they're inconvenient, might be firewalled for some people, and may change or get deleted later. Commented May 1, 2018 at 21:07
  • 1
    I'd also recommend you learn about PHP's associative arrays because this sort of style of programming is not sustainable. With an associative array you can do $message[$_GET['404']] instead of needing N if statements. Commented May 1, 2018 at 21:08
  • Its a flipping 58 lines, wouldnt pasting it in there be considered spam or something? Commented May 1, 2018 at 21:09
  • The trick is to post just enough lines that we know what you're talking about. Like post a,b...y,z and we can understand there's other stuff snipped for brevity. You don't need to exhaustively list all the things. Posting it off-site doesn't help us read all that duplicated code any faster. Commented May 1, 2018 at 21:10

2 Answers 2

3

Instead of :

    $a = '<img src="https://img-9gag-fun.9cache.com/photo/aAxQ9Bd_460s.jpg" style="max-height: 500px; min-height: 500px; max-width: 500px;">';
    $b = '<img src="https://img-9gag-fun.9cache.com/photo/aPjmGrB_460s.jpg" style="max-height: 500px; min-height: 500px; max-width: 500px;">';
    $c = '<img src="https://img-9gag-fun.9cache.com/photo/a2oMpY1_460s.jpg" style="max-height: 500px; min-height: 500px; max-width: 500px;">';

etc, you could use an associative array, like:

$memes = array(
    'a' => '<img src="https://img-9gag-fun.9cache.com/photo/aAxQ9Bd_460s.jpg" style="max-height: 500px; min-height: 500px; max-width: 500px;">',
    'b' => '<img src="https://img-9gag-fun.9cache.com/photo/aPjmGrB_460s.jpg" style="max-height: 500px; min-height: 500px; max-width: 500px;">',
    'c' => '<img src="https://img-9gag-fun.9cache.com/photo/a2oMpY1_460s.jpg" style="max-height: 500px; min-height: 500px; max-width: 500px;">',
 );

Then you could use the query string value to get the member based on that key, like:

echo $memes[$_GET['404']];
Sign up to request clarification or add additional context in comments.

5 Comments

A great first refactoring.
Could you show me how I'd add this to my code? I have like 3 weeks of experience with PHP, 40% of this code isn't even by me...
@DoubleThePsycho replace the if ($_GET['404'] == 'a') { echo $a; } lines with if (array_key_exists('404', $_GET) && array_key_exists($GET['404'], $memes)){ echo $memes[$_GET['404']]; } else { //... error }
I gave this a try. The rest of the page loaded this time, but the meme itself doesn't appear. My current code: 'u' => '' ); if (array_key_exists('404', $_GET) && array_key_exists($GET['404'], $memes)){ echo $memes[$_GET['404']]; } else {} ?> <h1>You dun goofed</h1> <?php echo $random_error; ?>
Changed it to 3v4l.org/ADOtJ to make item display on random, thanks a lot for the help pal!
0

You can use a variable-variable:

echo $$_GET['404'];

although with it being an array you may have to do:

echo ${$_GET['404']};

http://sandbox.onlinephpfunctions.com/code/4f51e957806e803300dd58e892532e311a19a32a

A better solution, though, would be to save the variables in an array:

$images = ['a' => 'something.jpg', 'b' => . 'something-else.png'];
echo $images[$_GET['404']];

1 Comment

While technically correct, this just doubles down on the problematic design of this code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.