number 1 problem

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
hasanoca
Forum Newbie
Posts: 9
Joined: Fri Mar 05, 2010 4:42 am

number 1 problem

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

?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: number 1 problem

Post 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.
(#10850)
hasanoca
Forum Newbie
Posts: 9
Joined: Fri Mar 05, 2010 4:42 am

Re: number 1 problem

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: number 1 problem

Post 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.
(#10850)
hasanoca
Forum Newbie
Posts: 9
Joined: Fri Mar 05, 2010 4:42 am

Re: number 1 problem

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: number 1 problem

Post 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.
(#10850)
hasanoca
Forum Newbie
Posts: 9
Joined: Fri Mar 05, 2010 4:42 am

Re: number 1 problem

Post by hasanoca »

Here is the only table that I have. Eczane=pharmacy, zaman=time
Image
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: number 1 problem

Post 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';
(#10850)
hasanoca
Forum Newbie
Posts: 9
Joined: Fri Mar 05, 2010 4:42 am

Re: number 1 problem

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: number 1 problem

Post 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 :-(
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: number 1 problem

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply