Page 1 of 1

I need a simple translation problem solved: ASP to PHP

Posted: Wed Oct 08, 2003 4:28 pm
by voodoo9055
How do I do this ASP example

Code: Select all

While Not rs.EOF
   
     If rs.EOF Then
          Response.write "end of file"
     Else 
          Response.write "not end of file"
     End If
 
     rs.MoveNext
Wend
in PHP?????

I particularly interested in this section

Code: Select all

If rs.EOF Then
          Response.write "end of file"
     Else
The only thing I know how to do is

Code: Select all

<?php 
while ($row = mysql_fetch_array($link) ) {

      echo 'not end of file'
}
?>
but I don't know how to check for an end of file in PHP.

Posted: Wed Oct 08, 2003 4:35 pm
by mrvanjohnson
Sorry I'm not an ASP person but are you checking for End of File or End or Records, like from a database. You example would clue me that you are looking through records. If that's the case couldn't you just do this.

Code: Select all

<?php
while ($row = mysql_fetch_array($link) ) { 

      echo 'not end of file' 
} 
echo "End of records";

?>

Posted: Wed Oct 08, 2003 4:47 pm
by Paddy
I did ASP in my first job, shudder, it looks as if your ASP block of code never prints "end of file". Is that true?

Posted: Wed Oct 08, 2003 5:54 pm
by voodoo9055
Ooops,

Paddy, you are corrent. I meant to say how to translate this to PHP

Code: Select all

If rs.EOF Then 
          Response.write "end of file" 
     Else 
          While Not rs.EOF
               Response.write "not end of file" 
               rs.MoveNext 
          Wend 
     End If
mrvanjohnson,
You are right. I meant to the end of a record. Your example will always produce "End of records"

I am currently working on an internal mail script for a website and I want to show a message saying something like "You currently have no mail" instead of just a blank result whenever no mail exist.

In ASP, it is as simple as saying

Code: Select all

If rs.EOF Then 
          Response.write "You currently have no mail"
     Else 
          While Not rs.EOF
               Response.write rs.fields("member")
               Response.write rs.fields("subject")
               Response.write rs.fields("date")
               rs.MoveNext 
          Wend 
     End If

Posted: Wed Oct 08, 2003 7:41 pm
by Stoneguard
Well, I have done ASP for quite a while. I am pretty new to PHP, but loving it so far. The code would be:

Code: Select all

<?php
$row = mysql_fetch_array($result);

if ($row == false) // can use == or === here
   echo "end of file";
else
{ // I would use these braces for clarity
   while( $row)
   {
      echo "not end of file";
      $row = mysql_fetch_array($result);
   }
}
?>

Posted: Wed Oct 08, 2003 8:16 pm
by Paddy
This would produce the same with less code.

Code: Select all

<?php
$row = mysql_fetch_array($result); 
while( $row) 
{ 
      echo "not end of file"; 
      $row = mysql_fetch_array($result); 
} 
echo "end of file"; 
?>
I would have replied earlier but I have been busy. Damn clients. ;)

Posted: Wed Oct 08, 2003 8:28 pm
by Stoneguard
It would produce the same as my code, but it would not work for the desired end result of checking first and notifying the use if there were no mail.

Posted: Wed Oct 08, 2003 8:36 pm
by Paddy
It does, if it fails the while loop on the first go it notifies the user immediately.

Posted: Wed Oct 08, 2003 8:44 pm
by Stoneguard
Sorry, let me be more clear. In his example of how he plans to use the code, the following two blocks are NOT the same:

Block A

Code: Select all

<?php
$row = mysql_fetch_array($result); 
while( $row) 
{ 
      echo $row['member'];
      echo $row['subject'];
      echo $row['date'];
      $row = mysql_fetch_array($result); 
} 
echo "you currently have no mail";
?>
Block B

Code: Select all

<?php
$row = mysql_fetch_array($result); 

if ($row == false) // can use == or === here 
   echo "You currently have no mail"; 
else 
{ // I would use these braces for clarity 
   while( $row) 
   { 
      echo $row['member'];
      echo $row['subject'];
      echo $row['date'];
      $row = mysql_fetch_array($result); 
   } 
} 
?>
Block A will ALWAYS produce the message " You currently have no mail", even if you do have mail. Block B will not produce this message if there is mail. (mail here being an assumed set of entries returned by the unseen query)

Posted: Wed Oct 08, 2003 9:09 pm
by Paddy
Ah true. :) Sorry I should have looked more closely.

Posted: Wed Oct 08, 2003 9:55 pm
by voodoo9055
I got it to work. Thanks a lot guys.