Hello, I have a question regarding some PHP coding. I have an query that pulls data from db, puts the data into arrays which are displayed (20 at a time) on my page. The question is how can I get newest record(s) to display first? New records are seeming to default to the bottom of the list. Here is the code: <?php require_once('includes/templates/template.php'); require_once('includes/templates/items.template.php'); function main() { $whereFields = array( 'subject','category','subcategory','grades','core','price','ppt','strand','standard' ); $queryData = array(); $queryWhere = array(); $paginationParams = array(); foreach ($whereFields as $fieldName) { if (isset($_GET[$fieldName])) { $queryWhere[] = $fieldName.' = :'.$fieldName; $queryData[':'.$fieldName] = $_GET[$fieldName]; $paginationParams[]='category='.urlencode($_GET['category']); } } $queryWhere = ( count($queryWhere) == 0 ? '' : 'WHERE '.implode(' AND ',$queryWhere) ); $paginationParams = implode('&',$paginationParams); require_once('dbSettings.php'); $dbSettings = dbSettings(); $db = new PDO( $dbSettings['dsn'], $dbSettings['user'], $dbSettings['password'] ); unset($dbSettings); // delete it as soon as we're done with it! $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE); // make PDO work with LIMIT $showPerPage = 20; /* get the count */ if (empty($queryWhere)) { $statement = $db->query(' SELECT COUNT(*) FROM items '); } else { $statement = $db->prepare(' SELECT COUNT(*) FROM items '.$queryWhere.' '); $statement->execute($queryData); } $itemTotal = $statement->fetchColumn(); /* get the items */ $itemPage = isset($_GET['page']) ? $_GET['page'] : 0; $itemOffset = $itemPage * $showPerPage; /* add the extra values to our query data */ $queryData[':page'] = $itemOffset; $queryData[':perPage'] = $showPerPage; /* 'WHERE' has to go before 'LIMIT' -- stupid if you ask me */ $statement = $db->prepare(' SELECT * FROM items '.$queryWhere.' LIMIT :page , :perPage '); $statement->execute($queryData); theme_header('SQL To Item with Pagination worksheets'); /* echo ' <h1>Results for following restrictions</h1> <pre>',print_r($queryData),'</pre>'; */ while ($item = $statement->fetch()) theme_itemContainer($item); theme_pagination( $itemPage, $showPerPage, $itemTotal, 'worksheets.php', $paginationParams ); theme_footer(); } main(); ?> Code (markup): Any help would be greatly appreciated.
I think the only thing you are looking for is a simple ORDER BY in your mysql code, I'm guessing you have a ID in the table, just simply add ORDER BY `#ID FIELD#` DESC Code (markup): This code must be the very last code in your query's, UNLESS you use a LIMIT, than thats the last thing in the query. I hope I helped you out!