Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eval() and database query

Status
Not open for further replies.

ingernet

Programmer
Feb 5, 2001
68
US
Hi folks,

I have a situation where I'm about to do a batch-add of users to a database, and I don't have all of their vital stats. When they log in, I'm having the site do a query for their db records and if they're missing info (like fax number, company name, etc), I want the users to be sent to the "personal info" page, where they update their personal info.

I would just do a really kludgy "if $row[1] =='', send them to a the personal info page; if $row[2] == '', ..." check, but i just KNOW that there's an elegant way to do it to do with a foreach()/while()/eval() loop, that will allow for any number of database fields so that I can reuse the code someday.

Anyone have any ideas?

Thanks,
Inger

 
First, eval is a function and not a loop.
What you could do is this:
Code:
foreach ($row as $fieldname => $fieldvalue)
{
    if (empty($fieldvalue))
    {
        // This field was empty.
        echo &quot;$fieldname was empty. Sending you to the personal info page...\n<br />&quot;;
    }
}
This would loop through the record and see if there are any empty fields. //Daniel
 
Hrmm, well that gave me errors, so I played with it a bit, and came up with this:

Code:
$myquery = &quot;select * from USERS where username = 'ingernet'&quot;;
$link = mysql_query($myquery) or die (&quot;couldn't run query!&quot;);

while ($row = mysql_fetch_assoc($link)) {

    foreach ($row as $fieldname => $fieldvalue) {
    echo $fieldvalue;
    if (empty($fieldvalue)) {
          // This field was empty.
          echo &quot;$fieldname was empty. Sending you to the personal info page...\n<br />&quot;;
        }
}
...and that seemed to work.

Thanks for the head start!

Inger
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top