joecooper Posted August 28, 2008 Share Posted August 28, 2008 Hi, i have a site im building, its like one of those sites that update daily with funny videos / games. i have a mysql database with a game title, description and link. I need the last 2 enterys added (using the ID field) to be seperate at the top (featured), and then the rest to be on a table as a list, 10 on each page. i used to use this before but was a while ago and since lost the code i had. thanks. Link to comment https://forums.phpfreaks.com/topic/121752-help-with-multiple-mysql-rows/ Share on other sites More sharing options...
pocobueno1388 Posted August 28, 2008 Share Posted August 28, 2008 You could do this: <?php $query = mysql_query("SELECT fields FROM table"); $i = 1; while ($row = mysql_fetch_assoc($query)){ if ($i == 1 || $i == 2){ //this is where you put the code for the two featured posts } else { //this is where you put the code for all the other posts } $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/121752-help-with-multiple-mysql-rows/#findComment-628095 Share on other sites More sharing options...
joecooper Posted August 28, 2008 Author Share Posted August 28, 2008 so to return the "title", would it be $row['title']? Link to comment https://forums.phpfreaks.com/topic/121752-help-with-multiple-mysql-rows/#findComment-628096 Share on other sites More sharing options...
pocobueno1388 Posted August 28, 2008 Share Posted August 28, 2008 so to return the "title", would it be $row['title']? Yes, as long as you use the code I supplied. Just make sure you change the query to select what you need. Link to comment https://forums.phpfreaks.com/topic/121752-help-with-multiple-mysql-rows/#findComment-628099 Share on other sites More sharing options...
joecooper Posted August 28, 2008 Author Share Posted August 28, 2008 ok, although the 2 featured need to be the last 2 added, so the highest number ID, not 1 & 2, how can i do this? Link to comment https://forums.phpfreaks.com/topic/121752-help-with-multiple-mysql-rows/#findComment-628105 Share on other sites More sharing options...
pocobueno1388 Posted August 28, 2008 Share Posted August 28, 2008 change the query to $query = mysql_query("SELECT fields FROM table ORDER BY id DESC"); Link to comment https://forums.phpfreaks.com/topic/121752-help-with-multiple-mysql-rows/#findComment-628107 Share on other sites More sharing options...
joecooper Posted August 28, 2008 Author Share Posted August 28, 2008 include("db.php"); $query = mysql_query("Select * FROM games"); $i = 1; while ($row = mysql_fetch_assoc($query)){ if ($i == 1 || $i == 2){ echo("$row['title']"); } else { //this is where you put the code for all the other posts } $i++; } that always returns a blank page, no errors or anything. and even the HTML that comes after is cleared. Link to comment https://forums.phpfreaks.com/topic/121752-help-with-multiple-mysql-rows/#findComment-628108 Share on other sites More sharing options...
pocobueno1388 Posted August 28, 2008 Share Posted August 28, 2008 Try <?php include("db.php"); $query = mysql_query("SELECT * FROM games")or die(mysql_error()); $i = 1; while ($row = mysql_fetch_assoc($query)){ if ($i == 1 || $i == 2){ echo $row['title'] .'<br>'; } else { //this is where you put the code for all the other posts } $i++; } Link to comment https://forums.phpfreaks.com/topic/121752-help-with-multiple-mysql-rows/#findComment-628112 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.