Jump to content

Barand

Moderators
  • Posts

    24,566
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. see https://forums.phpfreaks.com/topic/303305-pdo-looping/?p=1543471&do=findComment&comment=1543471
  2. Then why are they output within a foreach loop?
  3. The times when you might want similar to that is when you want to specify the array indexes For example if ($stmt->execute()) { while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $products[ $row['user_id'] ] = $row; } } or to group results if ($stmt->execute()) { while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $products[ $row['cat_id'] ][] = $row; } }
  4. If, say, $_POST['sourcename'] is a string and not an array then $_POST['sourcename'][$i] with be the $ith character of the string and not the $ith element of an array.
  5. One query, get all users and store in an indexed array. Not separate numbered variables.
  6. In what way is that "outside the function"? $db = DB(); // connection OUTSIDE the function $vehicle_data = UserDetails($db, $user_id); // pass the $db connection as a parameter plus you need to redefine the function to accept the extra parameter function UserDetails ($db, $user_id) { // function code }
  7. It was better as it was. Just add a WHERE clause to tell it which user. $query = $db->prepare("SELECT users.user_id , users.registration , users.email , vehicle.reg_id , vehicle.vehicle , vehicle.registration , vehicle.chassis FROM users JOIN vehicle ON vehicle.registration = users.registration WHERE users.user_id = :user_id"); $query->bindParam("user_id", $user_id, PDO::PARAM_STR); It looks like the line $db = DB(); is connecting to your database. Move that code out of the function and pass $db as a parameter to your function with $user_id. Connecting is a relatively slow process and you don't want to do every for every query.
  8. Add those columns' names to the list of columns in your SELECT clause. EDIT: You now have a bound parameter but there is nowhere to bind the parameter to. You need a WHERE clause in the query
  9. Then that is what you should use for the join with your current tables. SELECT .... FROM user u JOIN vehicle v ON u.registration = v.registration However, a more flexible model would be ... +-------------+ | user | +-------------+ | user_id |--------+ | email | | +--------------+ | etc | | | vehicle | +-------------+ | +--------------+ | | vehicle_id | +--------<| user_id | | registration | | vehicle | | chassis_no | +--------------+ .. which easily allows you to add a second (and third...) car for a user just by adding another row with the user's user_id. In this model the join would be on the user_id columns.
  10. Your column names are ambiguous and confusing. Can you show us your table structures and some sample data?
  11. $data = []; is equivalent to $data = array();
  12. A sort function should return -ve, zero or +ve values depending on the comparison result. Yours will return a boolean result. This would achieve that return filemtime($x) - filemtime($y);
  13. You may find it better to store the date and contents in the array (with filename as the key). In which case a simple asort() will not work, you will need a custom sort function. For example $files['file1.txt'] = [ 'date' => '2017-01-03', 'content' => 'aaa' ]; $files['file2.txt'] = [ 'date' => '2017-01-01', 'content' => 'bbb' ]; $files['file3.txt'] = [ 'date' => '2017-01-02', 'content' => 'ccc' ]; $files['file4.txt'] = [ 'date' => '2017-01-04', 'content' => 'ddd' ]; uasort($files, function ($a, $b) { return strcmp ($a['date'], $b['date']); }); foreach ($files as $filename => $fdata) { // process the output }
  14. How can you say that when you have no knowledge of the application? Let's say I want to list names in three columns and want the numbers in each column to be as near equal as possible. For example I want A E I B F J C G K D H and not A F K B G C H D I E J I would be interested in your method of doing this without knowing how many there are.
  15. There is a clue in that error message - wrong parameters. So look up the function mysqli_select_db in the PHP manual and see what the parameters should be
  16. http://uk1.php.net/manual/en/class.pdo.php
  17. Benanamen's point was that you need to stop this happening future, so you don't have to keep repeating this exercise.
  18. You need session_start() at beginning of every script that needs to reference $_SESSION.
  19. You can create a table which has exactly the same structure as another CREATE TABLE table2 LIKE table1; I know of nothing similar down at the ADD COLUMN level.
  20. Sorry, careless with my column aliases Try SELECT DATE(payments.date) as dateportion , users.username , COUNT(*) as tot from payments INNER JOIN users ON payments.user_id = users.id WHERE payments.membership_id IN (6,7) GROUP BY payments.user_id, dateportion HAVING tot > 1
  21. Work with just the date parts of the fields using DATE(payments.date) SELECT DATE(date) as date , userid , COUNT(*) as tot FROM payments GROUP BY user_id, date HAVING tot > 1
  22. try $results = array( array("SALE_ID"=>"1","SALE_DATE"=>"2017-01-01"), array("SALE_ID"=>"2","SALE_DATE"=>"2017-02-15"), array("SALE_ID"=>"3","SALE_DATE"=>"2017-03-25"), array("SALE_ID"=>"4","SALE_DATE"=>"2017-04-10"), array("SALE_ID"=>"5","SALE_DATE"=>"2017-01-08"), array("SALE_ID"=>"6","SALE_DATE"=>"2017-02-23"), array("SALE_ID"=>"7","SALE_DATE"=>"2017-03-15"), array("SALE_ID"=>"8","SALE_DATE"=>"2017-04-09") ); $results_by_week = []; foreach ($results as $sale) { $w = week_number($sale['SALE_DATE']); $results_by_week[$w][] = (object)$sale; } ksort($results_by_week);
  23. Are you sure that is the same form - the hidden fields you originally posted are text fields in that form?
  24. reverse the array first (array_reverse())
  25. try removing the (..)s from around the insert statement.
×
×
  • 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.