Jump to content

Barand

Moderators
  • Posts

    24,572
  • Joined

  • Last visited

  • Days Won

    824

Everything posted by Barand

  1. 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
  2. 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"; } }
  3. 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.
  4. 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>
  5. Use code tags if you want things to line up neatly
  6. 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"; }
  7. 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
  8. 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 ) )
  9. 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 }
  10. Check the function definition for renderForm(). It probably requires 13 or more arguments to be passed to it. You call it with 12.
  11. 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
  12. 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
  13. Surely you noticed that getdate() was returning an array and not a date? Use SELECT COUNT(*) FROM `artesoes` WHERE data = CURDATE()
  14. Send me a PM and we can agree a price.
  15. 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 +-------------+--------+-----------+--------------+------------+
  16. I too had never noticed that OFFSET option in the LIMIT clause. Live 'n' learn.
  17. Isn't the pdo default for binding parameters "string" type and you therefore need to specify PDO::PARAM_INT?
  18. It's time you read this http://php.net/manual/en/pdostatement.execute.php
  19. Is this what you are trying to do? $data = array(); $roughHTTPPOST = @file_get_contents('php://input'); parse_str($roughHTTPPOST , $data); $current .= $data['social'];
  20. http://php.net/manual/en/reserved.variables.argv.php
  21. Then don't tell them 3 suitcases, tell them what we said (5 half-suitcases if you like) or give them a cubic capacity maximum. Instead of working in the simpler units as I suggested, work in absolute capacity (cu.ft or litres)
  22. Something like $update= "UPDATE produtos SET visivel = 1 WHERE id = $requiredID";
  23. Good to see we can agree on something
  24. From your query I am guessing your data looks like this mysql> SELECT * FROM vehicles; +-------------+---------+------------+------------+------------+ | vehicles_id | type | passengers | large_case | small_case | +-------------+---------+------------+------------+------------+ | 1 | Saloon | 4 | 2 | 1 | | 2 | Estate | 4 | 4 | 0 | | 3 | Estate | 4 | 3 | 2 | | 4 | Estate | 4 | 2 | 4 | | 5 | MPV | 6 | 3 | 2 | | 6 | Minibus | 8 | 10 | 5 | +-------------+---------+------------+------------+------------+ To save storing all the various combinations of luggage why not store the the total "Luggage Units" that it can hold. From your data you have Large case = 2 units Small case = 1 unit So the data would then be +-------------+---------+------------+------+ | vehicles_id | type | passengers | LU | +-------------+---------+------------+------+ | 1 | Saloon | 4 | 5 | | 2 | Estate | 4 | 8 | | 5 | MPV | 6 | 8 | | 6 | Minibus | 8 | 25 | +-------------+---------+------------+------+ the query for "Form Entry: 3 Passengers + 3 Large Bags + 2 Small Bags" becomes SELECT type FROM vehicles WHERE passengers >= 3 AND LU >= 8 ORDER BY passengers LIMIT 1 giving +--------+ | type | +--------+ | Estate | +--------+
×
×
  • 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.