-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
deleted
-
Then $_POST['abc'] doesn't contain 3 comma-separated items
-
Alternative $updated = array(); $fieldnames = array('id', 'name', 'date'); if (isset($_POST['abc']) && ! empty($_POST['abc'])) { //grab id, name and date and assign to variables $updated = array_combine($fieldnames, explode(',', $_POST['abc'])); } Giving $updated Array ( [id] => 1 [name] => xyz [date] => 06/04/2015 )
-
It's the combination of magic_quotes ON and real_escape_string that causes the data to be escaped twice and "\'" being written to the db. If magic_quotes is ON the slashes need to be stripped before using real_escape_string
-
Usual cause is "magic_quotes" User inputs "O'ReiIly" Magic quotes turns this into "O\'Reilly" real_escape_string then changes it to "O\\\'Reilly" Gets written to db as "O\'Reilly"
-
Is it worth storing database results in a session
Barand replied to NotionCommotion's topic in PHP Coding Help
My first advice would be "Don't use SELECT * ". Every one of those ids in the joins will be duplicated so you are getting a lot of data you don't need and therefore slowing your query. Specify just the columns you need -
SELECT Name, CONCAT_WS(', ', replace(location,'\\',''), address, city, state, zip) as location If you have any Irish names in the data then they too could give the same problem. I hope you now magic_quotes_gpc turned off
-
Either use php echo date('d-m-Y H:i:s', strtotime(db_date)); or use the SQL SELECT DATE_FORMAT(db_date, '%d-%m-%Y %H-%i-%s') ...
-
Using ENUM datatype in many columns with list of values is good practice?
Barand replied to thara's topic in MySQL Help
Pass the current value to the function and test for it when creating the options function enumOptions($db, $table, $colname, $current='') /********************************** * db - database connection * table - table name * colname - name of the ENUM column * current - current selected value ***********************************/ { $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) { $sel = (($k+1==$current) || (trim($val, "'")==$current)) ? 'selected="selected"' : ''; $opts .= sprintf("<option $sel value='%d'>%s</option>\n", $k+1, trim($val, "'")); } return $opts; } // EXAMPLE USAGE echo "<select name='status'>\n"; echo enumOptions($db, 'users', 'status', '1'); echo "</select>\n"; -
You need to the ropers in a consistent sequence and rotate the heelers each round A B C ------ M N O N O M O M N Once allocated the running order can be shuffled. So it would be like this <?php $ropers = [ 'Alice', 'Bob', 'Cindy', 'Dave', 'Emma', 'Fred', 'Gina', 'Henry', 'Imogen', 'Jack' ]; $heelers= [ 'Martin', 'Nicola', 'Oliver', 'Paula', 'Quentin', 'Rosie', 'Steve', 'Tracey', 'Uri', 'Vanessa' ]; $output = ''; for ($round=1; $round<=10; $round++) { $rotated = rotate($heelers, $round-1); $riders = array_combine($ropers, $rotated); $output .= "<table border='1'>\n <tr><th colspan='4'>Round $round</th></tr>\n <tr><th></th><th>Roper</th><th>Heeler</th><th>Time</th></tr>\n"; $turn=1; $rprs = $ropers; // keep ropers in original order shuffle($rprs); foreach ($rprs as $r) { $h = $riders[$r]; // get the heeler allocated to the roper for this round $output .= "<tr><td>$turn</td><td>$r</td><td>$h</td><td></td></tr>\n"; ++$turn; } $output .= "</table>\n"; } function rotate($array, $n) { for ($i=0; $i<$n; $i++) $array[] = array_shift($array); return $array; } ?> <html> <head> <title>Round Robin</title> <style type="text/css"> table { margin: 5px; width: 18%; float: left; } th { color: white; background-color: #369; } td { background-color: white; font-family: sans-serif; font-size: 9pt; } div { background-color: #CCC; } </style> </head> <body> <h1>Round Robin Event</h1> <div> <?=$output?> </div> </body> </html> PS : 100 (10x10) combinations
-
Dates have to be in yyyy-mm-dd format to perform comparisons
-
Recommend approach to insert multiple MySQL records with PHP
Barand replied to NotionCommotion's topic in PHP Coding Help
I am just curious (having never used multiple inserts in conjunction with a prepared statement) if that method would hold up when inserting a csv file with many columns and a few thousand rows -
So you want us to tell you how to get from X to your desired results when you won't even show us what X looks like? Not going to happen. Good luck with that.
-
Recommend approach to insert multiple MySQL records with PHP
Barand replied to NotionCommotion's topic in PHP Coding Help
I was thinking of the more general case $sql.='(?, ?, ?, NOW(), ?, ?),'; -
What does $_post['abc'] contain?
-
Recommend approach to insert multiple MySQL records with PHP
Barand replied to NotionCommotion's topic in PHP Coding Help
I have just noticed you are attempting a prepared statement in the second example - how do you propose binding the parameters? -
Recommend approach to insert multiple MySQL records with PHP
Barand replied to NotionCommotion's topic in PHP Coding Help
For a large number of inserts I would definitely choose the latter method, it's faster. For a few, as you have there, I would probably go for the convenience of a prepared query. -
Not sure from your post what you are trying to achieve so this example may or may not be relevant. Task: Remove old comments for items but only if there is a more recent comment, so we don't completely remove all comments for an item. mysql> SELECT * FROM comments; +----+---------------------+--------+--------+----------+ | id | dateAdded | itemid | userid | comment | +----+---------------------+--------+--------+----------+ | 1 | 2014-11-01 00:00:00 | 1 | 12 | aaaaaaaa | <- remove | 2 | 2014-12-12 00:00:00 | 2 | 15 | bbbbbbbb | | 3 | 2015-01-02 00:00:00 | 1 | 25 | cccccccc | | 4 | 2015-03-25 00:00:00 | 3 | 44 | dddddddd | +----+---------------------+--------+--------+----------+ DELETE a FROM comments a INNER JOIN comments b ON a.itemid = b.itemid AND b.dateAdded >= '2015-01-01' WHERE a.dateAdded < '2015-01-01'; mysql> SELECT * FROM comments; +----+---------------------+--------+--------+----------+ | id | dateAdded | itemid | userid | comment | +----+---------------------+--------+--------+----------+ | 2 | 2014-12-12 00:00:00 | 2 | 15 | bbbbbbbb | | 3 | 2015-01-02 00:00:00 | 1 | 25 | cccccccc | | 4 | 2015-03-25 00:00:00 | 3 | 44 | dddddddd | +----+---------------------+--------+--------+----------+
-
The first half of the UNION finds (for each trainer) free time before first booking free time between bookings all trainers time if no bookings The second part finds trainers' free time after last booking of the day. SELECT trainer , from_time , to_time , timeslot FROM ( SELECT a.day , TIMEDIFF(IFNULL(start_time,close_time), IF(a.trainer=@prevt,@prevend,open_time )) as timeslot , CAST(IF(a.trainer=@prevt,@prevend,open_time ) as TIME) as from_time , IFNULL(start_time,close_time) as to_time , @prevend := end_time as prevend , @prevt := a.trainer as trainer FROM bookingavailability a JOIN (SELECT @prevend:=null,@prevt:=null) as init LEFT JOIN bookingscalendar c ON a.trainer = c.trainer AND WEEKDAY(c.bookingdate) = a.day AND c.bookingdate = '2014-08-18' WHERE a.day = WEEKDAY('2014-08-18') ORDER BY a.trainer, c.start_time ) gaps UNION SELECT a.trainer , MAX(end_time) as from_time , a.close_time as to_time , TIMEDIFF(MAX(end_time),close_time) as timeslot FROM bookingavailability a INNER JOIN ( SELECT trainer , MAX(end_time) as end_time FROM bookingscalendar WHERE bookingdate = '2014-08-18' GROUP BY trainer ) eod WHERE a.day = WEEKDAY('2014-08-18') ORDER BY trainer,from_time;
- 30 replies
-
- php calendar
- mysql
-
(and 2 more)
Tagged with:
-
Use styles (CSS) EG <?php $str = "abcd{efgh}ijkl{mnop}qrst{uvwx}yz"; $str = str_replace('{', '{<span>', $str); $str = str_replace('}', '</span>}', $str); ?> <html> <head> <style type="text/css"> div { color: #00F; } span { color: #F0F; } </style> </head> <body> <h3>The text</h3> <div> <?=$str?> </div> </body> </html>
-
Get default values, unless the foreign key is present in the table?
Barand replied to maxxd's topic in MySQL Help
try SELECT * FROM tbl_featured_images WHERE (page_id = :page) UNION SELECT * FROM tbl_featured_images WHERE NOT EXISTS (SELECT * FROM imagetest WHERE page_id = :page) AND page_id IS NULL; -
Put the left float on the <div>s and not the <p> tags. Also removed the padding. <?php //how many display columns $number_columns = 8; //dummy array data $array = range(1, 100); //percentage for css division of page width $percentage = floor(100 / $number_columns); //create chunks from the array calculating amount in array and number of columns $columns = array_chunk($array, ceil(count($array) / $number_columns)); //loop the chunked columns foreach ($columns as $chunk) { //opted to make a divider and style echo "<div style='float:left;width: " . $percentage . "%;'>"; //loop each array value within the chunk foreach ($chunk as $values) { //opted to make a paragraph and style echo "<p>" . $values . "</p>"; } echo "</div>"; } ?>
-
Warning: Invalid argument supplied for foreach()
Barand replied to Sh4dowDan's topic in PHP Coding Help
Having executed the query you have to fetch the results before you can loop through them. -
I'd store the prices in the "$menu" array so that it is effectively the database. <?php session_start(); if (!isset($_SESSION['products'])) { $_SESSION['products'] = []; } $menu = [ 1 => ['name' => "Ham and Pineapple Pizza", 'price' => 3.49], 2 => ['name' => "Margherita", 'price' => 3.49], 3 => ['name' => "BBQ Chicken", 'price' => 2.99], 4 => ['name' => "Lamb Kebab", 'price' => 2.49], 5 => ['name' => "Beef Burger", 'price' => 2.49], 6 => ['name' => "Hot Dog", 'price' => 1.99] ]; $order = ''; $orderTotal = 0; if (isset($_GET['product'])) { $_SESSION['products'][] =$_GET['product']; } elseif (isset($_GET['view'])) { $order = "<ol>\n"; foreach ($_SESSION['products'] as $item) { $order .= "<li>{$menu[$item]['name']} ({$menu[$item]['price']})</li>\n"; $orderTotal += $menu[$item]['price']; } $order .= "</ol>\n"; if ($orderTotal) { $order .= sprintf("Total price: %6.2f<br>", $orderTotal); } } elseif (isset($_GET['cancel'])) { $_SESSION['products'] = []; } ?> <html> <head> <style type="text/css"> #cart { border: 1px solid gray; background-color: #C0FFC0; padding: 10px; } </style> </head> <body> <h1>Menu</h1> <table> <?php foreach ($menu as $key=>$item) { echo "<tr><td>{$item['name']}</td> <td>{$item['price']}</td> <td><a href='?product=$key'>Order item</a></td></tr>"; } ?> </table> <div id='cart'> Items ordered: <?= count($_SESSION['products'])?> items. [<a href='?view=1'>View</a>] <?=$order?> <br> [<a href='?cancel=1'>Cancel order</a>] </div> </body> </html>