while ($row = mysql_fetch_array($result))??????????

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

while ($row = mysql_fetch_array($result))??????????

Post by ddragas »

Hi all

I have problem with looping trough database, and creating year calendar of booking.

I know how to create calendar, but part that is confusing me is where to put
"while ($row = mysql_fetch_array($result))".

Here is complete code

Code: Select all

<?
$slobodno = " bgcolor=\"#00FF00\"";
$mogucnost = " bgcolor=\"#FFFF00\"";
$zauzeto = " bgcolor=\"#FF0000\"";

$font = "<font color=\"#000000\" size=\"2\" face=\"Times New Roman, Times, serif\">";
$godina="2005";

$smjestaj = '3521876818';

echo "<table width=\"70%\"  border=\"1\" align=\"center\">";
echo "<tr>";
echo "<td width=\"5%\" align=\"center\" bgcolor=\"#3399FF\">$font Mjesec</td><td colspan=\"31\" bgcolor=\"#3399FF\"><div align=\"center\" > $font Dani</div></td></tr>";

include("stranice/con_db.php");

	$result = mysql_query("select * from rezervacije where smjestaj ='$smjestaj'") or
	die (mysql_error());
	


	//while ($row = mysql_fetch_array($result)) //< ----------- WHERE TO PUT THIS LINE???

for ($i = 1; $i <= 12; $i++) 
{

    echo "<td align=\"center\" bgcolor=\"#3399FF\">" . $font . $i . "</td>";
    $broj_dana = cal_days_in_month (CAL_GREGORIAN, $i, $godina);
  
            for ($i1 = 1; $i1 <= $broj_dana; $i1++) 
				{	
					
					$dat_od = $row["od"];
					$dat_do = $row["do"];
					$status = $row["status"];



                	$datum11 = mktime(0, 0, 0, $i, $i1, $godina);
          
                	if (($datum11 >= $dat_od) and ( $datum11 <= $dat_do) and ($status == "zainteresiranost" )):
						
						$ispis = "<td width=\"4%\" align=\"center\"  $mogucnost> $font  $i1</td>";
						
						
					elseif	(($datum11 >= $dat_od) and ( $datum11 <= $dat_do) and ($status == "rezervacija" )):
						
						$ispis = "<td width=\"4%\" align=\"center\"  $zauzeto> $font  $i1</td>";
						
               		else:
						
						$ispis = "<td width=\"4%\" align=\"center\"  $slobodno>$font  $i1</td>";
					
					endif;
					
					echo $ispis;
					
                 }
                echo "</tr>";
}

		mysql_free_result($result);
		include("stranice/disconect.php");

echo "</table>";
echo "<br>";
echo $status;
echo "<table width=\"30%\"  border=\"1\" align=\"center\">";
echo "<td width=\"10%\" align=\"center\"  $mogucnost> $font  Interes postoji</td>";
echo "<td width=\"10%\" align=\"center\"  $slobodno> $font  Slobodno</td>";
echo "<td width=\"10%\" align=\"center\"  $zauzeto> $font  Zauzeto</td></tr>";
echo "</table>";
 
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

What does it do if you insert that 'while' line right where you've already got it?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

lol good point, just try it..

to be honest, you should know already exactly where that code goes seeing as you have written it, seems a lil' weird to me :?

i could help you, but the key is to learn from your own mistakes
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

pickle wrote:What does it do if you insert that 'while' line right where you've already got it?
It drows year calendars as meny records are in database, and I want one year calendar
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

malcolmboston wrote:lol good point, just try it..

to be honest, you should know already exactly where that code goes seeing as you have written it, seems a lil' weird to me :?

i could help you, but the key is to learn from your own mistakes
I agree with you about learning, and everything I've learned untill now I've learned thanks to this forum, and you folks.

I wouldn't ask for help if I don't need it.

litle help would be aprecheated
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

So is your desired layout to have one big table with 1 row, and each cell in that row have a calendar for a given month? If that's the case, I think you should put the while loop inside the for loop. Logically, what you want to do is:
1) Loop through each month
2) For each iteration, gather all the information from the db about that month
3) Generate a table using that data

