Waynz Posted June 18, 2008 Share Posted June 18, 2008 hi, im currently running a statement on a database which is used for a forum, what i want to be able to do is retrieve, the last 5 topics (topic_last_post_id), the most replies(topic_replies), the most views(topic_views). currently is have a statement which brings everything back, i want to know is it possible to create 3 different array for each (as state above) which is sorted by topic_last_post_id, topic_replies, topic_views. so in effect the statement would sort the results depending on what the array wanted. ARRAY1 = RECENT POSTS (top 5 TOPIC LAST POST ID) ARRAY2 = MOST REPLYS (top 5 TOPIC REPLIES) ARRAY3 = MOST VIEWS (top 5 TOPIC VIEWS) is this possible? here is what i have so far, don't know if this would work as im not an expert in this field. would i be able to put a ORDER BY in the statement which would order the results depending on the array and get the top 5? for example array one would be $result = mysql_query("SELECT topic_id, topic_title, topic_replies, topic_views, topic_last_post_id FROM phpbb_topics ORDER BY topic_last_post_id DESC LIMIT 0,5); so the order by would be a varible depending on the array thanks $result = mysql_query("SELECT topic_id, topic_title, topic_replies, topic_views, topic_last_post_id FROM phpbb_topics LIMIT 0,5); $result_topics_a = sprintf( $result,'t.topic_last_post_id'); $result_topics_b = sprintf( $result,'topic_replies'); $result_topics_c = sprintf( $result,'topic_views'); Quote Link to comment Share on other sites More sharing options...
fenway Posted June 18, 2008 Share Posted June 18, 2008 would i be able to put a ORDER BY in the statement which would order the results depending on the array and get the top 5? Why not? Quote Link to comment Share on other sites More sharing options...
Waynz Posted June 18, 2008 Author Share Posted June 18, 2008 how would i implement that into the array part? Quote Link to comment Share on other sites More sharing options...
Waynz Posted June 18, 2008 Author Share Posted June 18, 2008 ok i have it working using the variable $variable='topic_title'; $number=0; $the_5_last_posts=""; $forumQ=mysql_query("SELECT topic_id, topic_title, topic_replies, topic_views, topic_last_post_id FROM phpbb_topics ORDER BY $variable DESC LIMIT 20"); while($forumRow = mysql_fetch_row($forumQ)){ $number++; echo "<a>".$forumRow[1]. " - " .$foru mRow[2]." - " .$forumRow[3]." - " .$forumRow[4]." - " .$forumRow[5]."</a><br>"; } but thats me setting the variable at the begining, i want it to automatically change teh variable depending on the array its building ARRAY1 = RECENT POSTS varible would be LAST_POST_ID ARRAY2 = MOST REPLYS varible would be TOPIC_REPLIES ARRAY3 = MOST VIEWS varible would be TOPIC _VIEWS how would the new code look? $ARRAY1 = sprintf($forumQ); $ARRAY2 = sprintf($forumQ); <-------------- would i need to put $variable in here somewhere?, so it $ARRAY3 = sprintf($forumQ); changes the variable before running the statement, if so how thanks Quote Link to comment Share on other sites More sharing options...
fenway Posted June 18, 2008 Share Posted June 18, 2008 You need to use %s in your query. Quote Link to comment Share on other sites More sharing options...
Waynz Posted June 18, 2008 Author Share Posted June 18, 2008 sorry im unfamiliar with these, can you give me an example or definition please Quote Link to comment Share on other sites More sharing options...
fenway Posted June 18, 2008 Share Posted June 18, 2008 sorry im unfamiliar with these, can you give me an example or definition please You're unfamiliar with sprintf(), but you're using it in your code? Now really.... BTW $forumQ had better be the query string, not the result set. $forumQ="SELECT topic_id, topic_title, topic_replies, topic_views, topic_last_post_id FROM phpbb_topics ORDER BY %s DESC LIMIT 20"; $array1Q = sprintf( $forumQ, 'topic_title' ); And then you can simply run this query. Quote Link to comment Share on other sites More sharing options...
Waynz Posted June 19, 2008 Author Share Posted June 19, 2008 thanks got it sorted now Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.