Jump to content
Larry Ullman's Book Forums

margaux

Members
  • Posts

    453
  • Joined

  • Last visited

  • Days Won

    52

Posts posted by margaux

  1. Everytime you call mysql_fetch_array, it returns the current row and moves its internal pointer to the next row. So the second time you use it, it will be starting at the second row.

     

    For your requirements, you could either repeat the d/b query or store the query results in an array which you can access as you need to e.g.

    $names = array(); // create a variable to hold the query results
    $query = "SELECT name FROM users ORDER BY name ASC";
    $r = mysql_query($query, $dbc);
    while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
    $names[] = $row; // add the row in to the results array
    }
    

    Then you can loop through $names for your various dropdown menu options.

    • Upvote 1
  2. Here's what my goto 'techie' expert said "the MySQL server has to be configured to accept external connections, that is bind to the correct network interface. The firewall may also need to be configured to accept incoming connections on the appropriate port".

     

    I only had to use the GRANT ALL PRIVILEGES command before issuing the mysqli_connect so I wouldn't know what to do with the above but maybe it will be of some help to you?

    • Upvote 2
  3. I'm pretty certain you will have to compromise and add some extra markup to style your radio buttons. Radio buttons and checkboxes are difficult form elements to style.

     

    It will help if you put the label tags before the input tags and you may want to use the name attribute so that they are part of the same group, otherwise both buttons could be checked.

    <label for="male">Male</label> <input type="radio" name="gender" id="male">
    <label for="female">Female</label><input type="radio" name="gender" id="female">
    

    • Upvote 1
  4. To prevent anyone inserting dicey code into your MySQL statements, you should probably validate your variable in some way - e.g. check its type with the filter extension and if appropriate, is it within a specific range. Use the mysqli_real_escape_string() function which escapes any special characters in your query. Or you can use a prepared statement which sends the query and specific values separately to MySQL

    $view = 1;
    $done = filter_var($_GET['Ref2'], FILTER_VALIDATE_INT, array('min_range' => 1)); //ensure variable is an integer > 0
    $done1 = "UPDATE errors SET Viewed = ? WHERE Ref = ?"; // insert placeholders to your query
    $stmt = mysqli_prepare($dbc, $done1); // send the statement to MySQL and assign result to a variable
    mysqli_stmt_bind_param($stmt, 'ii', $view, $done); // bind variables to the placeholders, the middle parameter tells sql what type of variables to expect, in this instance two integers
    mysqli_stmt_execute($done1); // execute the sql statement
    check the result
    mysqli_stmt_close($stmt);// clears $stmt variable
    

    • Upvote 2
  5. This is a bit confusing. What is the tablename? Your select statement should be in the format of

    SELECT columnname FROM tablename WHERE columnname = '$variable' 

    Assuming your table is named discount_codes and the column is named vouchercode...

    SELECT discount_amount FROM discount_codes WHERE vouchercode = '$vouchercode'

    Run your mysql statements in something like phpmyadmin and if it doesn't work it will give you a message to indicate where the error is. Then in your posts you can be more specific rather than just stating it doesn't work, which doesn't actually give any information.

     

    I would recommend reading chap 5 and chap 8.

  6. My approach is to get all the booked dates from the d/b and store them in an array. Then I display the calendar with forward and backward arrows to allow the visitor to page through the months. Using some for and while loops, I check if each calendar date is in the booked_dates array and set a css class accordingly.

     

    The basic functionality is working so I will look at how I set the booking price but for now I wonder if you would comment on my code. I'm not sure how efficient it is - I've had to do alot of date/variable formatting and wonder if there are better solutions. I know its alot of code so if you don't want to go through it, I Many thanks for any comments.

    $r = mysqli_query($dbc, "SELECT start_date, num_days FROM bookings ORDER BY start_date");
    $days_booked=array();
    while (list($start, $num_days) = mysqli_fetch_array($r, MYSQLI_NUM)){
    //use number of days for loop count
    for ($i=0; $i<$num_days; $i++) {
    	if ($i==0){
    		$days_booked[] = $start;
    	}
    	else {
    	// separate date into year, month and day to add $i days to day
    		$date_parts = explode('-',$start);
    		$days_booked[] = date("Y-m-d",mktime(0,0,0, ($date_parts[1]), $date_parts[2] + $i, ($date_parts[0])));
    	}	
    } //end for loop
    } //end while loop
    
    
    // check if month and year have been passed in url i.e. forward or backward arrow on calendar date has been clicked
    $mOptions = array('min_range' => 1, 'max_range' => 12);
    $yOptions = array('min_range' => 2012, 'max_range' => 2020);
    if (isset($_GET['m']) && isset($_GET['y']) ) {
    settype($_GET['m'],"integer");
    $month = filter_var($_GET['m'], FILTER_VALIDATE_INT, $mOptions);
    $year = filter_var($_GET['y'], FILTER_VALIDATE_INT, $yOptions);
    
    //forward arrow clicked	
    if (isset($_GET['a']) && ($_GET['a'] == 'f') ){
    $year= ($month == 12 ? $year + 1: $year);
    $month = ($month == 12 ? 01 : $month + 1);
    }
    
    //backward arrow clicked
    if (isset($_GET['a']) && ($_GET['a'] == 'b') ){
    $year= ($month == 1 ? $year - 1: $year);
    $month = ($month == 01 ? 12 : $month - 1);
    }
    $month =($month < 10 ? str_pad($month, 2, "0", STR_PAD_LEFT) : $month);
    }
    
    else {
    //else first time on page, get today's date to start display on current month
    $date = time ();
    //assign day, month, and year to separate variables
    $day = date('d', $date);
    $month = date('m', $date);
    $year = date('Y', $date);
    }
    
    // DISPLAY CALENDAR //
    //generate the first day of the month
    $first_day = mktime(0,0,0,$month, 1, $year) ;
    //get the month name
    $title = date('F', $first_day) ;
    
    //determine how many blank squares to display at start of month
    $day_of_week = date('D', $first_day);
    switch($day_of_week){
    case "Sun": $blank = 0; break;
    case "Mon": $blank = 1; break;
    case "Tue": $blank = 2; break;
    case "Wed": $blank = 3; break;
    case "Thu": $blank = 4; break;
    case "Fri": $blank = 5; break;
    case "Sat": $blank = 6; break;
    }
    
    //get num of days in the current month
    $days_in_month = cal_days_in_month(0, $month, $year);
    //display the table header
    echo "<table border=1 width=594 height=394>";
    echo "<thead><tr><th colspan=7> <a href=\"calendar.php?m=$month&y=$year&a=b\"><span class=\"bArrow\"><</span></a>$title $year <a href=\"calendar.php?m=$month&y=$year&a=f\"><span class=\"fArrow\">></span></a></th></tr></thead>";
    echo "<tr><td width=42 align=\"center\">Sun</td><td width=42 align=\"center\">Mon</td><td
    width=42 align=\"center\">Tue</td><td width=42 align=\"center\">Wed</td><td width=42 align=\"center\">Thu</td><td
    width=42 align=\"center\">Fri</td><td width=42 align=\"center\">Sat</td></tr>";
    //keep count of the days in the week, up to 7
    $day_count = 1;
    echo "<tr>";
    //display blank days
    while ( $blank > 0 ) {
     echo "<td></td>";
     $blank = $blank-1;
     $day_count++;
     }
    
    //set the first day of the month to 1
    $day_num = 1;
    //count up the days in the month
    while ($day_num <= $days_in_month ) {
    
    //format calendar day to be same format as date in days_booked array
    $day_num = str_pad($day_num, 2, "0", STR_PAD_LEFT);
    //concatenate year, month, day for comparison
    $compare_date = "$year-$month-$day_num";
    //set css class variable
    $avail = (in_array($compare_date,$days_booked) ? 'unavail' : 'avail');
     //display calendar day with appropriate css class
     echo "<td class=\"$avail\">$day_num</td>";
    $day_num++;
     $day_count++;
     //start a new row every week
     if ($day_count > 7) {
    	 echo "</tr><tr>";
    	 $day_count = 1;
    }
    }
    
    //finish the table with some blank squares if needed
    while ( $day_count >1 && $day_count <=7 ) {
     echo "<td> </td>";
    $day_count++;
    }
    echo "</tr></table>";
    

×
×
  • Create New...