Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 12/09/2019 in all areas

  1. don't execute SELECT queries inside of loops. with today's server hardware, the time it takes for php to send the sql query statement to the database server is several times longer than the time it takes the database server to execute a query. you want to limit the number of queries you execute. postgreSQL has a LIMIT n OFFSET m statement that you should use for pagination. if for some reason you are not supposed to use that for this assignment, use php's array_slice() on the $listings array to get an array of the desired ids. then use an IN() operator to get the desired rows of data using one query. btw - the header() redirect needs an exit/die statement to stop program execution. without an exit/die, all the rest of that code is still executed. since you haven't shown us what the build_listing_card() code is or what output it produces, cannot help you with what or why your view listing links should be or don't work. don't write code like this either. every pg_fetch_result() call performs a data-seek, followed by a fetch. this takes twice as long a just fetching the data, and you have a couple of dozen pg_fetch_result() statements. the query in this code will match at most one row of data. just fetch that row into a php variable, then access elements of that fetched array. this will run measurably faster and take a lot less typing to produce the code needed for the page. if your initial long list of echo statements are for debugging purposes, just use print_r() or var_dump(), surrounded by html <pre>...</pre> tags to format the output. if this output is instead a desired part of the page output, don't spend your time typing out line after line of code for each possible field/column. use a loop and let php do the work for you.
    1 point
  2. Are you not storing the users' answers anywhere? Write a record to the "user_response" table when a user answers a question. Then you have a record of which questions each user has answered. Only select questions for a user where there is no record in the response table for that question. SELECT Question , Answer1 , Answer2 , Answer3 , CorrectAnswer FROM question q LEFT JOIN user_response r ON q.id = r.q_id WHERE r.q_id IS NULL ORDER BY RAND() LIMIT 1 +-----------+ +-----------+ | user | | question | +-----------+ +-----------+ | user_id |--+ +----| id | | name | | | | question | +-----------+ | | | etc | | | +-----------+ | | | +---------------+ | | | user_response | | | +---------------+ | | | q_id |>-+ +--<| user_id | | test_date | | answer | +---------------+
    1 point
This leaderboard is set to New York/GMT-04:00
×
×
  • 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.