Forum Moderators: coopster

Message Too Old, No Replies

Static Variable inside a loop

         

BlackRaven

11:23 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



hi, ok got another problem for some reason my $xcount variable only shows a value of 1, even though there are more then one matches. The $xcount counts the number of matches and then passes this info further downscript (..not included..too long) where i use it to find the total number of pages. Everything works if u manually input a value of $xcount so i am sure it has to do something with the while loop. Thank You

<?php
$user="red";
$pw="red";
$table=$subcategory;
$mysql_access = mysql_connect("localhost", $user, $pw);
$result=mysql_select_db($dbname, $mysql_access);

if ( isset($locationcountry) && $locationcountry!= "" )
{
$where_query .= empty($locationcountry)? " WHERE " : " AND ";
$where_query .= " locationcountry LIKE '%" . $locationcountry . "%'";
}

$page = $_GET["page"];
if (($page<0) ¦¦ ($page=="")) $page=0;
$epp = 1; /* entries per page */

$l1 = $epp*($page);
$l2 = $epp;


$result = MYSQL_QUERY( "SELECT * FROM $table".$where_query." ORDER BY date LIMIT $l1, $l2 ");

$xcount=0;

while($row = mysql_fetch_row($result)){
$xcount=$xcount+1;
print($row[0]);
}

?>

dreamcatcher

11:47 pm on Jan 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you sure your while loop is returning more than 1 row? Your $xcount variable looks fine and should increment as the loop loops. I`m guessing your LIMIT clause might be suspect. Echo mysql_num_rows($result) after your query to see how many rows are fetched.

btw, you can also use

$xcount++;

which is the same as $xcount=$xcount+1;

dc

BlackRaven

7:25 am on Jan 5, 2005 (gmt 0)

10+ Year Member



yeah my while loops returns the corect number of rows, i also removed the LIMIT to see what would happed, still the $xcount returns 1.

storevalley

9:37 am on Jan 5, 2005 (gmt 0)

10+ Year Member



You gonna write any of this code yourself BlackRaven? :)

If you want to know how many rows there are in a result set, just use ...

$number_of_rows = mysql_num_rows($result);

If that tells you that there is one row too, I suspect that your query is not working quite the way you think it is ...

BlackRaven

7:00 pm on Jan 5, 2005 (gmt 0)

10+ Year Member



lol..ok..cheeky storevalley, i will try that once i get home. THanks

BlackRaven

11:43 pm on Jan 5, 2005 (gmt 0)

10+ Year Member



ok got it working but had to resort to using 2 sqlquery, am will this have much effect on performance?

$result = MYSQL_QUERY( "SELECT * FROM $table".$where_query." ORDER BY date LIMIT $l1, $l2 ");
$forrowcount = MYSQL_QUERY( "SELECT * FROM $table".$where_query);

storevalley

10:07 am on Jan 6, 2005 (gmt 0)

10+ Year Member



They are 2 different queries, BlackRaven.

If ...

  • $table and $where_query are the same in both SQL statements
  • The first is returning 1 row
  • The second is returning more than 1 row
Then your LIMIT clause is the reason that the original statement only returned one row.

Have you tried printing out $l1 and $l2? That will probably show you the problem straight away (maybe you are accidentally setting $l1 and $l2 to the same value?)

ergophobe

9:41 pm on Jan 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



exactly...

On your first page, $page gets set to 0, so...

$l1 = $epp*($page); // = 0
$l2 = $epp; // = 1

So your limit clause is

LIMIT 0, 1

that returns either one row (match) or zero rows (no matches). So $xcount will always be either 0 or 1.

ergophobe

9:44 pm on Jan 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



BTW, a better way to handle this is

if (empty($_GET['page']))

this handles the cases where
$_GET['page'] = 0
$_GET['page'] = ""
$_GET['page'] = is not set at all and error reporting is set to E_ALL

It would not, of course, handle negative numbers, but unless you're using negative nubmers for error reporting, there shouldn't be any (there's no page -1 presumably)

storevalley

10:14 pm on Jan 6, 2005 (gmt 0)

10+ Year Member



ergophobe ... what a spoilsport!

I deliberately left the last bit of the puzzle for him to work out for himself :)

ergophobe

10:35 pm on Jan 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry! I do that often as well. If I had spent more time on it, maybe I wouldn't have posted ;-) (you know the old Mark Twain comment, something like "sorry this letter is so long, but I didn't have much time).

storevalley

11:08 pm on Jan 6, 2005 (gmt 0)

10+ Year Member



Not to worry ... looks like poor old BlackRaven's got plenty of other code to practise on :)