Jump to content

Archived

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

hopeless

returning only first result with numrows

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

[code=php:0]
$count = mysql_num_rows($your_query);

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

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

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

[code=php:0]
$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
}
[/code]

Good Luck,
Tom

Share this post


Link to post
Share on other sites
Please post your code. Without it we can't help you.

Ken

Share this post


Link to post
Share on other sites
[code]<?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)}  ?> [/code]

Share this post


Link to post
Share on other sites
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 :))

Share this post


Link to post
Share on other sites
To colorize your code, edit your post to surround your code with [nobbc][code]<?php ?>[/code][/nobbc] tags.

Ken

Share this post


Link to post
Share on other sites

×

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.