It's a little hard to understand the code due to the foreign nature of the variable names, but that can't be helped I guess.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

What I want to do is make a year calendar of booking, loop trough database for ID of accommodation, and dates, and if date (record in database) is reserved mark that day in red, if for date (record in database) interes exists, mark that day in yelow, and if date is free, mark that day in green.

example:
http://www.croatia-tourist.biz/reservation.php


also code with variables in english

Code: Select all

<?
$free = " bgcolor=\"#00FF00\"";
$interest = " bgcolor=\"#FFFF00\"";
$reserved = " bgcolor=\"#FF0000\"";
 
$font = "<font color=\"#000000\" size=\"2\" face=\"Times New Roman, Times, serif\">";
$year="2005";
 
$accommodation = '3521876818';
 
echo "<table width=\"70%\"  border=\"1\" align=\"center\">";
echo "<tr>";
echo "<td width=\"5%\" align=\"center\" bgcolor=\"#3399FF\">$font Mjesec</td><td colspan=\"31\" bgcolor=\"#3399FF\"><div align=\"center\" > $font Dani</div></td></tr>";
 
include("stranice/con_db.php");
 
    $result = mysql_query("select * from rezervacije where smjestaj ='$accommodation'") or
    die (mysql_error());
    
 
 
    //while ($row = mysql_fetch_array($result)) //< ----------- WHERE TO PUT THIS LINE???
 
for ($i = 1; $i <= 12; $i++) 
{
 
    echo "<td align=\"center\" bgcolor=\"#3399FF\">" . $font . $i . "</td>";
    $no_of_days = cal_days_in_month (CAL_GREGORIAN, $i, $year);
  
            for ($i1 = 1; $i1 <= $no_of_days; $i1++) 
                {    
                    
                    $date_from = $row["od"];
                    $date_to = $row["do"];
                    $status = $row["status"]; // retrive status information (free, reserved, interes exist)
 
 
 
                    $datum11 = mktime(0, 0, 0, $i, $i1, $year);
          
                    if (($datum11 >= date_from) and ( $datum11 <= $date_to) and ($status == "zainteresiranost" )):
                        
                        $ispis = "<td width=\"4%\" align=\"center\"  $interest> $font  $i1</td>";
                        
                        
                    elseif    (($datum11 >= $date_from) and ( $datum11 <= $date_to) and ($status == "rezervacija" )):
                        
                        $ispis = "<td width=\"4%\" align=\"center\"  $reserved> $font  $i1</td>";
                        
                       else:
                        
                        $ispis = "<td width=\"4%\" align=\"center\"  $slobodno>$font  $i1</td>";
                    
                    endif;
                    
                    echo $ispis;
                    
                 }
                echo "</tr>";
}
 
        mysql_free_result($result);
        include("stranice/disconect.php");
 
echo "</table>";
echo "<br>";
echo "<table width=\"30%\"  border=\"1\" align=\"center\">";
echo "<td width=\"10%\" align=\"center\"  $interest> $font  Interes exist</td>";
echo "<td width=\"10%\" align=\"center\"  $free> $font  Free</td>";
echo "<td width=\"10%\" align=\"center\"  $reserved> $font  Reserved</td></tr>";
echo "</table>";
 
?>
Last edited by ddragas on Thu Apr 14, 2005 3:51 pm, edited 2 times in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Here's some pseudo-code which should show the logic:

Code: Select all

setup_table();
for($i=1;$i<=12;++$i)
{
 setup_row();

 $database_results = query_database_for_current_month();
 while(can_get_another_row($database_results...))
 {
   if($current_day_in_month_is_booked)
   {
     make_red_cell();
   }
   if($current_day_in_month_is_free)
   {
     make_green_cell();
   }
 }
 close_row();
}
close_table();
You can see where I put a while loop.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

please correct it in my code.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

give them fish and they'll have food for a day
learn them how to fish and they'll have food every day
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I think it'd be quicker if you corrected it yourself. You know the logic of your code the best, and I'm sure you can understand the logic of my code - it's pretty straightforward. Without looking too close, the while loop needs to be inside your for loop.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

thank you folks

It works now

chears ddragas
Post Reply