Forum Moderators: coopster

Message Too Old, No Replies

how to use if condition in this php script?

how to use if condition in this php script?

         

shams

1:54 pm on May 22, 2006 (gmt 0)

10+ Year Member



hi,
This is a table treatment for patients taking thier grugs regulary each month for some months, this script will get the drug name and the period of treatment in months as input and output the, amount of drug each patient need for his remaning mounts, obviously the script should first exclude that patients they completed thier period of treatment, the output of this script is:
ATT Completed
that is not correct, still there are many patients to take the drugs:
<?php
error_reporting(E_ALL);
$drug = $_POST['drug'];
$period = $_POST['period'];
mysql_connect("localhost","root") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());

$query = "SELECT name,$drug,count($drug) as needed FROM treatment group by name,$drug";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))
{
if($row[$drug] < $period) {
echo $row['name']." need more ".(($period - $row['needed']) * $row[$drug])." tables of ".$drug;
echo "<br />";
} else { echo "ATT Completed";}
}
?>

nfs2

2:01 pm on May 22, 2006 (gmt 0)

10+ Year Member



Maybe change

if($row[$drug]

to

if($row['$drug']

just a guess..

Habtom

2:05 pm on May 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

$query = "SELECT name,$drug,count($drug) as needed FROM treatment group by name,$drug";

Aren't the table fields fixed? Why $drug;

If you rewrite the query to:
$query = "SELECT name,drug,count(drug) as needed FROM treatment group by name,drug";

Then you might use to $row[drug] to display them.

But explain why you want the $drag to be variable, and I believe that is where the problem lies.

Habtom

shams

2:52 pm on May 22, 2006 (gmt 0)

10+ Year Member



thanks for replies,
hi Habtom,
drug is not the name of column in the table, but $drug will get the the name of drugs like PZA, RH etc which are columns names in the table, this script work fine without if condition but the problem is cannot exclude the patients who completed the treatment period.

nfs2

3:28 pm on May 22, 2006 (gmt 0)

10+ Year Member



What i dont get is

if($row[$drug] < $period) {

i assume you mean $row[$drug] is a word, right? Like the name of a drug? And i assume $period is a numerical value, because it seems you are mesuring a word against a number

I think you want the number of rows for the drug right?

Try

while($row = mysql_num_rows($result))
{
if($row['drug'] < $period) {
echo $row['name']." need more ".(($period - $row['needed']) * $row['drug'])." tables of ".$drug;
echo "<br />";
} else { echo "ATT Completed";}
}

Habtom

12:03 pm on May 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try echoing $row[$drug] and see what ouput it holds.

//With test echo
while($row = mysql_fetch_array($result))
{
echo $row[$drug];
if($row[$drug] < $period) {
echo $row['name']." need more ".(($period - $row['needed']) * $row[$drug])." tables of ".$drug;
echo "<br />";
} else { echo "ATT Completed";}
}

Hab

pmlb

4:06 pm on May 23, 2006 (gmt 0)



Hi,
If I understand, treatment table has a column 'name' which contains the patient's name and some more columns for instance 'PZA', 'RH', and so on, each one containing a number.
Let's take an example with PZA:
"SELECT name, PZA, count(PZA) as needed FROM treatment group by name, PZA"

1) count(PZA) gives a number of records (the number of records in the table for each patient and each PZA value); if there is one record for each patient, count(PZA) will always be 1; if there are 2 records for patient 'JD', and PZA values are the same, count(PZA) will be 2; if you have more than one record by patient you probably want to use sum(PZA); if you have one record by patient "SELECT name, PZA FROM treatment" is enough

2) if your script just outputs "ATT Completed " once, that means your query returns only one record with PZA value greater than $period received

3) as you do not give an ORDER clause in your query, it is unlikely that that patients having completed their period of treatment should first be excluded

Hope it will help.