Page 1 of 1

number 1 problem

Posted: Sun Apr 04, 2010 4:58 am
by hasanoca
Hello
I am new to php and it took me hours to write a script in order to display the duty nightpharmacies in my town in accordance with the day of the week.
Although it is now complete and seems to be working I have a problem. Above the table showing the pharmacy, its phone number and address, there appears number "1" with no reason. You can see it yourself at http://dene.paniad.org/1111.php.
Here is the code
<?php
mysql_connect("localhost", "db-user", "password") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
mysql_query("SET NAMES 'latin5'");
echo "<table border='1'>";

$s=date("H");
$d=date("D");

if ($d=="Mon" && $s<="8") {
echo $result = mysql_query("SELECT * FROM nobetci_pazarcik
WHERE id='4'") or die(mysql_error());
}
elseif ($d=="Mon" && $s>"8") {
echo $result = mysql_query("SELECT * FROM nobetci_pazarcik
WHERE id='7'") or die(mysql_error());
}
elseif ($d=="Tue" && $s<="8") {
echo $result = mysql_query("SELECT * FROM nobetci_pazarcik
WHERE id='7'") or die(mysql_error());
}
elseif ($d=="Tue" && $s>"8") {
echo $result = mysql_query("SELECT * FROM nobetci_pazarcik
WHERE id='8'") or die(mysql_error());
}
elseif ($d=="Wed" && $s<="8") {
echo $result = mysql_query("SELECT * FROM nobetci_pazarcik
WHERE id='8'") or die(mysql_error());
}
elseif ($d=="Wed" && $s>"8") {
echo $result = mysql_query("SELECT * FROM nobetci_pazarcik
WHERE id='6'") or die(mysql_error());
}
elseif ($d=="Thu" && $s<="8") {
echo $result = mysql_query("SELECT * FROM nobetci_pazarcik
WHERE id='6'") or die(mysql_error());
}
elseif ($d=="Thu" && $s>"8") {
echo $result = mysql_query("SELECT * FROM nobetci_pazarcik
WHERE id='1'") or die(mysql_error());
}
elseif ($d=="Fri" && $s<="8") {
echo $result = mysql_query("SELECT * FROM nobetci_pazarcik
WHERE id='1'") or die(mysql_error());
}
elseif ($d=="Fri" && $s>"8") {
echo $result = mysql_query("SELECT * FROM nobetci_pazarcik
WHERE id='2'") or die(mysql_error());
}
elseif ($d=="Sat" && $s<="8") {
echo $result = mysql_query("SELECT * FROM nobetci_pazarcik
WHERE id='2'") or die(mysql_error());
}
elseif ($d=="Sat" && $s>"8") {
echo $result = mysql_query("SELECT * FROM nobetci_pazarcik
WHERE id='5'") or die(mysql_error());
}
elseif ($d=="Sun" && $s<="8") {
echo $result = mysql_query("SELECT * FROM nobetci_pazarcik
WHERE id='5'") or die(mysql_error());
}
elseif ($d=="Sun" && $s>"8") {
echo $result = mysql_query("SELECT * FROM nobetci_pazarcik
WHERE id='4'") or die(mysql_error());
}

$row = mysql_fetch_array( $result ); {
echo "<tr><td>";
echo $row['eczane']." - ".$row['telefon'];
echo "</tr></td>";
echo "<tr><td>";
echo $row['adres'];
echo "</tr></td>";
}

echo "</table>";

?>

Re: number 1 problem

Posted: Sun Apr 04, 2010 12:06 pm
by Christopher
You are echo()ing $result in all the if()s. One is displaying the value of $result which is a result ID.

PS - You should think of putting the logic into the SQL instead of all those if()s.

Re: number 1 problem

Posted: Sun Apr 04, 2010 12:46 pm
by hasanoca
Christopher wrote:You are echo()ing $result in all the if()s. One is displaying the value of $result which is a result ID.

PS - You should think of putting the logic into the SQL instead of all those if()s.
I tried not to repeat $result but I didn't know how to place it. Would you suggest a way to do it?

By the way, do you mean I can schedule timed jobs with SQL?

Re: number 1 problem

Posted: Sun Apr 04, 2010 12:52 pm
by Christopher
Put the data/time information for each pharmacy in the database and use a single select to find all pharmacies for the current data/time.

Re: number 1 problem

Posted: Sun Apr 04, 2010 1:22 pm
by hasanoca
Christopher wrote:Put the data/time information for each pharmacy in the database and use a single select to find all pharmacies for the current data/time.
I've added date/time into the database. Can you tell me how to use a "single" select for the current time? Can you write an example line?

Re: number 1 problem

Posted: Sun Apr 04, 2010 1:38 pm
by Christopher
What did you put in your database? You probably need two tables -- pharmacy and pharmacy_hours. You would search for matches where the current date/time are between the open/close time in a pharmacy_hours record. You would join with the pharmacy table to get the pharmacy information.

Re: number 1 problem

Posted: Sun Apr 04, 2010 2:05 pm
by hasanoca
Here is the only table that I have. Eczane=pharmacy, zaman=time
Image

Re: number 1 problem

Posted: Sun Apr 04, 2010 2:27 pm
by Christopher
Create a second table with open and close times like:

[text]id pharmacy_id day open_time close_time
1 1 Mon 08:00 17:00
2 1 Tue 08:00 12:00
3 2 Mon 08:00 18:00
4 2 Tue 12:00 18:00[/text]

Then:

SELECT pharmacy.Eczane, pharmacy_scheule.* FROM pharmacy_schedule JOIN pharmacy ON pharmacy_schedule.pharmacy_id=pharmacy.id WHERE pharmacy_schedule..day='$day' AND pharmacy_schedule.open_time>='$time' AND pharmacy_schedule.close_time<='$time';

Re: number 1 problem

Posted: Sun Apr 04, 2010 2:39 pm
by hasanoca
Thank you very much for your help. While waiting I tried something different. I removed "echo"s from the beginnings of the lines and number 1 disappeared. I am not sure if it will work like this. Let's wait and see. There are 8 pharmacies and a different one is on duty each day. The order changes every week. So, I am planning to change the id's every week.

Re: number 1 problem

Posted: Wed Apr 07, 2010 2:44 am
by josh
hasanoca wrote: The order changes every week. So, I am planning to change the id's every week.
That is a poor idea, you should add a column called `order` and ORDER BY `order` to your queries.

Imagine if your phone number & address changed every week. All these IDs and numbers in our lives are confusing enough. No need to switch them up every week.

It would especially be bad if someone posted an order for pharmacy # 3 but at the same time you happened to be changing the IDs, and that person never got their medecine :-(

Re: number 1 problem

Posted: Wed Apr 07, 2010 9:49 am
by pickle
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:1. Select the correct board for your query. Take some time to read the guidelines in the sticky topic.
Moved.