Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Integrating PHP with Databases Limiting Records in SQL Getting Specific with Queries

ISSUE USE ORDER BY AND LIMIT IN PHP

WHY IT DONT WORK LIKE TEACHER [MY ERRO] http://imgur.com/Dcvq12Y

index.php
<?php 
include("inc/functions.php");
$pageTitle = "Personal Media Library";
$section = null;

include("inc/header.php"); ?>
        <div class="section catalog random">

            <div class="wrapper">

                <h2>May we suggest something?</h2>

        <ul class="items">
            <?php
            $random = random_catalog_array();
            foreach ($random as $item) {
                echo get_item_html($item);
            }
            ?>                          
                </ul>

            </div>

        </div>

<?php include("inc/footer.php"); ?>
catalog.php
<?php 
include("inc/functions.php");
$catalog = full_catalog_array();

$pageTitle = "Full Catalog";
$section = null;

if (isset($_GET["cat"])) {
    if ($_GET["cat"] == "books") {
        $pageTitle = "Books";
        $section = "books";
    } else if ($_GET["cat"] == "movies") {
        $pageTitle = "Movies";
        $section = "movies";
    } else if ($_GET["cat"] == "music") {
        $pageTitle = "Music";
        $section = "music";
    }
}

include("inc/header.php"); ?>

<div class="section catalog page">

    <div class="wrapper">

        <h1><?php 
        if ($section != null) {
            echo "<a href='catalog.php'>Full Catalog</a> &gt; ";
        }
        echo $pageTitle; ?></h1>

        <ul class="items">
            <?php
            $categories = array_category($catalog,$section);
            foreach ($categories as $id) {
                echo get_item_html($catalog[$id]);
            }
            ?>
        </ul>

    </div>
</div>

<?php include("inc/footer.php"); ?>
fuction.php
<? function random_catalog_array() {
  include("connection.php");
  try{
  $results = $db->query("SELECT media_id, title,category,img
            FROM Media
            ORDER BY RAND()
            LIMIT 4"
          );
   } 
    catch(Exception $e){
    echo "Unable to retrieved results";
    exit;
    }
    $catalog=$results->fetchAll();
    return $catalog;
} ?>

1 Answer

Simon Coates
Simon Coates
28,694 Points

Her method (random_catalog_array) looks the same, but she uses ORDER BY RANDOM() Can you see if that changes anything?

Thanks

Ranvir Sahota
Ranvir Sahota
9,844 Points

But why does this work? In the video she uses RAND but I had to use RANDOM? what was the difference between the two?

Simon Coates
Simon Coates
28,694 Points

Ranvir Sahota I think it might be the use of different databases and some differences in terms of sql between SQLLite and MySQL. But it's been ages since I did the tutorials, so i'm not sure.