Jump to content

Need a little bit of help!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
jaydee

jaydee

    Newbie

  • Members
  • PipPip
  • 17 posts
Hey guys,

Right, basically I have 2 tables. History, and ads. I need to know how I go about selecting all the adid's from History and then selecting a random 'adid' from 'ads' that doesn't have a adid that is in the History table. Will try explain a little better, so basically I need to get a random row from the 'ads' table that hasn't already been added to the history table. Please post if you don't get what I mean, I'll try explain again a bit differently!

I've tried using mysql_fetch_array to get a array of data from the history table and then selecting from ads where 'adid' doesn't equal $adid. Like below...

$q2 = mysql_query("SELECT * FROM clickhistory");

	$r2 = mysql_fetch_array($q2);

	$ads = $r2['adid'];

	$q3 = mysql_query("SELECT * FROM ads WHERE adid != '$ads'");

	$r3 = mysql_fetch_array($q3);

I'm pretty tired so may have missed something, or this might be the total wrong way, I've just never had to do anything like this before! Code above is just a re-written example not the actual code!

Thanks in advance for any help!

#2
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Your query won't work as $ads contains the array. Only way I can currently come up with to do this would be to retrieve the history rows and add " AND adid != '$curr_adid' " to the query each time a new row is found ( and so a new adid should be excluded from results of the query ). Like:

$get_ch = mysql_query("SELECT * FROM clickhistory");
$ad_query = "SELECT * FROM ads";
$first = 1;
while($res = mysql_fetch_assoc($get_ch)) {
   $curr_adid = $res['adid'];
   if($first != 1) {
   $ad_query .= " AND ";
   }else{
   $ad_query .= " WHERE ";
   $first = 0;
   }
   $ad_query .= " adid != {$curr_adid} ";
}
//your query
$ad_query = mysql_query($ad_query);
//Then retrieve resultsI'd prefer using mysql_fetch_assoc inside a while loop though
$get_ads = mysql_fetch_array($ad_query);

Though there might be a better or faster way directly from the MySql query to do this, but couldn't come up with any. This should work atleast, though.

#3
kuhni

kuhni

    Newbie

  • Members
  • Pip
  • 1 posts
Да, сам там беру
куплю фасад

#4
jaydee

jaydee

    Newbie

  • Members
  • PipPip
  • 17 posts
Will post back if it works later on, I think this is probably the most easiest as I thought it would be something along those lines just couldn't think exactly how to get there!