I need a simple translation problem solved: ASP to PHP

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
User avatar
voodoo9055
Forum Commoner
Posts: 51
Joined: Sat Apr 26, 2003 3:27 pm
Location: Montgomery, AL

I need a simple translation problem solved: ASP to PHP

Post 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.
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post 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";

?>
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post 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?
User avatar
voodoo9055
Forum Commoner
Posts: 51
Joined: Sat Apr 26, 2003 3:27 pm
Location: Montgomery, AL

Post 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
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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);
   }
}
?>
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post 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. ;)
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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.
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

It does, if it fails the while loop on the first go it notifies the user immediately.
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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)
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

Ah true. :) Sorry I should have looked more closely.
User avatar
voodoo9055
Forum Commoner
Posts: 51
Joined: Sat Apr 26, 2003 3:27 pm
Location: Montgomery, AL

Post by voodoo9055 »

I got it to work. Thanks a lot guys.
Post Reply