Jump to content

Barand

Moderators
  • Content Count

    19,106
  • Joined

  • Last visited

  • Days Won

    361

Everything posted by Barand

  1. Barand

    Join or Union?

    The LEFT JOIN should work with the tables as they are, but the conditions on the deal table need to be in the JOIN ON and not in the WHERE. IE FROM Prospects LEFT JOIN Deals ON Prospects.ProspectID = Deals.ProspectID AND Deals.PIGLead = 2 AND Deals.Status = 'Won'
  2. No need to prepare/execute a query with no input parameters. Just use $find_entries = $db->query("SELECT entry_id, votes FROM entries WHERE votes = (SELECT MAX(votes) FROM entries)");
  3. Barand

    mySQL and rebuilding keys

    Primary keys do not require a name. Other indexes do. If you do not provide a name, MySql will generate one mysql> CREATE TABLE test_a ( -> aaa int NOT NULL PRIMARY KEY, -> bbb int, -> ccc int -> ); mysql> ALTER TABLE test_a ADD INDEX (bbb); -- no name specified mysql> ALTER TABLE test_a ADD INDEX my_index_name (ccc); -- name specified mysql> SHOW CREATE TABLE test_a; +--------+---------------------------------- | Table | Create Table +--------+---------------------------------- | test_a | CREATE TABLE `test_a` ( `aaa` int(11) NOT NULL, `bbb` int(11) DEFAULT NULL, `ccc` int(11) DEFAULT NULL, PRIMARY KEY (`aaa`), KEY `bbb` (`bbb`), -- name generated automatically KEY `my_index_name` (`ccc`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 +--------+----------------------------------
  4. Perhaps you want to do something like this SELECT entry_id , votes FROM entries WHERE votes = ( SELECT MAX(votes) FROM entries );
  5. Barand

    trying to learn invoice creater

    <form> ... </form> <button>Submit Order</button> I got it to work by moving it outside the form and letting send the ajax request. Your class was a bit messed up. Three class methods were defined as stand-alone functions outside the class definition and your spelling of "receipts" with the class was haphazard - sometimes "ie" and sometimes "ei". I also had to change your call to the parent constructor. Revised php: <?php require('test/fpdf/fpdf.php'); class PDF_receipt extends FPDF { function __construct ($orientation = 'P', $unit = 'pt', $format = 'Letter', $margin = 40) { parent::__construct($orientation, $unit, $format); $this->SetTopMargin($margin); $this->SetLeftMargin($margin); $this->SetRightMargin($margin); $this->SetAutoPageBreak(true, $margin); } function Header() { $this->SetFont('Arial', 'B', 20); $this->SetFillColor(36, 96, 84); $this->SetTextColor(225); $this->Cell(0, 30, "Nettuts+ Online Store", 0, 1, 'C', true); } function Footer() { $this->SetFont('Arial', '', 12); $this->SetTextColor(0); $this->SetXY(0,-60); $this->Cell(0, 20, "Thank you for shopping at Nettuts+!", 'T', 0, 'C'); } function PriceTable($products, $prices) { $this->SetFont('Arial', 'B', 12); $this->SetTextColor(0); $this->SetFillColor(36, 140, 129); $this->SetLineWidth(1); $this->Cell(427, 25, "Item Description", 'LTR', 0, 'C', true); $this->Cell(100, 25, "Price", 'LTR', 1, 'C', true); $this->SetFont('Arial', ''); $this->SetFillColor(238); $this->SetLineWidth(0.2); $fill = false; for ($i = 0; $i < count($products); $i++) { $this->Cell(427, 20, $products[$i], 1, 0, 'L', $fill); $this->Cell(100, 20, '$' . $prices[$i], 1, 1, 'R', $fill); $fill = !$fill; } $this->SetX(367); $this->Cell(100, 20, "Total", 1); $this->Cell(100, 20, '$' . array_sum($prices), 1, 1, 'R'); } } $pdf = new PDF_receipt(); $pdf->AddPage(); $pdf->SetFont('Arial', '', 12); $pdf->SetY(100); $pdf->Cell(100, 13, "Ordered By"); $pdf->SetFont('Arial', ''); $pdf->Cell(100, 13, $_POST['name']); $pdf->SetFont('Arial', 'B', 12); $pdf->Cell(50, 13, 'Date'); $pdf->SetFont('Arial', ''); $pdf->Cell(100, 13, date('F j, Y'), 0, 1); $pdf->SetX(140); $pdf->SetFont('Arial', 'I'); $pdf->Cell(200, 15, $_POST['address'], 0, 2); $pdf->Cell(200, 15, $_POST['city'] . ', ' . $_POST['province'], 0, 2); $pdf->Cell(200, 15, $_POST['postal_code'] . ' ' . $_POST['country']); $pdf->Ln(100); $pdf->PriceTable($_POST['product'], $_POST['price']); $pdf->Ln(50); $message = "Thank you for ordering at the Nettuts+ online store. Our policy is to ship your materials within two business days of purchase. On all orders over $20.00, we offer free 2-3 day shipping. If you haven't received your items in 3 busines days, let us know and we'll reimburse you 5%.\n\nWe hope you enjoy the items you have purchased. If you have any questions, you can email us at the following email address:"; $pdf->MultiCell(0, 15, $message); $pdf->SetFont('Arial', 'U', 12); $pdf->SetTextColor(1, 162, 232); $pdf->Write(13, "store@nettuts.com", "mailto:example@example.com"); $pdf->Output('receipt.pdf', 'F'); ?> You could have a problem if two users request a receipt at the same time - whose receipt.pdf will they get?
  6. Barand

    trying to learn invoice creater

    You have a form with action=create_receipt.php which you are submitting with a submit button. That same button has a click event which sends an ajax request to create_receipt.php You need to make up your mind exactly how you intend to process the form data.
  7. Barand

    Call PHP Output in JS?

    Store the value from php into a hidden input field <?php $hidden_content = "Hello World"; // CREATE HIDDEN CONTENT ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript" src="mytest.js"></script> </head> <body> <input type="hidden" id="my-hidden" value="<?=$hidden_content?>"> <!-- STORE HIDDEN CONTENT --> </body> </html> In your js file, get the value from the hidden input $().ready( function() { alert( $("#my-hidden").val()) })
  8. Barand

    trying to learn invoice creater

    Try putting your button outside the form <form> ... </form> <button type="submit">Submit Order</button>
  9. Barand

    trying to learn invoice creater

    Put your code in code boxes , please (<> button in the toolbar). I have done it for you this time.
  10. Of course - no matter what value you pass, "1" is in the array so it returns "House". Functions exit as soon as a value is returned. Perhaps function property_structure_type ($data) { $types = [ 1 => 'House', 2 => 'Cabin', 3 => 'Mobile Home', 4 => 'Apartment', 5 => 'Condominium', 6 => 'Townhome' ]; return isset($types[$data]) ? $types[$data] : 'None'; } echo property_structure_type(3); //-> Mobile home
  11. Barand

    SELECT QUERY from table with no relationship

    I meant it isn't data that you want to be displayed on a web page anywhere.
  12. Barand

    SELECT QUERY from table with no relationship

    You might want to look up "url rewriting". I'd be more worried about broadcasting your user's secret answers
  13. Barand

    Site Won't Submit Multiple Variables

    This should be close to what you need <?php if (isset($_GET['city_id'])) { $where = [ "p.status != 'trashed'", "paid = 1" ]; $whereclause = ''; $params = []; if ($_GET['city_id'] != 0) { $ids = explode(';', $_GET['city_id']) ; // split multiple ids $k = count($ids); $placeholders = array_fill(0, $k, '?'); $placestr = join(',', $placeholders); $where[] = "city_id IN ($placestr)"; $params = $ids; } if (trim($_GET['query']) != '') { $where[] = "MATCH(place_name, description) AGAINST (?)"; $params[] = $_GET['query']; } $whereclause = "WHERE " . join(' AND ', $where); // FIND HOW MANY RECORDS FOR PAGINATION $res = $conn->prepare("SELECT COUNT(*) FROM cities c JOIN places p USING (city_id) $whereclause "); $res->execute( $params ); $total_rows = $res->fetchColumn(); // NOW GET THE RECORDS FOR DISPLAY $rows_per_page = 10; $total_pages = ceil($total_rows/$rows_per_page); $page = $_GET['page'] ?? 1; $offset = ($page - 1) * $rows_per_page; $res = $conn->prepare("SELECT c.city_name , c.state , p.place_id , p.place_name , p.lat , p.lng , p.description FROM cities c JOIN places p USING (city_id) $whereclause ORDER BY city_name, place_name LIMIT $offset, $rows_per_page "); $res->execute( $params ); // OUTPUT YOUR RESULTS HERE } ?> <hr> <form> City <select id="query-input" name="city_id" required> <option value="">Select Area</option> <option value="3;1">Ches & VB</option> <option value="3">Chesapeake , VA</option> <option value="9">Hampton , VA</option> <option value="10">Newport News , VA</option> <option value="2">Norfolk , VA</option> <option value="12">Poquoson , VA</option> <option value="4">Portsmouth , VA</option> <option value="5">Suffolk , VA</option> <option value="1">Virginia Beach , VA</option> <option value="11">Williamsburg , VA</option> <option value="0">All Active US Cities</option> </select> <br> Search for <input type="text" name="query" value=""> <input type="submit" name="btnSub" value="Search"> <br>
  14. Barand

    Site Won't Submit Multiple Variables

    Use GET when you want to get data from the server to display. Use POST when sending something to the server, for example, to update a record. Exceptions would be when using sensitive data (eg passwords) or large amounts of data since GET has limit of around 2K. (Fuller explanation)
  15. Barand

    SELECT QUERY from table with no relationship

    There are a couple of methods you could use the data_id and button value that I showed you earlier, coupled with an ajax request to update the points have a separate form for each row, put the userID and points values into hidden fields, and submit with the redeem button.
  16. Barand

    SELECT QUERY from table with no relationship

    Maybe it's time to go to plan B and use the data_id attribute instead of your INSERT ... SELECT?
  17. Barand

    SELECT QUERY from table with no relationship

    Have you investigated that?
  18. Barand

    SELECT QUERY from table with no relationship

    That's the general theory (however, as with any rule, there may be exceptions in practice. For example, I expect my bank stores the closing balance on my last statement, otherwise it will have to go through 50 years of transactions to get the opening balance on my next statement). Storing the individual transactions that build up to the total gives you an audit trail. There is also a possibility that a stored total could get out of sync with the total of the individual transactions, and then you have two versions of the "truth".
  19. Barand

    SELECT QUERY from table with no relationship

    Don't store totals (or any other derived data) in your tables. You get totals by requerying your data when required. You have a INSERT ... SELECT query. If you run the SELECT portion on its own, do you only get a single record every time?
  20. Barand

    Is there a way to simplify this code?

    Something like this, perhaps TEST DATA +----+-----------+--------------+------------+ | id | name | date_started | days_count | +----+-----------+--------------+------------+ | 1 | Contest 1 | 2019-04-16 | 1 | | 2 | Contest 2 | 2019-04-15 | 3 | | 3 | Contest 3 | 2019-04-14 | 5 | | 4 | Contest 4 | 2019-04-14 | 2 | | 5 | Contest 5 | 2019-04-15 | 1 | +----+-----------+--------------+------------+ QUERY SELECT name , date_started as startdate , date_started + INTERVAL days_count-1 DAY as enddate , datediff(curdate(), date_started) + 1 as daynum FROM contests WHERE curdate() BETWEEN date_started AND date_started + INTERVAL days_count-1 DAY ORDER BY date_started; RESULTS +-----------+------------+------------+--------+ | name | startdate | enddate | daynum | +-----------+------------+------------+--------+ | Contest 3 | 2019-04-14 | 2019-04-18 | 3 | | Contest 2 | 2019-04-15 | 2019-04-17 | 2 | | Contest 1 | 2019-04-16 | 2019-04-16 | 1 | +-----------+------------+------------+--------+
  21. Barand

    SELECT QUERY from table with no relationship

    "data-" attributes are useful, EG <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $().ready( function() { $(".redeem").click( function() { var pts = $(this).val() var userid = $(this).data("id") alert("User: " + userid + "\nPoints: " + pts) }) }) </script> </head> <body> <button class='redeem' data-id='42' value='10' >Redeem</button> <button class='redeem' data-id='25' value='30' >Redeem</button> </body> </html>
  22. Barand

    Site Won't Submit Multiple Variables

    Don't spread the same problem over several topics. We don't want to have to search the board for "the story so far"
  23. Barand

    SELECT QUERY from table with no relationship

    When they redeem the milk reward (using the 10 points) write a record for -10 points for that user to the points table. That way, the next time you total the points it will be 10 less.
  24. Barand

    Arrays: concatenation operator .= vs =

    ... or how little?
×

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.