Thursday, 15 July 2010

How do I display data retrieved from mysql db in a specific order using php -



How do I display data retrieved from mysql db in a specific order using php -

i trying display questions, along users reply in specific order, not asc or desc, defined "question_order" column.

i have next tables in mysql db:

questions (qid, question_text) answers (aid, uid, answer) usermeta (userid, question_order) "questions" table contains questions "answers" table contains every users answers questions "usermeta" table contains sort order questions in "question_order".

"question_order" unique per user , in db pipe delimited list. (i.e.: 85|41|58|67|21|8|91|62,etc.)

php version 5.3.27

if entire procedure can improve accomplished using different method, please allow me know.

my php ability limited. said, below have @ moment after several hours of playing ...

$sql = " select * ".usermeta_table." userid = {$userid} "; $result = $db->query($sql) or sql_error($db->error.'<br />'.$sql); $row = $result->fetch_assoc(); $order_array = explode('|', $row['question_order']); $sql = " select * ".questions_table." "; $result = $db->query($sql) or sql_error($db->error.'<br />'.$sql); $row = $result->fetch_assoc(); // effort @ sorting questions. $order_array // not have unique id kind of lost // how create work usort($myarray, function($order_array, $row) { homecoming $order_array - $row['qid']; }); $sql = " select * ".questions_table." "; $result = $db->query($sql) or sql_error($db->error.'<br />'.$sql); while ( $row = $result->fetch_assoc() ) { $sql = " select * ".answers_table." uid = {$userid} , qid = ".$row['qid']." limit 1 "; $result2 = $db->query($sql) or sql_error($db->error.'<br />'.$sql); $row2 = $result2->fetch_assoc(); echo ' <p>'.$row['question_text'].'</p>'."\n"; echo ' <p>'.$row2['answer'].'</p>'."\n"; }

filter out info when retrieving db.

use:- select * [table_name] order qid desc

then in php can utilize session variables , modify values accordingly.

php mysql arrays usort

No comments:

Post a Comment