Passing a new value to the table

Passing a new value to the table

Postby MrRundog » Sun Apr 13, 2008 10:03 am

Hi, I'm trying to pass a new value to the table - I have added bookingID and have this passed through my booking sequence with no probs - However - I can't seem to pass this value to the sql table when I click on a day to add -

Cheers
User avatar
MrRundog
 
Posts: 14
Joined: Fri Apr 11, 2008 12:57 pm

Re: Passing a new value to the table

Postby chris » Sun Apr 13, 2008 10:42 am

Hi,
so you want to add a new variable via the ajax - is that right?
This is something that is not included in the calendar script but should not be hard to add.

I wonder how far you have got?

You could do this in one of 2 ways.
1.use a hidden form field to hold the bookingID and then access it via the javascript
or
2. add this id to the function.

TO be honest both methods are pretty much equal, it is probably just a question of preference.

This is how you could do it with a hidden field:

I will assume that you have a hidden something field like this:
Code: Select all
<input type="hidden" id="bookingID" name="bookingID" value="<?php echo $_POST["bookingID"]; ?>">


You will now need to update the update_cal.js
Go to the "ajax_mod_state" function and add this pretty much at the start:
Code: Select all
id_booking=document.getElementById('bookingID').value;

Then we add this new value to the url that is created to call the php ajax file further down:
replace this:
Code: Select all
var the_url=update_url+"?id_item="+id_item+"&the_date="+the_date+"&special_state="+special_state;

with this:
Code: Select all
var the_url=update_url+"?id_item="+id_item+"&the_date="+the_date+"&special_state="+special_state+"&id_booking="+id_booking;

So, if all is well we are now passing the id_booking value to the ajax file (you can check that we have the value by adding alerts to the js)

The final stage would be to modify the update_cal.ajax.php file to add this new value to the db table. I am sure that you can handle this part but, just in case....
Update the "$insert" value to add this value ie:
Code: Select all
$insert="INSERT INTO ".T_BOOKINGS." SET id_item='".$_REQUEST["id_item"]."', the_date='".$_REQUEST["the_date"]."', state='".$_REQUEST["special_state"]."', bookingID='".$_REQUEST["bookingID"]."'";
      

That should be it.
Please note that I haven't tested this code so it might fail on some small syntax error, but the theory should be right. I will give it a go myself later anyway just in case.

Good luck ;)

Chris
User avatar
chris
Site Admin
 
Posts: 1089
Joined: Mon Dec 17, 2007 7:42 pm
Location: Málaga, Spain

Re: Passing a new value to the table

Postby chris » Sun Apr 13, 2008 10:55 am

OK, I just tested it and it worked for me first time so there are no errors ;)

just a note, you could also add the booking id in a visible field so that you could modify it for each day without having to go via a form - check out my test version here: http://www.cbolson.com/code/availabilit ... ersion2.0/ and you will see that I have added a form field where you can write in the booking id manually - you never know, it might be useful.
The same method could easily be adapted for a comments box or for adding the price etc.

Chris
User avatar
chris
Site Admin
 
Posts: 1089
Joined: Mon Dec 17, 2007 7:42 pm
Location: Málaga, Spain

Re: Passing a new value to the table

Postby MrRundog » Sun Apr 13, 2008 11:15 am

Actually - I was attempting to add it via the function,

I have tried you code but it's not picking up the bookingID - Just incase i'm missing the blindingly obvious - where should I have placed that hidden field in your post?
User avatar
MrRundog
 
Posts: 14
Joined: Fri Apr 11, 2008 12:57 pm

Re: Passing a new value to the table

Postby chris » Sun Apr 13, 2008 11:27 am

well, the hidden form field could be anywhere on the page as it is detected by it's "id".
Yes, I have tried it and it worked first time :)

Passing it directly to the function may be easier, just add the bookingID to the end of the list of variables so that in the hrefs it looks like this:
Code: Select all
ajax_mod_state(\''.$id.'\',\''.$this_date.'\',\''.$day_counter.'\',event, \''.$bookingID.'\')

and in the funciton it is detected like this:
Code: Select all
function ajax_mod_state(id_item,the_date,the_num,e,id_booking){   


This way you won't need the hidden field or the extra js line that detects it, you will however need to keep the change made to the var the_url

The reason I prefer the other method is that you could add many other bits of information in the same way without having to modify the function variables each time.


Chris
User avatar
chris
Site Admin
 
Posts: 1089
Joined: Mon Dec 17, 2007 7:42 pm
Location: Málaga, Spain

Re: Passing a new value to the table

Postby MrRundog » Sun Apr 13, 2008 12:24 pm

Hi - I think that code will be of use to loads of people - for myself - I have now done this setting a session with bookingID - This way I am dynamic and only need to add one peice of info to the insert statement (also the session start stuff).

Thanks for the help
Andy
User avatar
MrRundog
 
Posts: 14
Joined: Fri Apr 11, 2008 12:57 pm

Re: Passing a new value to the table

Postby chris » Sun Apr 13, 2008 12:32 pm

good idea - using a session variable is of course another valid alternative ;)

Glad to have been able to help :)

Chris
User avatar
chris
Site Admin
 
Posts: 1089
Joined: Mon Dec 17, 2007 7:42 pm
Location: Málaga, Spain

Re: Passing a new value to the table

Postby MrRundog » Sun Apr 13, 2008 1:25 pm

OO This gets more interesting -

Ok Given that I now have my bookingID available when I need it - Would there be a fairly simple way to prevent any deletions that were !=bookingID by either simply making that box not clickable or a warning (Alert) telling the user it's not possible?

PS. When my project is done and I get paid - You can expect a donation - this program & your support is A1

Andy
User avatar
MrRundog
 
