1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

help with PHP calendar (reservation system)

Discussion in 'PHP' started by absentx, Aug 31, 2011.

  1. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #21
    Just so a Javascript for() loop to clear all the cells.

    BTW, unless there's some reason for wipe to be a variable, you van use
    url: "write_dates.php?type=wipe",
     
    Rukbat, Sep 15, 2011 IP
  2. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #22
    Okay since we are in the PHP forum why don't we get back to some php right?

    Rukbat, you have sent me from javascript nothing to at least a white belt I am forever in your debt...I am starting to understand what it takes.

    Alright so now let me ask a few questions about PHP which I am clearly a master (white belt with one black stripe!!!) at...hehe..yeah right. I am finally to the point with php where I am starting to embrace MCV and use functions, its been a long road but I am happy to be here.

    So, my current question is this (and this is very relevant to about where I am in my php skill level)

    I have a few url passed variables...specifically month, date and year..

    So lets say:

    
    $_REQUEST['month'];
    $_REQUEST['date'];
    $_REQUEST['year'];
    
    PHP:
    I need to make sure those variables are managed properly. I want to first of all get them out of their globalness hard to write state and put them into nice variables like:

    
    $current_month = $_REQUEST['month'];
    $current_day = $_REQUEST['day'];
    $current_year = $_REQUEST['year'];
    
    PHP:
    But now I need to make sure they are date formatted correctly...meaning, I cant be having $current_month = '05'..it needs to be '5'.

    So how do I take $_REQUEST['any global variable'] convert it to the date(); that I need, then make it a nice variable in one fell swoop?

    I tried some function work tonight with the issue and it just didn't work!

    Thanks
     
    absentx, Sep 17, 2011 IP
  3. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #23
    Still trying to work on this...I just cant get a nice clean way to do this...simply because I don't know how.


    I want $current_month to equal date("n") of $_REQUEST["month"];
     
    absentx, Sep 18, 2011 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #24
    You don't send improperly-formatted data to the server, so validate things that can be validated in the client in the client, before sending them to the server. (In PHP, of course, date('n', $current_date) will be 1-9, 10-12.) Just make sure that the date passed by the browser is a valid date. There's lots of Javascript code on the web to validate dates.)

    If you really want $current_month = $_REQUEST['month']; to be a single character for January to September, just take the value:

    $current_month = intval($_REQUEST['month']);

    If you really want to be picky, cast that as a string:

    $current_month = (string) intval($_REQUEST['month']);
     
    Rukbat, Sep 18, 2011 IP
  5. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #25
    Okay the intval suggestions works great for now, but as I get farther into this there will be more considerations for date and how the information is handled, but I will deal with that down the road.

    You mentioned error handling a few replies back when talking about changing the cell colors on click.

    So I have done some research on errors with PHP but, it is all kind of overwhelming to me. Like you state, we need to make sure that everything in the php script works okay when ajax sends the request to it. The php script is either writing or deleting the dates for the given session as such:

    
    //First lets check to see if the date is already in the database for this particular user, if it is, we need to delete it.
    $cell= mysqli_real_escape_string($dbc, trim($_REQUEST['cell']));
    $wipe_em_all = $_REQUEST['type'];
    $selected_dates = array();
    
    $query= "SELECT reservation FROM dates WHERE customer ='$id'";
    $data = mysqli_query($dbc, $query);
    //first check to see if the date is already in the db, if so, the user clicked it to remove it, so remove it.
    while ($row = mysqli_fetch_array($data)) {	
    //populate our array of dates incase we need to clear them all later
    $selected_dates[] = $row['reservation'];
    if ($cell == $row['reservation']){
    	$query = "DELETE FROM dates WHERE reservation= '$cell'";
    	$data = mysqli_query($dbc, $query);
    	$color_change ='0';
    	echo  $cell.'|'.$color_change;
    	exit();
    	}
    }
    //the clear all button has been clicked, lets make sure there is something there to clear, if not, let the user know
    if (mysqli_num_rows($data) == 0 && $wipe_em_all=='clear'){
    	echo '2';
    	exit();
    	}
    //there are dates to clear, so clear them all
    if($wipe_em_all == 'clear'){
    	$query = "DELETE FROM dates WHERE customer= '$id'";
    	$data = mysqli_query($dbc, $query);
    	foreach ($selected_dates as $date){
    		echo $date.'|';
    	}
    	exit();
    	}
    //We have made it through the above loop, since the date doesn't exist in the table for the current user, lets write it.
    $query =  "INSERT INTO dates (customer, reservation) VALUES ('$id','$cell')";
    $data = mysqli_query($dbc, $query);
    $color_change ='1';
    echo  $cell.'|'.$color_change;
    
    mysqli_close($dbc);
    //------
    
    
    PHP:
    So if I could get pointed in the right direction on server error checking in there that would be great. I see lots of examples for creating custom error handlers and all of that, but I am not sure I understand what would trigger an error in this situation, so I need some help with that.
     
    absentx, Sep 19, 2011 IP
  6. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #26
    Okay still looking for help on checking for server errors and such, but I am also still struggling with some of the calendar date colors.

    The person I am doing this for has a lot of specific requirements for dates...ie, certain times are three night minimums, four night minimums etc..

    Well, I am color coding the dates for each type of reservation. I have the calendar populating beautifully color wise, meaning it is following all my rules nicely to generate the proper class for each cell depending on the date.

    Now...when a user clicks a cell, javascript changes the class to indicate a selected date...but what if a user clicks the cell again (meaning they no longer wish to have that date as their reservation) OR they hit the clear button to clear all dates from their reservation.

    I want those dates to return to their original cell color.

    Options:

    1. in the initial ajax request, pass some type of color variable along with the cell variable...store it in the db, then use that information when the user clicks again.

    My problem with this option is given the code below, I do not know how to
    A. add a second parameter to pass in the onclick (something like (this.id,this.class)????????)
    B. How to add it to the url parameter of the AJAX function (really bad at strings in javascript yet...getting there though)

    
    //onclick for the cell
    ajax_click_regular(this.id)
    
    //which calls this function
    function ajax_click_regular(neato) {
    	
    $.ajax({
      type: "POST",
      url: "write_dates.php?cell="+neato,
      context: document.body,
      success: function(data){
        //data is now the value that PHP echoed
    phpsaid = data.split('|');
    var state = phpsaid[1];
    if (state == '1'){
         document.getElementById(phpsaid[0]).className = "number_selected";
    	 document.getElementById("response").innerHTML= phpsaid[0]+' Has Been Successfully Added to Your Reservation';
    	 }
    else
        {
    	 document.getElementById(phpsaid[0]).className = "number";
    	 document.getElementById("response").innerHTML= phpsaid[0]+' Has Been Successfully Removed From Your Reservation';
    
        }
      }
    });
    
    
    
    }
    
    
    Code (markup):
    2. Just write some javascript code that checks the current class of the cell and then changes it based on that.

    The problem with that option is I cannot get that to work!

    3. I take it there is not a nice easy way to just have everything on the page "revert" to its original class?



    suggestions please!

    EDIT EDIT EDIT************************

    Okay so apparently writing this thread helped me figures some things out...

    this code works nicely
    
     var current_class = document.getElementById(phpsaid[i]).className
    	   if (current_class == 'number_selected_peak'){
    	   document.getElementById(phpsaid[i]).className = 'peak';
    	
    	   }
    	   else{
           document.getElementById(phpsaid[i]).className = 'number';
    	   
    	   }
    document.getElementById("response").innerHTML = 'All reservations for this session have been cleared';
    
    Code (markup):
     
    Last edited: Sep 21, 2011
    absentx, Sep 21, 2011 IP
  7. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #27
    Okay so this project has come full circle at least once now...I have recoded it just over the past week and we are in much better shape.

    I need to add a unique identifier to each reservation.

    I know for a fact this system will not get a ton of use initially..even 500 uses is WAY over what will probably happen the first year.

    Regardless, knowing that, what is the best strategy for unique reservation id generation?

    There are 50 billion different ways to do it but I am looking for some practical input on what is the most efficient way to generate one for this system knowing that:

    -this will not be a highly used system initially (perhaps a year or two)
    -we want a confirmation id that is reasonable for the customer to recall or save

    thoughts?
     
    absentx, Nov 10, 2011 IP