Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. $result->fetch_row() will return an array with 1 item (the count)
  2. After posting the above I noticed that if the last ticket sold in a row was #21 then 22-25 would not be found as it looks for gaps between seat numbers. This revised version adds dummy sales for seat 26 for each row for each session to create the gap between 21 and 26. $tktsRequired = 4; $session = 3; $sql = " SELECT session_id, row , CASE WHEN seatnumber < 13 THEN seatnumber-$tktsRequired ELSE seatnumber-blocksize END as fromseat , CASE WHEN seatnumber < 13 THEN seatnumber-1 ELSE seatnumber-blocksize+$tktsRequired-1 END as seatto FROM ( SELECT IF(@prevsess=session_id AND @prevrow=row AND @prevseat!=seatnumber-1, seatnumber-@prevseat-1, IF(@prevrow<>row, seatnumber-1, 0)) as blocksize , @prevrow := row as row , @prevseat := IF(@prevrow=row,seatnumber,0) as seatnumber , @prevsess := session_id as session_id FROM ( SELECT row, seatnumber, session_id FROM ticket_sales UNION SELECT row, seatnumber, session_id FROM ( SELECT row, 26 as seatnumber FROM ticket_sales GROUP BY row ) as endseats CROSS JOIN ( SELECT DISTINCT session_id FROM ticket_sales ) as sessions ORDER BY session_id, row DESC, seatnumber ) as add_dummies JOIN (SELECT @prevrow:='' , @prevseat:=0, @prevsess:=0) as init ) as allsales WHERE blocksize >= $tktsRequired AND session_id = $session ORDER BY session_id, row DESC, IF(seatnumber<13, 13-seatnumber, seatnumber-12)"; $res = $db->query($sql);
  3. Barand

    FULL JOIN

    I believe you can simulate a FULL join with a union of two LEFT joins
  4. Something like this will find the available number of seats in the order you want (row E to A and giving priority to centre seats) $tktsRequired = 4; $session = 3; $sql = " SELECT session_id, row , CASE WHEN seatnumber < 13 THEN seatnumber - $tktsRequired ELSE seatnumber - blocksize END as fromseat , CASE WHEN seatnumber < 13 THEN seatnumber - 1 ELSE seatnumber - blocksize + $tktsRequired - 1 END as toseat FROM ( SELECT IF(@prevsess=session_id AND @prevrow=row AND @prevseat!=seatnumber-1, seatnumber-@prevseat-1, IF(@prevrow<>row, seatnumber-1, 0)) as blocksize , @prevrow := row as row , @prevseat := IF(@prevrow=row,seatnumber,0) as seatnumber , @prevsess := session_id as session_id FROM ticket_sales JOIN (SELECT @prevrow:='' , @prevseat:=0, @prevsess:=0) as init ORDER BY session_id, row, seatnumber ) as blocks WHERE blocksize >= $tktsRequired AND session_id = $session ORDER BY session_id, row DESC, IF(seatnumber<13, 13-seatnumber, seatnumber-12)"; $res = $db->query($sql); /* SAMPLE RESULTS for BLOCK of 4 tickets ** +------------+------+----------+--------+ | session_id | row | fromseat | toseat | +------------+------+----------+--------+ | 3 | E | 3 | 6 | | 3 | D | 7 | 10 | | 3 | D | 2 | 5 | | 3 | B | 11 | 14 | | 3 | B | 1 | 4 | | 3 | A | 9 | 12 | | 3 | A | 3 | 6 | +------------+------+----------+--------+ */ My sample data:
  5. you need to check for same type too SELECT 1 + (SELECT count( * ) FROM highscores a WHERE a.score > b.score AND a.type = b.type) AS rank FROM highscores b WHERE Name = 'Gustavo' AND Type = 'VsStory' ORDER BY rank
  6. OK, sprayed it with a can of "Datakill" to get rid of a couple of bugs and gave it a coat of varnish <?php $xr = 10; $xc = 7; // // generate array of cells // X if data, ' ' if blank // $cells = array(); for ($r = 0; $r < $xr; $r++) { for ($c = 0; $c < $xc; $c++) { $cells[$r][$c] = rand(0,2)<1 ? 'X' : ' '; } } $gaps = array(); for ($r = 0; $r < $xr-1; $r++) { for ($c = 0; $c < $xc-1; $c++) { if ($cells[$r][$c]==' ') { $gs = gapsize($cells, $r, $c, $xr, $xc); if ($gs[3]>=3 && $gs[4]>=2) { // accept 3+ rows x 2+ cols only $gaps[] = $gs; } } } } if (count($gaps) > 0) { rsort($gaps); vprintf("Largest suitable area is %d cells at row %d, col %d (%d rows x %d cols)<br>", $gaps[0]); } else echo "No suitable area found<br>"; // set boundary vars for output if (isset($gaps[0])) { list ($count, $row, $col, $rowcount, $colcount) = $gaps[0]; $rend = $row + $rowcount - 1; $cend = $col + $colcount - 1; } else { $row = $col = $rend = $cend = 999; $rowcount = $colcount = 0; } // output the array echo '<table border="1" style="border-collapse:collapse">'; echo "<tr><th></th>"; for ($i=0; $i<$xc; $i++) echo "<th>$i</th>"; echo "</tr>\n"; foreach ($cells as $r => $rowdata) { echo "<tr><th>$r</th>"; if ($r < $row || $r > $rend) { echo "<td>" . join('</td><td>', $rowdata) . "</td></tr>\n"; } else { foreach ($rowdata as $c => $cell) { if ($r==$row && $c==$col) { echo "<td rowspan='$rowcount' colspan='$colcount' style='background-color:#ccc'> </td>"; } else { if ($c < $col || $c > $cend) echo "<td>$cell</td>"; } } echo "</tr>\n"; } } echo '</table>'; function gapsize(&$cells, $r, $c, $xr, $xc) { if (($cells[$r][$c+1]!=' ')||($cells[$r+1][$c]!=' ')||($cells[$r+1][$c+1]!=' ')) { return array (1, $r, $c, 1, 1); } $arr = array(); $ccount=0; $c1 = $c; while ($c1 < $xc) { if ($cells[$r][$c1]==' ') { $arr[]=$c1; ++$ccount; ++$c1; } else break; } $depths = array_fill_keys($arr, 1); foreach ($arr as $k=>$c2) { $r1 = $r+1; if ($cells[$r1][$c2] != ' ') { #$arr = array_slice($arr, 0, $k); $ccount = $k; } else { while ($r1 < $xr) { if ($cells[$r1][$c2]== ' ') { $depths[$c2]++; } else { break; } $r1++; } if ($k > 0 && $depths[$c2] < 3) { $ccount = $k; break; } } } $depths = array_slice($depths,0,$ccount); $arr = array_slice($arr,0,$ccount); $rcount = min($depths); return array($rcount*$ccount,$r,$c,$rcount,$ccount); } ?>
  7. Then the number of columns in the table doesn't match the number of values being inserted. Specify the columns receiving the data in same order as the values
  8. Best to put the query string into variable so you can echo the submitted query when there is an error $sql = "INSERT IGNORE INTO " .$table. "VALUES (" . implode("';'", $data) . ""; But it looks like the ) at the end of the VALUES ( is missing
  9. This might help <?php $xr = 10; $xc = 7; // // generate array of cells // X if data, ' ' if blank // $cells = array(); for ($r = 0; $r < $xr; $r++) { for ($c = 0; $c < $xc; $c++) { $cells[$r][$c] = rand(0,2)<1 ? 'X' : ' '; } } $gaps = array(); for ($r = 0; $r < $xr-1; $r++) { for ($c = 0; $c < $xc-1; $c++) { if ($cells[$r][$c]==' ') { $gs = gapsize($cells, $r, $c, $xr, $xc); if ($gs[3]>=3) { // accept 3 or more rows only $gaps[] = $gs; } } } } if (count($gaps) > 0) { rsort($gaps); vprintf("Largest suitable area is %d cells at row %d, col %d (%d rows x %d cols)<br>", $gaps[0]); } else echo "No suitable area found<br>"; // output the array echo '<table border="1" style="border-collapse:collapse">'; echo "<tr><th></th>"; for ($i=0; $i<$xc; $i++) echo "<th>$i</th>"; echo "</tr>"; for ($r = 0; $r < $xr; $r++) { echo "<tr><th>$r</th>"; echo '<td>' . join('</td><td>', $cells[$r]) . '</td>'; echo '<tr>'; } echo '</table>'; function gapsize(&$cells, $r, $c, $xr, $xc) { if (($cells[$r][$c+1]!=' ')||($cells[$r+1][$c]!=' ')||($cells[$r+1][$c+1]!=' ')) { return array (1, $r, $c, 1, 1); } $arr = array(); $ccount=0; $c1 = $c; while ($c1 < $xc) { if ($cells[$r][$c1]==' ') { $arr[]=$c1; ++$ccount; ++$c1; } else break; } $depths = array_fill_keys($arr, 1); foreach ($arr as $k=>$c2) { $r1 = $r+1; if ($cells[$r1][$c2] != ' ') { #$arr = array_slice($arr, 0, $k); $ccount = $k; } else { while ($r1 < $xr) { if ($cells[$r1][$c2]== ' ') { $depths[$c2]++; } else { break; } $r1++; } if ($k > 0 && $depths[$c2] < 3) $ccount = $k; } } $depths = array_slice($depths,0,$ccount); $arr = array_slice($arr,0,$ccount); $rcount = min($depths); return array($rcount*$ccount,$r,$c,$rcount,$ccount); } ?>
  10. The secret is to create an INSERT statement with the correct syntax.
  11. try SELECT f.fid , f.destination , f.plannedDeparture , f.passengerCount , f.aid , m.name , m.usefulLoad , m.usefulLoad*0.65 + (m.pilotcount + f.passengercount)*160 as totalLoad , CASE WHEN m.usefulLoad*0.65 + (m.pilotcount + f.passengercount)*160 > usefulLoad THEN m.usefulLoad*0.65 + (m.pilotcount + f.passengercount)*160 - usefulLoad ELSE 0 END as overLoad , CASE WHEN m.usefulLoad*0.65 + (m.pilotcount + f.passengercount)*160 > usefulLoad THEN CEIL((m.usefulLoad*0.65 + (m.pilotcount + f.passengercount)*160 - usefulLoad)/160) ELSE 0 END as passengerSurplus FROM flight f INNER JOIN airplane a USING (aid) INNER JOIN model m USING (mid) ORDER BY plannedDeparture
  12. $query2 = "insert into complaint(complain,d_name,complainant_id) values ('$complain_det','$comp_name','{$row['complainant_id']}')"; mysql_query($query2); $the_auto_id_that_was_just_created = mysql_insert_id(); // now use that value as the foreign key when you add the detail record
  13. Have I got this right? Take the Piper Archer TX with a usefulLoad of 870 Fuel is 65% which leaves (870 * 0.35) for passengers and pilot ie 304.5 Take out the pilot and that leaves an allowance of (304.5 - 160) 144.5 for passengers. This is less than the 160 average so cannot carry passengers, or, at least, is overloaded with a single passenger ???
  14. How is the table being built?
  15. your code will fail if $fromlink4 starts with one of the items in your array, otherwise it should work. Also, $fromlink4 is already a string so there is no need for the the double quotes round it in the strpos() parameters. A better test is this which will work even if the found string is in position 0. if(strpos($fromlink4, $item) !== false) {
  16. Is mysqli_error($con) outputting an error message on 252? If so, what. Off-topic, looking at your current code I see you totally ignored my advice on the "the, The, THE" problem! Why do I bother?
  17. in my code, change $res = mysqli_query($con, $sql); to $res = mysqli_query($con, $sql) or die (mysqli_error($con));
  18. I used your connection variable, $con. You need to check your mysqli connection code
  19. try <td>Type of Business:</td> <td> <select name="typeofbusiness"> <option value=''> - select type of business -</option> <?php $sql = "SELECT id, Agent FROM tbl_typesofbusiness"; $res = mysqli_query($con, $sql); while (list($id, $tob) = mysqli_fetch_row($res)) { echo "<option value='$id'>$tob</option>\n"; } ?> </select> </td>
  20. get_group() isn't a PHP function so it must be a user function defined somewhere - probably in another php file that you haven't included
  21. All you have done is define a string. ( and why the sprintf() for a simple string definition??? ) You need to submit the query then fetch the data.
  22. You shouldn't be querying the database for every radio button. Just one query is required <?php require ("database.php"); $result = mysqli_query($con,"SELECT * FROM tab1"); $row = mysqli_fetch_array($result); $data = $row['sadlle']; $data = unserialize($data); for ($i=1; $i <= 50; $i++) { if (in_array($i, $data)) { echo "<input type=\"radio\" name=\"stall\" value=\"41\" 'disabled' /> <span style='color:#ccc'> $i</span><br>"; } else { echo "<input type=\"radio\" name=\"stall\" value=\"41\" /> $i<br>"; } } ?> At the moment, when a number is selected you have to get the serialized array, unserialize, add number to array, reserialize, update database If you normalized your data and held each selected number is a separate row in the table then you would just need to add a new record when a number was selected
  23. This your second post in 24 hours about a failing query. Check your queries for errors using mysql_error() to find out what is wrong. $sql = "SELECT * FROM users WHERE email = '".$email."' AND password = '".$password."' LIMIT 1 "; $res = mysql_query ($sql) or die(mysql_error() );
  24. Using backticks for all identifiers isn't wrong, just unnecessary (unless the identifier is a reserved word or contains spaces or other special characters), and IMO makes the query cluttered and not as readable. But I guess that's personal taste. On the other hand, MySQL is very lenient when it comes to structure rules and allows several things that other dialects of SQL would not. The use of field selection and GROUP BY clauses is one of then and the strict typing of variables, writing a string value to a numeric column, is another. It will let you quote numeric valuables but strict SQL syntax says they should be unquoted.
  25. ...because it should now look like mysqli_query($con,"INSERT INTO battery (`Range`, Percent, Sleep) VALUES ($miles, $battery_level, 1) "); `Range` being the only one requiring backticks and numerals not requiring quotes
×
×
  • 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.