Jump to content

Barand

Moderators
  • Posts

    24,604
  • Joined

  • Last visited

  • Days Won

    830

Everything posted by Barand

  1. Like this function displayList(&$cats, $parent, $level=0) { switch ($level) { case 0: $class = "list-unstyled categorychecklist"; break; case 1: $class = "children"; break; case 2: $class = "children2"; break; } if ($parent==0) { foreach ($cats[$parent] as $id=>$nm) { displayList($cats, $id); } } else { echo "<ul class='$class'>\n"; foreach ($cats[$parent] as $id=>$nm) { echo "<li><input type='checkbox' value=''> $nm</li>\n"; if (isset($cats[$id])) { displayList($cats, $id, $level+1); //increment level } } echo "</ul>\n"; } }
  2. The value of "$level" will tell you whether is the main or nested.
  3. You can test the dates are in range when you output with PHP, eg if (date('Y-m-d',strtotime($row['vidate'])) >= date('Y-m-d') && strtotime($row['vidate'])<= date('Y-m-d',strtotime("+7 days"))) { echo "Insurance Expiry Date: {$row['vidate']}<br/>"; } or you can adjust the SQL SELECT visitor_id , visitor_name , visitor_email , IF(visitor_tax BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY, DATE_FORMAT(visitor_tax, '%e %M %Y') , 'n/a') as taxdate , IF(visitor_mot BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY, DATE_FORMAT(visitor_mot, '%e %M %Y' , 'n/a') as motdate , IF(visitor_insurance BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY, DATE_FORMAT(visitor_insurance, '%e %M %Y' , 'n/a') as vidate FROM visitors WHERE visitor_tax BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY OR visitor_mot BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY OR visitor_insurance BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY
  4. Let's take the problems one by one Checking for date < today + 7 days will also return records from last week, last month, last year, as those dates all fit the condition. What you need is dates between today and today + 7 days. You need to check the dates individually. I am assuming that if any one of those dates is due then an email is sent (therefore using OR condition). If all three must be due then change to AND. The problem with SELECT * is it selects all columns. If you have 12 columns in the table and only need 4 then you you retrieve 3x more data (approx) and it therefore takes 3x longer to transfer the data from the server. If, in future, someone decides to add,say, a BLOB column to store an image of the insurance document, then your queries suddenly slow down as instead of the 20 bytes you need, you now transfer 50,000 bytes for each record. If you had specified the columns then that wouldn't happen. SELECT visitor_id , visitor_name , visitor_email , DATE_FORMAT(visitor_tax, '%e %M %Y') as taxdate , DATE_FORMAT(visitor_mot, '%e %M %Y') as motdate , DATE_FORMAT(visitor_insurance, '%e %M %Y') as vidate FROM visitors WHERE visitor_tax BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY OR visitor_mot BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY OR visitor_insurance BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY
  5. You need to format the dates separately "SELECT * , DATE_FORMAT(visitor_tax, '%e %M %Y') as taxdate , DATE_FORMAT(visitor_mot, '%e %M %Y') as motdate , DATE_FORMAT(visitor_insurance, '%e %M %Y') as vidate SELECT * !!! - ok, don't listen, that's the best way to stop people helping Exactly what date conditions are you wanting here?
  6. If your queries are only concerned with DATE types then you should use CURDATE() and not NOW(). The NOW() function also contains the time of day. Alternatively you can format the date in the query SELECT DATE_FORMAT(visitor_insurance, '%e %M %Y') as vidate Don't use SELECT * unless you really do want every column retrieved, it's inefficient and obscures the purpose of the query. Specify what you need. SELECT visitor_id , visitor_name , DATE_FORMAT(visitor_insurance, '%e %M %Y') as vidate FROM visitors WHERE visitor_insurance > DATE_ADD(CURDATE(), INTERVAL -1 DAY)
  7. If visitor_mot is a datetime then you need to ignore the time portion. SELECT * FROM visitors WHERE DATE(visitor_mot) = DATE_ADD(curdate(), INTERVAL 1 DAY) or SELECT * FROM visitors WHERE DATE(visitor_mot) = CURDATE() + INTERVAL 1 DAY
  8. It's the programming equivalent of sitting on a branch while you saw through it. You are processing an array but in that process you overwrite the very array you are trying to process
  9. Pass a level value down the function calls and use that function displayList(&$cats, $parent, $level=0) { switch ($level) { case 0: $class = "level0"; break; case 1: $class = "level1"; break; case 2: $class = "level2"; break; } if ($parent==0) { foreach ($cats[$parent] as $id=>$nm) { displayList($cats, $id); } } else { echo "<ul>\n"; foreach ($cats[$parent] as $id=>$nm) { echo "<li>$nm</li>\n"; if (isset($cats[$id])) { displayList($cats, $id, $level+1); //increment level } } echo "</ul>\n"; } }
  10. The calculation in that position does nothing - $price and $weight have not been defined. $request = mysql_query("SELECT * FROM database WHERE ean = '12' ") or die (mysql_error()); while ($row = mysql_fetch_array($request)) { if ($row['weight']==0) { $calculation1 = 0; } else { $calculation1 = $row['price'] / $row['weight']; } echo "$row[name] and $row[weight]<br>"; echo "$row[price] and $calculation1 <br>"; echo "<br><br>"; } Alternatively SELECT name , price , weight , CASE weight WHEN 0 THEN 0 ELSE price/weight END as calculation FROM database WHERE ean = '12' You should move off mysql_ functions and change to mysqli_ or PDO libraries instead.
  11. Probably. On reflection I would prefer to recurse with this simpler query and method though, and it should work for any number of levels and doesn't display the top level. $sql = "SELECT category_id , name , IFNULL(parent, 0) FROM category"; $res = $db->query($sql); while (list($id,$name,$parent) = $res->fetch_row()) { $cats[$parent][$id] = $name; } displayList($cats, 0); function displayList(&$cats, $parent) { if ($parent==0) { foreach ($cats[$parent] as $id=>$nm) { displayList($cats, $id); } } else { echo "<ul>\n"; foreach ($cats[$parent] as $id=>$nm) { echo "<li>$nm</li>\n"; if (isset($cats[$id])) { displayList($cats, $id); } } echo "</ul>\n"; } } With your data, gives <ul> <li>Dogs</li> <ul> <li>Bulldog</li> <li>Bullmastiff</li> <li>Chow Chow</li> <li>Cocker Spaniel</li> <li>German Shepherd</li> <li>Gordon Setter</li> </ul> <li>Cats</li> <ul> <li>American Bobtail</li> <li>Balinese</li> <li>Birman</li> <li>British Shorthair</li> <li>Burmese</li> </ul> <li>Birds</li> <li>Fish</li> <li>Reptiles</li> </ul>
  12. Use code tags if you want things to line up neatly
  13. Or you can use recursion $res = $db->query($sql); while (list($lev1,$lev2,$lev3) = $res->fetch_row()) { $cats[$lev1][$lev2][] = $lev3; } displayList($cats); function displayList($cats) { echo "<ul>\n"; foreach ($cats as $k=>$v) { if (is_array($v)) { echo "<li>$k</li>\n"; displayList($v); } else echo "<li>$v</li>\n"; } echo "</ul>\n"; }
  14. Items in the WHERE clause need to be columns in the table rows (or expressions/functions using those values). You need either to duplicate the calculation in the WHERE WHERE 2 * ASIN(SQRT(POWER(SIN((@latitude- u.latitude) * pi().... < 5 or use a HAVING clause
  15. Modified accordingly $orig = range(1,25); $pool = $orig; // need a copy to assign from $assigned = []; $u = 0; // first user assign($pool, $assigned, $orig[$u], 6); // remaining users while ($pool) { ++$u; assign($pool, $assigned, $orig[$u], ; } echo '<pre>',print_r($assigned, true),'</pre>'; function assign(&$users, &$assigned, $to, $n) { $assigned[$to] = array_slice($users,1,$n); $users = array_slice($users,$n); } Gives $assigned Array ( [1] => Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 7 ) [2] => Array ( [0] => 8 [1] => 9 [2] => 10 [3] => 11 [4] => 12 [5] => 13 [6] => 14 [7] => 15 ) [3] => Array ( [0] => 16 [1] => 17 [2] => 18 [3] => 19 [4] => 20 [5] => 21 [6] => 22 [7] => 23 ) [4] => Array ( [0] => 24 [1] => 25 ) )
  16. Is this what you are describing for 25 users? +------------+-----+-----+-----+ | player | 1 | 8 | 17 | +------------+-----+-----+-----+ | | 2 | 9 | 18 | | | 3 | 10 | 19 | | | 4 | 11 | 20 | | assigned | 5 | 12 | 21 | | | 6 | 13 | 22 | | | 7 | 14 | 23 | | | | 15 | 24 | | | | 16 | 25 | +------------+-----+-----+-----+ If it is, then $users = range(1,25); $assigned = []; // first user assign($users, $assigned, 6); // remaining users while ($users) { assign($users, $assigned, ; } function assign(&$users, &$assigned, $n) { $u = $users[0]; $assigned[$u] = array_slice($users,1,$n); // assign next n users to first in list $users = array_slice($users,$n+1); // reduce the users array }
  17. Check the function definition for renderForm(). It probably requires 13 or more arguments to be passed to it. You call it with 12.
  18. Barand

    update

    Autoincrement applies to insert queries. Don't set a value for the id in the update or inserts, let the autoincrement look after the values. Once an id has been allocated on INSERT, it should never be changed
  19. Barand

    update

    All your options have an empty string as their value. Either omit the value=''so it defaults to to the "nome" value. Explicitly specify the $row['nome'] as the value Better still, specify the id as the value and use the id instead of name in related records
  20. Surely you noticed that getdate() was returning an array and not a date? Use SELECT COUNT(*) FROM `artesoes` WHERE data = CURDATE()
  21. Send me a PM and we can agree a price.
  22. Here is how I would create an array of booked dates for a month $monthStart = date('Y-m-01'); $monthEnd = date('Y-m-t'); $bookings = array(); // CREATE DATE PERIOD FOR THE MONTH $s = new DateTime($monthStart); $e = new DateTime("$monthEnd + 1 days"); $oneday = new DateInterval('P1D'); $dp = new DatePeriod($s, $oneday, $e); // INITIALIZE BOOKINGS ARRAY FOR MONTH foreach ($dp as $d) { $bookings[$d->format('Y-m-d')] = ''; } // FIND BOOKING WHICH ARE ALL OR PART IN THIS MONTH $sql = "SELECT userID , bookingStart , bookingEnd FROM bookings WHERE machineID = ? AND bookingStart < ? AND bookingEnd > ? "; $getBookings = $con->prepare($sql); $getBookings->bind_param('iss', $_GET['id'], $monthEnd, $monthStart); $getBookings->execute(); $getBookings->bind_result($uid, $bstart, $bend); while ($getBookings->fetch()) { $s = new DateTime(max($bstart, $monthStart)); // FIND THE START AND END $e = new DateTime(min($bend, $monthEnd)); // DATES WITHIN THE MONTH $e->modify('+1 day'); $dp = new DatePeriod($s, $oneday, $e); // DATE PERIOD FOR THE BOOKING foreach ($dp as $d) { // STORE userid IN THE DAYS BOOKED $bookings[$d->format('Y-m-d')] = $uid; } } which gives an array, indexed by date, showing the user who has booked the machine from this data +-------------+--------+-----------+--------------+------------+ | bookings_id | userID | machineID | bookingStart | bookingEnd | +-------------+--------+-----------+--------------+------------+ | 1 | 1 | 1 | 2015-05-20 | 2015-05-31 | previous month | 2 | 2 | 1 | 2015-05-28 | 2015-06-08 | starts in prev month | 3 | 3 | 1 | 2015-06-10 | 2015-06-12 | | 4 | 4 | 1 | 2015-06-15 | 2015-06-23 | | 5 | 5 | 1 | 2015-06-28 | 2015-07-10 | ends in next month | 6 | 6 | 1 | 2015-07-11 | 2015-07-20 | next month +-------------+--------+-----------+--------------+------------+
  23. I too had never noticed that OFFSET option in the LIMIT clause. Live 'n' learn.
  24. Isn't the pdo default for binding parameters "string" type and you therefore need to specify PDO::PARAM_INT?
×
×
  • 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.