Forum Moderators: coopster

Message Too Old, No Replies

While loop error? showing specific days as a link on a calendar

         

nshack31

5:48 pm on Jan 23, 2005 (gmt 0)

10+ Year Member



I have a calendar and i want the dates that contain a match to display as a hyper link. It works, BUT it is only showing one day (the first record in the database). Clearly my while loop is only selecting one record from the DB but i need it to display all records......

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$dbday=$row['day'];
$dbmonth=$row['month'];
$dbyear=$row['year'];

$link = "";
if ($day == $dbday && $month == $dbmonth && $year == $dbyear)
{
$link = "displaymatches.php?day=$day&month=$month&year=$year";
}
return $link;
}
include 'close.php';
}

The value that works is the first record in my table of "29". So 29 links correctly on the calendar, but my DB wont show the other days as a link. Any ideas please?!
Thanks

mcibor

8:30 pm on Jan 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you post the sql query? If the while is processed only once, there may lay the problem. Where do you display all days?

Try to see how many times loop is done:

cnt = 0;
while(...)
{
...
cnt++;
}

echo "<br>Counter: ".$cnt;

Also see, if you're correctly entering into the if clause. Why do you display only one displaylink? Where are you "return"ing from, when you don't have any function?

Hope these questions add some light to what you're trying to achieve.

nshack31

10:58 pm on Jan 23, 2005 (gmt 0)

10+ Year Member



thanks for your reply! I'll have a look at this tomorrow and post some more code if needs be!

nshack31

4:47 pm on Jan 24, 2005 (gmt 0)

10+ Year Member



ok! my function is..

function getDateLink($day, $month, $year)
{
include 'open.php';
$query="Select * From challenge";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$dbday=$row['day'];
$dbmonth=$row['month'];
$dbyear=$row['year'];

$link = "";
if ($day == $dbday && $month == $dbmonth && $year == $dbyear)
{
$link = "displaymatches.php?day=$day&month=$month&year=$year";
}
return $link;
}
include 'close.php';
}

This code should be linking dates on the calendar, where there is an entry in the DB. It only links the first result (29th jan 2005) But there are other entries such as (17th jan 2005) which are not appearing on my calendar! :(

I think its the while loop only returning the first record over and over. How can i make it return them all!

thankyou for your time

mcibor

11:05 pm on Jan 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nope, that's not the problem.

The real thing why it isn't working is that variables $day are not being changed in the loop.

eg at first "while" you got $dbday = 29;, and $day id 29;, so you enter the if statement, and write the link.

on the second loop you get $dbday = 17;, but $day is still unchanged and equals 29, so the loop doesn't enter if.

If that's not the problem, then check as I suggested how many times the loop is being done. And also good idea would be to see what the loop is writing under $dbday, $dbmonth and $dbyear

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$dbday .= $row['day']."; ";
$dbmonth .= $row['month']."; ";
$dbyear .= $row['year']."; ";
}
echo $dbday."<br>".$dbmonth."<br>".$dbyear

nshack31

9:23 am on Jan 25, 2005 (gmt 0)

10+ Year Member



ah, i see! thankyou :)

nshack31

10:24 am on Jan 25, 2005 (gmt 0)

10+ Year Member



If i run a counter it correctly display 5 results but it displays it 31 times (which i assume is once for each day)

If i echo dbday."<br>".$dbmonth."<br>".$dbyear ;

It displays

29; 17; 13; 13; 28; <------ days
1; 1; 1; 1; 1; <------- month
2005; 2005; 2005; 2005; 2005; <--- year

So the following days should be lighting up, 29th 17th 13th 13th and 28th but only the 29th lights up! Therefore it is only allowing the 1st record to light up on my calendar. Would this require a new if statement?

Thanks again

nshack31

10:31 am on Jan 25, 2005 (gmt 0)

10+ Year Member



sorted! rather than == i used .=

(im new to using '.' )

Thanyou!

edit.... i got a bit excited :( that didnt work at all. It lit up every single day! DOH!

Ah the $link variable is storing the same value over and over again..

displaymatches.php?day=29&month=1&year=2005displaymatches.php?day=29& month=1&year=2005displaymatches.php?day=29&month=1& year=2005displaymatches.php?day=29&month=1& year=2005displaymatches.php?day=29&month=1&year=2005

hmmm! How do i change it to store the other values? Is a . needed?

[edited by: jatar_k at 5:56 pm (utc) on Jan. 25, 2005]
[edit reason] fixed sidescroll [/edit]

nshack31

3:40 pm on Jan 25, 2005 (gmt 0)

10+ Year Member



is it possible to create a while loop within a while loop? Im just wondering if replacing the If statement with a while loop may solve the problem

nshack31

6:10 pm on Jan 25, 2005 (gmt 0)

10+ Year Member



all sorted, i had to place day, month and year in arrays.

Took me all day mind :(

mcibor

3:01 pm on Jan 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry I didn't write earlier. but I'm glad that it's working now.

Keep on good work!
Michal Cibor