Jump to content
Larry Ullman's Book Forums

Recommended Posts

Assuming I have made this query:

$q = "SELECT c_frame + c_glass + c_materials + c_fees + c_commissions + c_other AS total_cost,
image_title, sold, price, b_sell_price, print_available, DATE_FORMAT(date_created, '%Y-%m-%d') 
AS dc,  DATE_FORMAT(date_added, '%Y-%m-%d') AS da, DATE_FORMAT(b_date_sold, '%Y-%m-%d') AS ds  
FROM art WHERE ($date_col_name >=  '$begin_date') AND ($date_col_name <= '$end_date') ORDER BY $order_by"

$r = @mysqli_query ($dbc, $q);

If I save the value of $r so it is available after re-sending the page from the server, can I then resort the query differently than the original sort rather than making the same query again?

 

I have column headings in my table that look like this:

 

<th><a href="overview.php?sort=image_title">Title</a></th> (so the data can be resorted)

 

However the original query (above) has selected data according to a date range. I would like to keep the exact results from the original query and sort it according to the column specified in the <th></th>.

 

I hope I am being clear.

 

Link to comment
Share on other sites

The $r variable is a pointer to a result set, which is to say it points to the results of that query and only that query. You cannot just change part of the query and then fetch the results, as that's a new query. If the goal is to reuse the previously selected dates, you should either store them in a cookie or session or also pass them along in URLs (such as the link to change the sorting). 

Link to comment
Share on other sites

Thank you. I had a feeling there was something there that I hadn't understood. I'm not sure "where" that result set is that gets pointed to so it creates a bit of a void in my "grokking"  it. That's okay though, I can accept that as part of the mystery and see that "mysqli_fetch_array" is the way (and only way) to access it wherever it's hiding out.

 

I think I'll try passing the dates in the URL contained in the <th> tags, then do the query again... if I understand you correctly.

 

thanks again

Link to comment
Share on other sites

You can look at it this way: mysqli_query() runs the query on the database as if you had run it directly using the command-line MySQL client. mysql_fetch_array() fetches results into PHP. So as soon as you've run mysqli_query(), the query's results already exist for fetching. 

 

The main issue here is how you maintain previously selected values--specifically dates, and you can do that by either using a storage mechanism such as a session or by passing those values along in the URL just the same as you would the sorting preference.

Link to comment
Share on other sites

 Share

×
×
  • Create New...