PHP - $num_of_rows - How to summarize from an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
laural
Forum Newbie
Posts: 6
Joined: Fri Apr 04, 2008 9:44 am

PHP - $num_of_rows - How to summarize from an array

Post 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.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

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

Post 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?
cavemaneca
Forum Commoner
Posts: 59
Joined: Sat Dec 13, 2008 2:16 am

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

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
laural
Forum Newbie
Posts: 6
Joined: Fri Apr 04, 2008 9:44 am

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

Post 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
Post Reply