Page 1 of 1

mysql -> array question

Posted: Fri Feb 22, 2008 8:07 am
by bettor
Mod | Please use [ php], [ code ] and [ syntax="..." ] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi all,

I am new to php and mysql and I am trying to create a calender with a checkbox next to each number. In addition, I am trying to make a check whether a day has been booked and to display the calendar checkbox checked if it is. So what i do is a function where I put the result from the DB into an array but I am having a problem using this array outside the function for an external check. here is some code to see what I am doing wrong:

Code: Select all

 
function res(){
$sql = "SELECT * FROM calendar_field";
$query = mysql_query($sql);
if ($query){
while($row = mysql_fetch_assoc($query)) {
$check[] = $row['date'];
            }
return $check;
            }
return true;
}
$checkdate = res();
print_r($checkdate); // just to see what it returns
for ($i=1; $i<=$days; $i++){
?>
<td class='cal'><?php echo $i; ?><br /><input type='checkbox' name='month[<?php echo $i; ?>]'
<?php
            if ($i == $checkdate) {
            echo " checked /></td>";
            }else{
echo "/></td>";}
}
 
everything I think works except for

Code: Select all

if ($i == $checkdate)
.... it doesn't check anything. Any help is welcome. Thanks


Mod | Please use [ php], [ code ] and [ syntax="..." ] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: mysql -> array question

Posted: Sat Feb 23, 2008 6:28 am
by EverLearning

Code: Select all

 
if ($i == $checkdate)
 
won't work because you're comparing an integer value(ranging from 1 to 28-31, depending on month) with the whole array of dates(I asume they're dates from your code) returned from the res().

What you can do, is limit your query to one month(ex. february) and then compare $i with the day part of the date pulled from the database. You'll need to change the returning data in your function so it collects dates like this

Code: Select all

 
$check[date("j", $row['date'])] = $row['date'];
 
In the snippet above, i'm asummng that the date in the database is in timestamp form, if its a DATE type, that you can use

Code: Select all

date("j", strtotime($row['date']))
as a key in the $check array.
The key is added so in the printing days loop bellow, you don't have to loop through the whole $checkdate array for every day of the month to see if the day is checked. You now check if the date is checked :) like this

Code: Select all

 
$days = 29; //february
for ($i=1; $i<=$days; $i++){
?>
<td class='cal'>
<?php echo $i; ?><input type='checkbox' name='month[<?php echo $i; ?>]' <?php
            if (isset($checkdate[$i])) {
            echo " checked ";
}
echo "/></td><br />";
}
 

Re: mysql -> array question

Posted: Sat Feb 23, 2008 2:45 pm
by bettor
Thanks EverLearning.

There is something very weird in my code that i don't understand. Here it is:

Code: Select all

for ($i=1; $i<=$days; $i++){
 
if ($i == $check[1]) {
    echo "<td class='cal'>" . $i . "<br /><input type='checkbox' name='month[" . $i . "]' checked /></td>";
    
    }else{
    echo "<td class='cal'>" . $i . "<br /><input type='checkbox' name='month[" . $i . "]' /></td>";
        }
}

When I write

Code: Select all

if($i == $check[1])...
it works and checks for this particular array whether we have equality. However, when I code it like this:

Code: Select all

if($i == $check[$i])...
it doesn't work. I don't know why my script does not want to read $i in my array so it can loop through all the arrays to see if there is a $key and display if it is on.
The check array comes from a function which pools out the reserved dates and is an integer(1-29/31).

Re: mysql -> array question

Posted: Sun Feb 24, 2008 6:49 am
by EverLearning
bettor wrote:The check array comes from a function which pools out the reserved dates and is an integer(1-29/31).
What are the keys for the $check array, since your code

Code: Select all

if($i == $check[$i])...
checks to see if $i is equal to the $check array element whose key is $i so if

$i=5

it checks to see if 5 == $check[5]. Since you say that your $check array contains integers(1-29/31), when you populate your array just do it something like this:

Code: Select all

$check[$day] = $day;
and then your check

Code: Select all

if($i == $check[$i])...
should work.

Re: mysql -> array question

Posted: Mon Feb 25, 2008 1:54 am
by bettor
Hi,

Thats what I tried but for some reason was not working. However, I found this nice php function in_array which worked for me like this:

Code: Select all

if(in_array($i, $check)) {....do some code
Thanks for your help EverLearning