Jump to content


Photo

returning only first result with numrows


  • Please log in to reply
7 replies to this topic

#1 hopeless

hopeless
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 29 September 2006 - 12:12 AM

Hope some one can help. I have a query which goes through an entire table and counts the number of instances of the same value which are spread across 3 columns. It does this as a foreach for a set of values it gets from another table according to the users selection.

It only needs to display the results where the count is less than 5. All works fine, it counts correctly and I can output the counts to a table and hide those which are over 5 but I actually only need it to display the first value it finds less than 5 of not all of them.

Any clues?

#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 29 September 2006 - 12:21 AM

Post some of you code so that I may be able to see how to place it in your code. Here is a basic example.

$count = mysql_num_rows($your_query);

if ($count < 5) {
    //do something
}else{
   //do something else
}

As I said this is a rather simple example. Does that help or??

EDIT I didn't throughly read your post..lol. I see what you are wanting to do now. You could do something like this.

$count = mysql_num_rows($your_query);

if ($count < 5) {
    $i = 1;
    while (($rw = mysql_fetch_assoc($your_query)) && ($i < 2)) {
            //do something with the $rw['whatever']
        $i++;
   }      
}else{
   //do something else
}

Good Luck,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 September 2006 - 12:21 AM

Please post your code. Without it we can't help you.

Ken

#4 hopeless

hopeless
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 29 September 2006 - 12:36 AM

<?php if ($this_year==2006){
  $start_month=$month+1;
$periods="SELECT id, month, y_06 FROM ads_months where id >='$start_month'";
$periods_list=mysql_query($periods) or die("couldn't get time periods");
while ($periods_row=mysql_fetch_array($periods_list)) {
$periods_final[$i++] = $periods_row;
			}
		//loop through the periods and get the number of ads for this page on this site for each period
				foreach ($periods_final as $periods_final) {
				$ads_totals = mysql_query("SELECT entry_id FROM ads_sites_TEST WHERE site_fk='$area_ID' AND page_fk='$ad_page' AND (month_1='".$periods_final['y_06']."' OR month_2='".$periods_final['y_06']."' OR month_3='".$periods_final['y_06']."')");
$numrows = mysql_num_rows($ads_totals); // count rows returned from query. 


(and what comes here is the bit I keep changing to get it to only display the first result under 4 - sorry I mistyped 5 before it's actually 4)}  ?>


#5 hopeless

hopeless
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 29 September 2006 - 12:37 AM

sorry I couldn't see how to put it in colour  :-[

#6 hopeless

hopeless
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 29 September 2006 - 12:42 AM

tomfmason - that's the sort of line I'd been folowing but I still get it showing all the results which are less than 4 not just the first one it finds an dthen ignoring any subsequent matching results. I am obviously missing something (and it is possibly obvious - I wouldn't be suprised at all :))

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 September 2006 - 12:47 AM

To colorize your code, edit your post to surround your code with [code]<?php ?>[/code] tags.

Ken

#8 hopeless

hopeless
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 29 September 2006 - 12:54 AM

hmm I think I got that done okay - has colour at least.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users