Jump to content

Need help with while loop


Incredinot

Recommended Posts

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

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! (:

I figured it out... Quite simple accually :o (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"); 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.