Page 1 of 1

PHP - $num_of_rows - How to summarize from an array

Posted: Mon Dec 15, 2008 4:05 pm
by laural
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I have a page that searches mssql and returns a result set. I use $num_of_rows = mssql_num_rows ($msresults); to get the number of results. So far so good. After this comes my for statement and each line item of my results. I use an if statement to determine whether the line item is allowed to be viewed by the end user. So, my result set may have 15, but only 5 of those are being displayed to the end user. What I want to know is how do I make $num_of_rows reflect only what I am showing to the viewers instead of the total of all records found from the sql search... I can create a variable with a 1 if the viewer is allowed to see the data, and a 0 if they are not, but I do not know how to get that sum into the $num_of_rows variable... hopefully this is clear enough that someone could enlighten me... :banghead:

Code: Select all

<?php
 
SQL SEARCH DONE HERE

$num_of_rows = mssql_num_rows ($msresults); 
 
 for($i=0; $i<$RowPerPage; $i++) {
  $row = mssql_fetch_array($msresults);
 if(!$row){ break; }
 
VARIABLES SET HERE
 
If (Statement to determine which data is viewable) {

echo DATA;
 
}
:D Laura


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: PHP - $num_of_rows - How to summarize from an array

Posted: Mon Dec 15, 2008 4:08 pm
by jaoudestudios
What is in the conditional statement? Two if statements in a loop will be a performance hit. Can you not just pull out exactly what you want from the database. i.e put the conditions in the query?

Re: PHP - $num_of_rows - How to summarize from an array

Posted: Mon Dec 15, 2008 4:42 pm
by cavemaneca
Same as above, just put a "WHERE `allowed` = '1'" or whatever you use to check permissions in the original query.
Also, try it like this

Code: Select all

if ($num_of_row > 0) {
  while {$row = mssql_fetch_array($msresults)} {
    print_r $row;
  }
}
else {
  echo 'No rows found!';
}
It's simpler and still echoes a result if the query returns no data.

Re: PHP - $num_of_rows - How to summarize from an array

Posted: Mon Dec 15, 2008 4:49 pm
by pickle
The problem is that you want to display the number of rows earlier in the page than where you show the actual rows?

Rather than outputing the rows directly, store them in a variable. Then, in your for() loop, increment a counter every time an allowed row is encountered. After your for() loop is done, output the counter, then output the variable that's storing your row data.

Re: PHP - $num_of_rows - How to summarize from an array

Posted: Mon Dec 15, 2008 7:42 pm
by laural
What is in the conditional statement? Two if statements in a loop will be a performance hit. Can you not just pull out exactly what you want from the database. i.e put the conditions in the query?
Unfortunately the conditions do not exist (nor can they exist) in the database. It is pretty complicated...
The problem is that you want to display the number of rows earlier in the page than where you show the actual rows?

Rather than outputing the rows directly, store them in a variable. Then, in your for() loop, increment a counter every time an allowed row is encountered. After your for() loop is done, output the counter, then output the variable that's storing your row data.
This sound like what I need... I am going to give this a try - thanks so much everyone!

Laura