Jump to content

[SOLVED] Loop w/ Function


Lamez

Recommended Posts

I have this code here, it is a function to see if the user is in one table and in the other table.

 

anyways I have a function in the middle of the loop, I can only declare it once, but since it is a loop, it re-declares the function.

 

here is my code:

  <?
$qr=mysql_query("select * from users order by first");
$pt=mysql_query("select * from paid order by user");
while ($rs=mysql_fetch_assoc($qr)) {
$pu=mysql_fetch_assoc($pt);
$table = 'paid';
$field = 'user';
$compared = $pu['user'];

function checkUnique($table, $field, $compared){
if (get_magic_quotes_gpc()) {
$table = stripslashes($table);
$field = stripslashes($field);
$compared = stripslashes($compared);
}
$table = mysql_real_escape_string($table);
$field = mysql_real_escape_string($field);
$compared = mysql_real_escape_string($compared);

$result = mysql_query("SELECT $field FROM $table WHERE $field = '$compared'");
if(mysql_num_rows($result)==0) {
return TRUE;
echo "Paid";
}
else {
return FALSE;
echo "Not Paid";
}
} 

 

How do I fix this to only declare once?

 

I get this error:

Fatal error: Cannot redeclare checkunique() (previously declared in /mounted-storage/home48c/sub007/sc33591-LWQU/lamezz.info/_admin/bracket/paid.php:25) in /mounted-storage/home48c/sub007/sc33591-LWQU/lamezz.info/_admin/bracket/paid.php on line 25

Link to comment
https://forums.phpfreaks.com/topic/90002-solved-loop-w-function/
Share on other sites

You have to invoke the function from within the loop.

 

Also, the "echo" statement in the function after the return statements will not be executed, since the return statement ends the execution of the function.

 

Looking at the function, I don't think you really need a to use a function here, just make the code in-line.

 

<?php
$qr=mysql_query("select * from users order by first");
$pt=mysql_query("select * from paid order by user");
while ($rs=mysql_fetch_assoc($qr)) {
       $pu=mysql_fetch_assoc($pt);
       $table = 'paid';
       $field = 'user';
       $compared = $pu['user'];
       $result = mysql_query("SELECT $field FROM $table WHERE $field = '$compared'");
       if(mysql_num_rows($result)==0) {
             echo "Paid";
       } else {
             echo "Not Paid";
       }
}
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.