0

This code should display images from a database. It also creates a random background-color which works fine. The code is a function, the function is posted in index.php which is basically html. When I put the code directly in index.html witch the function and the first while loop it runs just fine.

    $query = mysql_query("SELECT `image_id`, `name`, `likes` FROM `images`");
    while(($row = mysql_fetch_assoc($query)) !== false){
        $sql = mysql_query("SELECT * FROM images");
        while($row = mysql_fetch_array($sql)){

        $images_id = $row["image_id"];
            $images_path = $row["path"];
            $images_name = $row["name"];
            $images_type = $row["type"];
            $images_size = $row["size"];
            $images_likes = $row["likes"];

            if($images_id == $images_id){   

            $kiesGetal = rand(1, 6);
                    switch ($kiesGetal){
                        case 1: $randKleur = "#a4a6a2";
                        break;

                        case 2: $randKleur = "#7e8661";
                        break;

                        case 3: $randKleur = "#9faac7";
                        break;

                        case 4: $randKleur = "#80957a";
                        break;

                        case 5: $randKleur = "#e09e6d";
                        break;

                        case 6: $randKleur = "#a0dd8d";
                        break;
                        }

        echo"<li style=\"background-color:$randKleur\" id=\"$images_id\" class=\"fotoFrame\">"
                     . "<p class=\"imagas_name\" id=\"$images_name\">$images_name</p>" 
                     . "<p>$images_likes mensen vinden dit leuk.</p>"        
                     . "<img class=\"images\" src='img/" . $row['name'] . "'></>" . "</li>";
            }else{
                    echo "error";
                }
        }
    }
            };
    ?> `
3
  • use switch case outside of while Commented Sep 26, 2013 at 11:30
  • 1
    and whats your question? Commented Sep 26, 2013 at 11:30
  • Why are you selecting all images in inner loop? If you really want to, change variables names, because you're overriding your previous query results. But I don't think you want to, it doesn't make any sense. Commented Sep 26, 2013 at 11:31

1 Answer 1

1
$query = mysql_query("SELECT `image_id`, `name`, `likes` FROM `images`");
while(($row = mysql_fetch_assoc($query)) !== false)

These lines are not necessary.

Sign up to request clarification or add additional context in comments.

Comments

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.