Page 1 of 1
Help needed with intranet project
Posted: Wed Mar 24, 2004 9:47 am
by Friends
Hello there, and thanx for reading.
I am building a Intranet app. for a bowling alley. The idea is to make reservations thrue the intranet using PHP and MySQL.
Its already working... and they use it with great pleasure.. however its SLOW as hell (5 seconds execution time and more)
This is what it looks like:
As you can see, its just a table.. it fills the TD's with reservations, and the little arrows are to make one at a time and a lane. As you can see it already uses COLSPAN to create a larger reservationblock, but doesnt seem to work well with ROWSPAN (i really want that to work aswell)
The script is bloated. I used ALOT of whiles and ifs to make this work.. at one point there are 5 or 6 loops running at one time. (:( ) anyways.. i need to get this faster..
This is what the database table with reservations looks like:
| ID | ClientID | Date | Time (used as 2-2.5-3-3.5 for 14:00 to 16:00) | Lanes (looks like 1-2-3-4 for the first 4 lanes) |
Hope you can help me with making a whole new function to get the data out of the database, and parse a TABLE with the reservations!
Thanx in advance..
Posted: Wed Mar 24, 2004 9:52 am
by pickle
Maybe post the function so we can all see where it can be optimized.
Posted: Wed Mar 24, 2004 9:55 am
by Friends
ghehe sure thing, but i have to warn you.. this thing drives you crazy.. thats why i want a new one..
Code: Select all
<?
$baanteller=1;
$loadbaanteller=0;
for($baanteller=1; $baanteller<=$banenaantal; $baanteller++) {
$urenteller=1;
//echo("</TR><TR HEIGHT=40><TD ALIGN=CENTER><B>$baanteller</B></TD>");
echo("</TR><TR HEIGHT=40><TD ALIGN=CENTER bgcolor=#458AF7 CLASS="banen"><b><font color=#FFFFFF>$baanteller</b></TD>");
for($urenteller=1; $urenteller<=$urenaantal; $urenteller+0) {
$geendata = 1;
$colspanteller = 0;
$sql="SELECT * FROM data WHERE dataDatum = '$date' ORDER BY sortfield DESC";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result)) {
unset($loadbaan);
unset($loadtijd);
$loadbaan = explode("-", $row["dataBanen"]);
asort($loadbaan);
$loadtijd = explode("-", $row["dataTijdvan"]);
asort($loadtijd);
unset($loadtijdteller);
$loadtijdteller=0;
while ($loadtijd[$loadtijdteller] != "") {
if ($loadtijd[$loadtijdteller] == $urenteller) {
$loadbaanteller=0;
while ($loadbaan[$loadbaanteller] != "") {
if ($loadbaan[$loadbaanteller] == $baanteller) {
//JA we hebben een reservering op deze tijd op deze baan
//Hebben we over een half uur nog steeds deze boeking?
//Ja? Niets Plaatsen COLSPAN omhoog
//Nee? Plaatsen met de juiste COLSPAN
$colspanteller=0;
while ($loadtijd[$loadtijdteller] == $urenteller) {
$colspanteller++;
$loadtijdteller++;
$urenteller = $urenteller + 0.5;
$klantID = $row["dataKlantID"];
$specs = $row["dataSpecsID"];
$dataID = $row["dataID"];
$geendata = 0;
$telopdiehandel = 0;
}
}
$loadbaanteller++;
}
}
if ($telopdiehandel == 0) {
$telopdiehandel = 1;
}
else {
$loadtijdteller++;
}
}
}
if ($geendata == 0) {
//echo("</TD>");
$sql2="SELECT * FROM klanten WHERE klantID = ".$klantID;
$result2=mysql_query($sql2);
while($row2 = mysql_fetch_array($result2)) {
$klantBedrijf = $row2["klantBedrijf"];
$klantNaam = $row2["klantNaam"];
$klantTel = $row2["klantTel"];
}
$loadspecsteller=0;
$specsKleur = "";
if($specs != "") {
$loadSpecsID=explode("-", $specs);
while ($loadSpecsID[$loadspecsteller] != "") {
$sql3="SELECT * FROM specs WHERE specsID = $loadSpecsID[$loadspecsteller]";
$result3=mysql_query($sql3);
$specsKleur[$loadspecsteller] = mysql_result($result3, "0", "specsKleur");
$specsOms[$loadspecsteller] = mysql_result($result3, "0", "specsOms");
$loadspecsteller++;
}
}
$loadspecsteller2 = 0;
$randtoevoeg = "";
$specsbackground = "";
while ($loadspecsteller2 <= $loadspecsteller) {
if ($specsKleur[$loadspecsteller2] != "") {
if ($specsbackground == "") {
$specsbackground = $specsKleur[$loadspecsteller2];
}
else {
$randtoevoeg = $randtoevoeg . "<img src=images/$specsKleur[$loadspecsteller2].gif alt=".addslashes($specsOms[$loadspecsteller2])."> ";
}
}
$loadspecsteller2++;
}
if ($specsbackground != "") {
$fontcolor="#FFFFFF";
$editbutton = "editdark.gif";
$deletebutton = "deletedark.gif";
}
else {
$fontcolor="#000000";
$specsbackground = "#ECECEC";
$editbutton = "editlight.gif";
$deletebutton = "deletelight.gif";
}
if ($klantBedrijf != "") {
$klant = $klantBedrijf;
}
else {
$klant = $klantNaam;
}
echo("<TD ALIGN=CENTER COLSPAN=$colspanteller BGCOLOR=$specsbackground onDblClick="window.open('details.php?id=$dataID', 'newWindow', 'scrollbars=1,resizable=0,height=420,width=320,alwaysRaised')">$randtoevoeg<a href=wijzig.php?id=$dataID&year=$year&today=$today&month=$month><img src=images/$editbutton align=left border=0></a><br><font size=1 color=$fontcolor>$klant<br>$klantTel</font></TD>\n");
}
else {
$addbaanselect = "";
if (intval($urenteller / 1) == ($urenteller / 1)) {
$addbaanselect = "<a href="addbaanselect.php?year=$year&today=$today&month=$month&starttijd=$urenteller&startbaan=$baanteller"><img src="images/document.gif" border="0"></a>";
}
echo("<TD ALIGN=left VALIGN=top WIDTH="30">$addbaanselect</TD>\n");
$urenteller=$urenteller+0.5;
}
// if ($geendata == 1) {
// $urenteller=$urenteller+0.5;
// }
}
// }
//$baanteller++;
}
?>
?>
Posted: Wed Mar 24, 2004 9:58 am
by magicrobotmonkey
huh variables in other languages are hard to read!
Posted: Wed Mar 24, 2004 10:25 am
by pickle
1) Change your date format to UNIX timestamp. That will eliminate the need to explode the date variable, and sort it.
[php_man]date[/php_man]
[php_man]time[/php_man]
2) Only assign variables inside loops that generate their values. ie:
Code: Select all
while($row = mysql_fetch_array($result)) {
unset($loadbaan);
unset($loadtijd);
$loadbaan = explode("-", $row["dataBanen"]);
asort($loadbaan);
$loadtijd = explode("-", $row["dataTijdvan"]);
asort($loadtijd);
unset($loadtijdteller);
$loadtijdteller=0;
//variables commented below only need to be set here
while ($loadtijd[$loadtijdteller] != "") {
if ($loadtijd[$loadtijdteller] == $urenteller) {
$loadbaanteller=0;
while ($loadbaan[$loadbaanteller] != "") {
if ($loadbaan[$loadbaanteller] == $baanteller) {
$colspanteller=0;
while ($loadtijd[$loadtijdteller] == $urenteller) {
$colspanteller++;
$loadtijdteller++;
$urenteller = $urenteller + 0.5;
// these variables only need to be set once in the
// location marked
$klantID = $row["dataKlantID"];
$specs = $row["dataSpecsID"];
$dataID = $row["dataID"];
This will save them from being set inside four loops rather than one.
It looks like you're trying to accomplish quite a bit with this one bit. I would highly suggest breaking up some of the different logic into different functions. That makes it much easier to read, debug, and optimize. Further, since the variables are in another language (German or Dutch?) it would certainly help me if you had inline comments to explain what you're doing, as it won't be obvious to many of us, just by looking at the code.
Posted: Wed Mar 24, 2004 10:34 am
by JayBird
as pickle said, it looks like you ar trying to do an awful lot all in one go. You really need to split your code into smaller chunks
I had a similar layout to the one you show for a calendar. Take a look at this thread to see how we shortened the code. May difficult to understand though -
viewtopic.php?t=16491&highlight=head+month
Mark