-
Posts
24,572 -
Joined
-
Last visited
-
Days Won
824
Everything posted by Barand
-
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
-
Listing Categories and Sub categoris with PHP and Mysql
Barand replied to thara's topic in PHP Coding Help
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"; } } -
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.
-
Listing Categories and Sub categoris with PHP and Mysql
Barand replied to thara's topic in PHP Coding Help
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> -
Some help working with three tables if you can thanks
Barand replied to 0o0o0's topic in PHP Coding Help
Use code tags if you want things to line up neatly -
Listing Categories and Sub categoris with PHP and Mysql
Barand replied to thara's topic in PHP Coding Help
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"; } -
Yes.
-
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
-
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 ) )
-
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 }
-
Check the function definition for renderForm(). It probably requires 13 or more arguments to be passed to it. You call it with 12.
-
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
-
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
-
Surely you noticed that getdate() was returning an array and not a date? Use SELECT COUNT(*) FROM `artesoes` WHERE data = CURDATE()
-
Send me a PM and we can agree a price.
-
Adapting my Calendar to work across more than 1 Month.
Barand replied to fileparts's topic in PHP Coding Help
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 +-------------+--------+-----------+--------------+------------+ -
I too had never noticed that OFFSET option in the LIMIT clause. Live 'n' learn.
-
Isn't the pdo default for binding parameters "string" type and you therefore need to specify PDO::PARAM_INT?
-
It's time you read this http://php.net/manual/en/pdostatement.execute.php
-
Is this what you are trying to do? $data = array(); $roughHTTPPOST = @file_get_contents('php://input'); parse_str($roughHTTPPOST , $data); $current .= $data['social'];
-
PHP CLI- how to take input from command prompt (important and urgent)
Barand replied to ManojVarma's topic in PHP Coding Help
http://php.net/manual/en/reserved.variables.argv.php -
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)
-
Something like $update= "UPDATE produtos SET visivel = 1 WHERE id = $requiredID";
-
Good to see we can agree on something
-
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 | +--------+