Posts: 14
Joined: Fri Apr 11, 2008 12:57 pm

Re: Passing a new value to the table

Postby chris » Sun Apr 13, 2008 2:22 pm

OK, lets go with this,
We now have a column in the database called something like "id_booking" (probably bookingID in your case)

We now need to add this data to the array that we define when checking the calendar. TO do this unfortunatly we need to slightly alter the array so that it becomes multilevel.

Find the code where we get the calendar states:
Code: Select all
$booked_days   =   array();
$sql="SELECT * FROM ".T_BOOKINGS." WHERE id_item=".ID_ITEM."";
if(!$res=mysql_query($sql))   die("ERROR checking id item availability dates<br>".mysql_error()."<br>".$sql);
while($row=mysql_fetch_assoc($res)){
   $booked_days[$row["the_date"]]=array("state"=>$row["state"], "id_booking"=>$row["id_booking"]);
}

and replace with this:
Code: Select all
$booked_days   =   array();
$sql="SELECT * FROM ".T_BOOKINGS." WHERE id_item=".ID_ITEM."";
if(!$res=mysql_query($sql))   die("ERROR checking id item availability dates<br>".mysql_error()."<br>".$sql);
while($row=mysql_fetch_assoc($res)){
   $booked_days[$row["the_date"]]=array("state"=>$row["state"], "id_booking"=>$row["id_booking"]);
}

you will see there that we have added this new value to the "sub" array data.
We now need to modify the function to both recognise the new way that the state is defined and to catch this new var to prevent the update link.

In the draw_cal function find this:
Code: Select all
if(array_key_exists($this_date,$booked_days)){
            //echo "<br>".$booked_days[$this_date];
            if($booked_days[$this_date]=="am")      $day_image='cal_am_'.$day_counter.'.png';
            elseif($booked_days[$this_date]=="pm")   $day_image='cal_pm_'.$day_counter.'.png';
            elseif($booked_days[$this_date]=="prv")   $day_image='cal_prv_'.$day_counter.'.png';
            else                     $day_image='cal_on_'.$day_counter.'.png';
         }

and replace with this:
Code: Select all
if(array_key_exists($this_date,$booked_days)){
            //echo "<br>".$booked_days[$this_date];
            if($booked_days[$this_date]["state"]=="am")      $day_image='cal_am_'.$day_counter.'.png';
            elseif($booked_days[$this_date]["state"]=="pm")   $day_image='cal_pm_'.$day_counter.'.png';
            elseif($booked_days[$this_date]["state"]=="prv")   $day_image='cal_prv_'.$day_counter.'.png';
            else                     $day_image='cal_on_'.$day_counter.'.png';
         }

That will get the code working again as regards the states (am, pm and prv)
Now, the final stage is to make the booked dates that have an "id_booking" (bookingID) un modifiable.
Find this code where the links are defined:
Code: Select all
if($show_link==1){
            $day_link_start   ='<a href="#" onclick="ajax_mod_state(\''.$id.'\',\''.$this_date.'\',\''.$day_counter.'\',event); return false"  title="'.$lang["modify_availability_for"].' '.$lang["day_".$week_day.""].' '. $date_format.'" class="cal_day">';
            $day_link_end   ='</a>';
         }else{
            $day_link_start   ="";
            $day_link_end   ="";
         }

and replace with this:
Code: Select all
//   default NO link state for all dates
$day_link_start   ="";
$day_link_end   ="";
$day_alt   =$lang["days"][$week_day].' '. $date_format;
//   show ajax link or not
if($booked_days[$this_date]["id_booking"]>0){
   //   date with idbooking - NO link
   //   you could also define diferent style here to "highlight" the dates
   $date_alt      =   $lang["day_".$week_day.""].' '. $date_format.' booked - id: '.$booked_days[$this_date]["id_booking"];
}
elseif($show_link==1)
{
   $date_alt       =    $lang["modify_availability_for"].' '.$lang["days"][$week_day].' '. $date_format;
   $day_link_start   =   '<a href="#" onclick="ajax_mod_state(\''.$id.'\',\''.$this_date.'\',\''.$day_counter.'\',event); return false"  title="'.$date_alt.'" class="cal_day">';
   $day_link_end   =   '</a>';
}

I have actually changed things here a bit to define the default "no link" states before the special cases.
I have also added the image title text here so that is is dynamic - clearly you can make the "hover" text say whatever you need ;)
You will need to update the actual image itself so as to use this text:
Code: Select all
<span id="state_'.$this_date.'">
               <img src="'.DIR_IMAGES.''.$day_image.'" alt="'.$date_alt.'" title="'.$date_alt.'" border="0"  width="20" height="20">
            </span>


And that "should" be everything.
1. We have added the new info to the array
2. We have modified the function to use the new sub array for the states
3. We have modified the link control to NOT allow dates booked with an Id to be modifiable.

I think that that was what you where after ;)

As always, let me know how it goes :)

Chris
User avatar
chris
Site Admin
 
Posts: 1089
Joined: Mon Dec 17, 2007 7:42 pm
Location: Málaga, Spain

Re: Passing a new value to the table

Postby chris » Sun Apr 13, 2008 2:26 pm

I have just reread your last post and realise that I might be slightly off target with my solution as you mention "!=bookingID"
I wonder if it is just specific bookings that you don't want modifiable????

In any case, most of what I have posted is still valid and necesarry, you just might need to play around with the line :
Code: Select all
if($booked_days[$this_date]["id_booking"]>0){
within the funciton so as to identify the actual id rather than just blocking all of them.

Chris
User avatar
chris
Site Admin
 
Posts: 1089
Joined: Mon Dec 17, 2007 7:42 pm
Location: Málaga, Spain

Next

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest