Jump to content

Barand

Moderators
  • Posts

    24,566
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Of course it echoes all items in the menu regardless - so you can place an order. When you view the order it only echoes those that have been selected by the user (ie those in the $_SESSION['products'] array)
  2. Here's a simple example using an array as suggested <?php session_start(); if (!isset($_SESSION['products'])) { $_SESSION['products'] = []; } $menu = [ 1 => "Ham and Pineapple Pizza", 2 => "Margherita", 3 => "BBQ Chicken", 4 => "Lamb Kebab", 5 => "Beef Burger", 6 => "Hot Dog" ]; $order = ''; if (isset($_GET['product'])) { $_SESSION['products'][] = $menu[$_GET['product']]; } elseif (isset($_GET['view'])) { $order = "<ol>\n"; foreach ($_SESSION['products'] as $item) { $order .= "<li>$item</li>\n"; } $order .= "</ol>\n"; } ?> <html> <head> <style type="text/css"> #cart { border: 1px solid gray; background-color: #C0FFC0; padding: 10px; } </style> </head> <body> <h1>Menu</h1> <?php foreach ($menu as $key=>$item) { echo "<p>$item <a href='?product=$key'>Order item</a></p>"; } ?> <div id='cart'> Items ordered: <?= count($_SESSION['products'])?> items. (<a href='?view=1'>View</a>) <?=$order?> </div> </body> </html>
  3. Just add your products into an array, say $_SESSION['products']. $_SESSION['products'][] = 'Ham pizza'; $_SESSION['products'][] = 'Margherita'; $_SESSION['products'][] = 'BBQ Chicken'; echo count($_SESSION['products']) . 'Items<br>'; foreach ($_SESSION['products'] as $item) { echo "$item<br>"; }
  4. Where id is true (ie non=zero) example mysql> SELECT -> id -> , username -> FROM users -> WHERE id -> ORDER BY status; +----+----------+ | id | username | +----+----------+ | 2 | User 222 | | 4 | User 444 | | 1 | User 111 | | 3 | User 333 | +----+----------+ 4 rows in set (0.00 sec)
  5. This is the download function that I use. function sql2csv($mysqli, $sql, $filename='', $headings=1) /** * Parameters * $mysqli - connection * $sql - the sql query to be executed * $filename - name of download file (default "download_yymmddhhii.csv") * $headings - 1 if fieldname headings required (default), 0 if not required */ { if (!$filename) $f = 'download_' . date('ymdhi') . '.csv'; else $f = $filename; $fp = fopen('php://output', 'w'); // so you can fputcsv to STDOUT if ($fp) { $res = $mysqli->query($sql); if ($res) { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="'.$f.'"'); header('Pragma: no-cache'); header('Expires: 0'); $row = $res->fetch_assoc(); if ($headings) { fputcsv($fp, array_keys($row)); } do { fputcsv($fp, $row); } while ($row = $res->fetch_assoc()); } else echo "Error in query"; fclose($fp); } } Use it in a php file that you link to with a download link.
  6. I am guessing your second helpful quote (locations) should look like this geoname_id | locale_code | continent_code | continent_name | country_iso_code | country_name 49518 | en | AF | Africa | RW | Rwanda but I had a problem deciphering the first (block) quote network | geoname_id | registered_country_geoname_id | represented_country_geoname_id | is_anonymous_proxy | is_satellite_provider 1.0.0.0/24 | 2077456 | 2077456 | 0 | 0 ?????? They do have "geoname_id" column in common (foreign key)
  7. Thought I'd have a go myself using the GD library
  8. Functions normally return a value based on the arguments passed to it rather than echoing the output. Here's a trivial example function add ($a, $b) { return $a + $b; } echo add(2,3);
  9. I prefer the use of lookup tables and using the foreign key in the data. It is much easier to create dropdown selection menus It enforces referential integrity If you want to stick with enum and need to get the options available for dropdowns in your forms, this function will do it for you function enumOptions($db, $table, $colname) /********************************** * db - database connection * table - table name * colname - name of the ENUM column ***********************************/ { $sql = "SELECT SUBSTRING(COLUMN_TYPE,5) as vals FROM information_schema.COLUMNS WHERE table_schema = (SELECT DATABASE()) AND table_name='$table' AND column_name = '$colname'"; $res = $db->query($sql); list($vals) = $res->fetch_row(); $vals = explode(',', trim($vals, '()')); $opts = "<option value=''>- select $colname -</option>\n"; foreach($vals as $k => $val) { $opts .= sprintf("<option value='%d'>%s</option>\n", $k+1, trim($val, "'")); } return $opts; } // EXAMPLE USAGE echo "<select name='status'>\n"; echo enumOptions($db, 'users', 'status'); echo "</select>\n";
  10. Pass them as arguments and have what you read about globals clinically erased. function phpfreaks() { global $pdo; ... return; } is fine until the day comes when you find yourself having to work with two simultaneous PDO connections, and all your functions are wired in to the first. Now you have to start swapping values. If you had used function phpfreaks($pdo) { ... return; } there would be no problems.
  11. What specific problems are you having with the results?
  12. Replace will DELETE then INSERT an new record if there is record there already with same key. Try using INSERT...ON DUPLICATE KEY UPDATE instead
  13. Barand

    trim

    You also need to pass the first argument to __invoke() by reference otherwise the callback works but on a copy of the array. class Trim { public function __invoke(&$data, $dummy, $character_mask = null) { if (is_array($data)) { array_walk_recursive($data, array($this, '__invoke'),$character_mask); } else { $data = trim($data, $character_mask); } return $data; } } $string = 'this a test .'; $array = array('this is a string .', ' test two'); $trim = new Trim; $string = $trim($string, null, 't'); $array = $trim($array, null, 't'); echo '<pre>'.$string; print_r($array); echo trim('yesssss', 's'); /* OUTPUTS his a test . Array ( [0] => his is a string . [1] => test two ) ye */
  14. I'm sure you meant id BETWEEN $start AND $start+$count-1
  15. Because if the data comes from a database the technique used for paginating your data will be different from that used if it comes from a text or xml file.
  16. If you do that you won't know about the clients that are double-booked to inform them and change their bookings, you just hide the problem. This query will pull any booking where a room is booked at the same time as another booking. SELECT id ,bookingdate ,room ,start_time ,end_time ,trainer ,customer_id FROM bookingscalendar WHERE id IN ( SELECT b1.id FROM bookingscalendar b1 INNER JOIN bookingscalendar b2 ON b1.bookingdate = b2.bookingdate AND b1.room = b2.room AND b1.start_time < b2.end_time AND b1.end_time > b2.start_time AND b1.id <> b2.id ) ORDER BY bookingdate,room,start_time; Ensure at the time of booking that only free rooms can be allocated.
  17. And you still haven't told us where the articles are coming from. I guess you don't want help after all.
  18. We still need to see code. We have no idea where the articles are coming from.
  19. Your availability is for trainer 2, your bookings for trainer 1. Dates will be null when no matching bookings for a trainer
  20. I notice you have added another level of complexity with the inclusion of the "room" in the bookings. So as well as checking for trainer availability you also have to check for room availability.
  21. These are my table definitions CREATE TABLE `bookingscalendar` ( `id` int(11) NOT NULL AUTO_INCREMENT, `bookingdate` date DEFAULT NULL, `trainer` int(11) DEFAULT NULL, `start_time` time DEFAULT NULL, `end_time` time DEFAULT NULL, `customer_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ); CREATE TABLE `bookingavailability` ( `availability_id` int(11) NOT NULL AUTO_INCREMENT, `trainer` int(11) DEFAULT NULL, `dayofweek` int(11) DEFAULT NULL, `open_time` time DEFAULT NULL, `close_time` time DEFAULT NULL, PRIMARY KEY (`availability_id`) )
  22. Or you can use PHP gd library to zoom images. This will give a x2 zoom to the area clicked in the image imagezoom.html imagezoom.php
  23. Booking date looks OK when I run it. I had to comment out the trainername column, that should be in a trainer table, not in availability table) mysql> SELECT trainer -> -- , trainername -> , dayofweek -> , bookingdate -> , CONCAT(from_time,'') as from_time -> , to_time, timeslot -> FROM -> ( -> SELECT a.trainer -> -- , a.trainername -> , dayofweek -> , bookingdate -> , TIMEDIFF(start_time, IF(bookingdate=@prevdate,@prevend,open_time )) as timeslot -> , IF(bookingdate=@prevdate,@prevend,open_time ) as from_time -> , start_time as to_time -> , @prevend := end_time as prevend -> , @prevdate := bookingdate as prevdate -> FROM bookingavailability a -> JOIN (SELECT @prevend:=null,@prevdate:=null) as init -> INNER JOIN bookingscalendar c -> ON a.trainer = c.trainer -> AND WEEKDAY(c.bookingdate) = a.dayofweek -> -> UNION -> -> SELECT a.trainer -> -- , a.trainername -> , dayofweek -> , bookingdate -> , TIMEDIFF(close_time, IFNULL(MAX(end_time),open_time) ) as timeslot -> , IFNULL(MAX(end_time),open_time) as from_time -> , close_time as to_time -> , null as prevend -> , null as prevdate -> FROM bookingavailability a -> LEFT JOIN bookingscalendar c -> ON a.trainer = c.trainer -> AND WEEKDAY(c.bookingdate) = a.dayofweek -> GROUP BY a.trainer,dayofweek,bookingdate -> ) as gaps -> WHERE timeslot > '00:00:00' -> ORDER BY trainer, dayofweek, bookingdate, from_time; +---------+-----------+-------------+-----------+----------+----------+ | trainer | dayofweek | bookingdate | from_time | to_time | timeslot | +---------+-----------+-------------+-----------+----------+----------+ | 1 | 0 | 2014-08-18 | 09:00:00 | 10:00:00 | 01:00:00 | | 1 | 0 | 2014-08-18 | 11:00:00 | 13:00:00 | 02:00:00 | | 1 | 0 | 2014-08-18 | 14:30:00 | 16:00:00 | 01:30:00 | | 1 | 0 | 2014-08-18 | 17:30:00 | 20:00:00 | 02:30:00 | | 1 | 1 | 2014-08-19 | 10:30:00 | 17:00:00 | 06:30:00 | | 1 | 2 | 2014-08-20 | 11:00:00 | 12:00:00 | 01:00:00 | | 1 | 2 | 2014-08-20 | 13:00:00 | 15:00:00 | 02:00:00 | | 1 | 3 | 2014-08-21 | 08:00:00 | 10:00:00 | 02:00:00 | | 1 | 3 | 2014-08-21 | 11:00:00 | 13:00:00 | 02:00:00 | +---------+-----------+-------------+-----------+----------+----------+ Check your data
  24. When you say "zoom" do you want to show part of the image enlarged or just enlarge the whole image like this <html> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript"> var zoom = 0; $().ready(function() { $("#pic").click(function() { if (zoom==1) { $(this).attr({"width":200,"height":200}); zoom = 0; } else { $(this).attr({"width":400,"height":400}); zoom = 1; } }) }) </script> </head> <body> <img id='pic' src="myimage.jpg" border="0" width="200" height="200"> </body> </html>
  25. Add "LIMIT 50" to the end of the query
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.