Incredinot Posted March 24, 2010 Share Posted March 24, 2010 Hi.. Okay, first off here is the code, (Ill try explain underneeth it what i am trying to achieve) <?php $not_allowed = 5; $butik_id = mysql_query("SELECT DISTINCT nid FROM node WHERE type='butikker' ORDER BY RAND() LIMIT 3"); $i = 1; while ($i <= 3) { // Get node id. $butik_id_row = mysql_fetch_array($butik_id); $id = $butik_id_row['nid']; // Get term id $term_id_search = mysql_query("SELECT tid FROM term_node WHERE nid='$id'"); $term_id = mysql_fetch_array($term_id_search); $tid = $term_id['tid']; //-------------------------------------------------------------------------------------- // Print ID // IF the $tid is equal to $not_allowed it should skip it and grap a new one. if($tid != $not_allowed){ print $id; $i++;}} ?> First of i want to pull out a random value from a DB, and then (with a while loop) print out 3 values - which works just fine! But now i want to avoid getting some specific values out from the DB. In the code above i dont want any values where the "$tid" is equal to 5 ($not_allowed). Now when i try to run this code - it do not print out any values that have a $tid of 5 which is fine - BUT... At the same time, it sometimes only show one value - or even no values.... And one last thing... Im only able to get the $tid after i know the $id of the node. Link to comment https://forums.phpfreaks.com/topic/196333-need-help-with-while-loop/ Share on other sites More sharing options...
slurpee Posted March 24, 2010 Share Posted March 24, 2010 What about just using a single sql query? SELECT term_node.tid FROM term_node, node WHERE term_node.tid=node.nid AND node.type='butikker' ORDER AND term_node.tid != $not_allowed BY RAND() LIMIT 3 Link to comment https://forums.phpfreaks.com/topic/196333-need-help-with-while-loop/#findComment-1030950 Share on other sites More sharing options...
Incredinot Posted March 24, 2010 Author Share Posted March 24, 2010 That dont work.. Maybe there is something wrong with it? What i want it to do is: 1. Get distinct and random "nid" from "node". 2. Get "tid" from "term_node" where "nid" = (the above nid) and "tid" != 5. 3. Make an mysql_fetch_array so $id contains an accual value... Ive been trying to get this to work, but so far no luck... Ill keep on trying, but if anyone know how todo this - please help me! (: Link to comment https://forums.phpfreaks.com/topic/196333-need-help-with-while-loop/#findComment-1030956 Share on other sites More sharing options...
Incredinot Posted March 24, 2010 Author Share Posted March 24, 2010 I figured it out... Quite simple accually (Or aint all programming that when you know how to do it? :b) Here is my final code that worked: $butik_id = mysql_query("SELECT * FROM node, term_node WHERE term_node.tid != 5 AND node.type ='butikker' ORDER BY RAND() LIMIT 3"); Link to comment https://forums.phpfreaks.com/topic/196333-need-help-with-while-loop/#findComment-1030960 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.