Jump to content

[SOLVED] 3 arrays from one search, ordered by varible depending on array


Waynz

Recommended Posts

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');

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.