-
Posts
24,604 -
Joined
-
Last visited
-
Days Won
830
Everything posted by Barand
-
Puzzling indeed. Does locating the element that does have data-userid=8 help to clarify what's going on? BTW, I think the closing line of your event handler is missing the final ")"
-
Have you tried using your browser's developer tools to step through the JS code and inspect the variables to find the point of failure?
-
Don’t know how to get values from stored procedure
Barand replied to narutofan's topic in PHP Coding Help
No -
Don’t know how to get values from stored procedure
Barand replied to narutofan's topic in PHP Coding Help
No so much making fun of you but rather showing the folly of your approach. I've suggested a couple of times in this thread that you just use two normal queries. You could even use a single query with a union but you said you wanted the two results kept separate for some reason. Yet despite any advice (which you always have a habit of ignoring) you insist on this "easier" stored procedure approach. -
I haven't waded through all the code but I wondered, from the symptoms, if the cause is similar to this situation. Consider this simple html script <!DOCTYPE html> <html> <head> <title>Sample Search</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type='text/javascript'> $().ready( function () { $("button").click( function() { $("#main").append("<button>Click to add new button</button><br>") }) }) </script> </head> <body> <header> <h1>Sample</h1> </header> <div id="main"> <button>Click to add new button</button> <button>Click to add new button</button> <button>Click to add new button</button> <br><br> </div> </body> </html> Very simple - buttons which, when clicked, add another button for you to click. You click a button and, lo, another appears. Click a new button - nothing! Click an original button and another appears. The on-click event handler was created just after the form loaded and so was only applied to button objects that existed then. New buttons are not assigned the handler. I was wondering if a similar thing is affected your script. You only get values from the stage the form loaded
-
Don’t know how to get values from stored procedure
Barand replied to narutofan's topic in PHP Coding Help
Here's a simpler example to illustrate your problem mysql> select id, name INTO @a FROM locations; ERROR 1222 (21000): The used SELECT statements have a different number of columns The query is trying to select 2 columns into 1 variable so it throws that error. Aha! so all we have to do is put the 2 columns into 2 variables? mysql> select id, name INTO @a, @b FROM locations; ERROR 1172 (42000): Result consisted of more than one row Alas, you can't do that with a query returning many rows, so... mysql> select id, name INTO @a, @b FROM locations LIMIT 1; Query OK, 1 row affected (0.00 sec) mysql> select @a, @b; +------+-----------------+ | @a | @b | +------+-----------------+ | 1 | Test Location 1 | +------+-----------------+ Now we're getting somewhere - all you need to do is create a stored procedure and call it 10 times, each time selecting a different single row in to 7 variables. Then after you've finished, put all those 70 variables into your timeline. As you said, much easier than using two normal queries. -
Don’t know how to get values from stored procedure
Barand replied to narutofan's topic in PHP Coding Help
LOL - yes, it's evident that you find it so much easier. Seems like you really need to fix those problems - they won't go away on their own. As you can with two queries, one for each table. But I guess you would find that so much more difficult! [EDIT] If you putting them all in the one timeline, why would want to keep them separate? I would have thought you would want them all merged an sorted by date. -
Don’t know how to get values from stored procedure
Barand replied to narutofan's topic in PHP Coding Help
Why a stored procedure? -
Don’t know how to get values from stored procedure
Barand replied to narutofan's topic in PHP Coding Help
How can I? I have no idea what you are trying to do. I can see the method you are attempting to achieve your objective but I haven't clue about what that objective is, or why you have chosen this method. -
Although you never actually confirmed your expected output, this is what I think you want SELECT p.productName , p.productCode , pi.current_inventory , pi.transaction_date , l.name FROM product_locations pl JOIN products p ON pl.product_id = p.productCode JOIN locations l ON pl.location_id = l.id JOIN product_inventory pi ON pl.id = pi.product_location_id JOIN ( SELECT pl.product_id , MAX(transaction_date) as date FROM product_locations pl JOIN product_inventory pi ON pl.id = pi.product_location_id WHERE transaction_date < '2020-01-03 23:59:59' AND pl.location_id = 1 GROUP BY pl.product_id ) latest ON latest.product_id = pl.product_id AND pi.transaction_date = latest.date; +---------------------------------------+-------------+-------------------+---------------------+-----------------+ | productName | productCode | current_inventory | transaction_date | name | +---------------------------------------+-------------+-------------------+---------------------+-----------------+ | 1969 Harley Davidson Ultimate Chopper | S10_1678 | 25 | 2020-01-03 11:27:35 | Test Location 1 | | 1952 Alpine Renault 1300 | S10_1949 | 11 | 2020-01-01 12:23:11 | Test Location 1 | +---------------------------------------+-------------+-------------------+---------------------+-----------------+ Subquery finds latest date for each product and uses that date to find matching inventory record
-
Don’t know how to get values from stored procedure
Barand replied to narutofan's topic in PHP Coding Help
Why are you preparing a query that is a call to a stored procedure? The whole point of preparing queries is to isolate the data from the query code. Stored procedures already do that (the IN clauses are the same as binding parameters in a prepared query). Just use $result = $this->db->query("CALL timeline($friend_name, $session_id)"); That's because, by default, MySQL will not run multiple queries. You can turn on multiple client queries but that will probably give you even more problems. Split it into two calls/ stored procedures (if you feel it really must be stored procedures) -
OK, so it looks like this? The key to your problem lies in that join between your other tables and the subquery Your query results +---------------------------------------+-------------+-------------------+---------------------+-----------------+------+ | productName | productCode | current_inventory | transaction_date | name | id | +---------------------------------------+-------------+-------------------+---------------------+-----------------+------+ | 1969 Harley Davidson Ultimate Chopper | S10_1678 | 25 | 2020-01-03 00:00:00 | Test Location 1 | 1 | | 1952 Alpine Renault 1300 | S10_1949 | NULL | NULL | Test Location 1 | 3 | +---------------------------------------+-------------+-------------------+---------------------+-----------------+------+ | | +-------------------------------------------- your join -------------------------------------------------+ | | | Subquery results +---------------------+---------------------+-------------------+ | product_location_id | transaction_date | current_inventory | +---------------------+---------------------+-------------------+ | 1 | 2020-01-03 00:00:00 | 25 | +---------------------+---------------------+-------------------+ Adding pl.id to your query's field list illustrates it. Your two records have values 1 and 3. Because you LIMIT 1 in the subquery it's going to give only one record, which matches the "1"
-
What would you like the result to look like?. Then we know what you are trying do. Your inventory data is only by location and date - there is no product id - so it's hard to get values for each product. You have stock totals by product and location but I see no mention of stock in your query.
-
Show me how to load those data images into a test database so I can check out the subqueries and I'll be glad to help.
-
try <?php require 'db_inc.php'; $db = pdoConnect(); // function defined in db_inc.php $search = $_GET['search'] ?? ''; $tdata = ''; $stmt = $db->prepare("SELECT ID , GAME , PLATFORM , Owned FROM games WHERE GAME LIKE ? "); $stmt->execute([ "$search%" ]); foreach ($stmt as $row) { $tdata .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n"; } ?> <!DOCTYPE html> <html> <head> <title>Sample Search</title> <style type='text/css'> body { font-family: calibri, sans-serif; font-size: 11pt;} header{ background-color: black; color: white; padding: 16px; } form { padding: 16px; } table { width: 75%; margin: 30px auto; border-collapse: collapse; } th { background-color: black; color: white; padding: 8px; } td { padding: 4px 8px;} </style> </head> <body> <header> <h1>Sample</h1> </header> <form method='GET'> Search for: <input type="text" name="search" value="<?=$search?>" size="40"> <input type="submit" name="btnSub" value="Search"> </form> <div> <?php if ($search) { ?> <table border='1'> <thead> <tr> <th>ID</th> <th>GAME</th> <th>PLATFORM</th> <th>OWNED</th> </tr> </thead> <tbody> <?=$tdata?> </tbody> </table> <?php } ?> </div> </body> </html>
-
loop through array with incremental for striped table
Barand replied to Wal-Wal-2's topic in PHP Coding Help
Try incrementing the counter at the end of the loop. Also no need for all the duplicate code. All you need is to set a variable $suc = $counter % 2 == 1 ? 'success' : ''; E.G. $counter = 0; foreach ($res as $row) { $suc = $counter % 2 == 1 ? 'success' : ''; $tdata .= "<tr class='$suc'> <td>{$row['firstname']}</td> <td>{$row['lastname']}</td> </tr>"; $counter++; } then <table> <?=$tdata?> </table> -
The more information a query retrieves the slower it will run. In the code below all you want is a single count yet you retrieve every column in every record. // find number of rows in the db $query = "SELECT * FROM menu_update WHERE special_of_theday = 1 AND restaurant_type = '".$selected_restaurant_type."' "; $query_result = mysqli_query($connection, $query); $row_count = mysqli_fetch_array($query_result); $total_rows = mysqli_num_rows($query_result); Far more efficient to $query = "SELECT COUNT(*) as total FROM menu_update WHERE special_of_theday = 1 AND restaurant_type = '$selected_restaurant_type' "; $query_result = mysqli_query($connection, $query); $row = mysqli_fetch_array($query_result); $total_rows = $row['total'];
-
It would appear from the code that you would always have links 1 2 3 4 ... N except when you are on the last page. You show the links on this condition... if ($page_number != $expected_number_of_pages) Do you, perchance, only have two pages at present? And do you mean $page_number or do you mean $current_page (confusing names)
-
Your misleading choice of variables names no doubt contributed to the problem. There are other things could be improved too Use prepared queries instead of putting variables directly into the sql query strings. (I would also recommend PDO in preference to mysqli) use css classes instead of repetitive inline style definitions you are using deprecated html markup code (eg <tr bgcolor = "#cccccc">) you use a lot of unnecessary concatenation when building strings - a common source of errors.
-
In your pagination loop you set all the links to the same page number! Should be $data = array( 'page' => $current_page, <-- CHANGED 'country' => $cleaned_current_page_country );
-
Do not use $_REQUEST. Use either $_POST or $_GET depending on your form's method attribute. (In this case, as you are updating the cart data, it should be POST.) session_start(); $cart_items = $_SESSION["cart_items"] ?? []; $id = $_POST["element_id"] ?? 0; $quantity = $_POST["quantity"] ?? 0; if ($id && $quantity) { if (!isset($cart_items[$id])) { $cart_items[$id] = 0; } $cart_items[$id] += $quantity; } var_dump($cart_items);
-
I have just noticed you don't use the $page_url in those pagination links. It looks like you intended to but then just used the query string $the_query = http_build_query($data); $page_url .= '?' . $the_query; echo '<li><a href=" '.$the_query.' ">'.$current_page.'</a></li> '; As you go to the same page that should be sufficient but I think you will need the preceding "?" Try $the_query = http_build_query($data); $the_query = '?' . $the_query; echo "<li><a href=\"$the_query\">$current_page</a></li>";
-
What does $page_url contain? Try var_dump($page_url) on line 7 after you set its value;
-
does it work if you put the "Italian.php" back in the middle of that url?
-
Have you checked the content of $data[0]? If it is blank, your query becomes "SELECT * FROM games WHERE id = ", and that would give you that syntax error. edit: P.S. You should be using a prepared statement when dealing with POST data