![]() |
Hover & Show Data in PHP/SQL table
HI all..
I have a table generated by PHP from my SQL database.. it's quite simple.. in one column i have a identifier (which is not unique) and in another column i have a price. Is there any way to hover the table row, and have a small popup (or title?) displaying the lowest price for the column with the identifier? for instance.. i have a talbe with 300 records.. 25 of them have the identifier "Television" in the 1st column.. can i hover over a row(or just the identifier) with "Television" and have little pop-up tell me what the lowest price(located in column 2) associated with this identifier throughout the table is?? |
You can use the title attribute.
For the lowest price, that would best be done when selecting from the database using the MIN() function. Group by the identifier, and it will pull the minimum price. Can't work without any data, but that would look something like: PHP Code:
Take note though that this will only pull the lowest of the returned resultset, so if you limit it you will have skewed results. To pull from the entire table all the time, either use a second query or use a sub select query within the select. The SQL guys would be able to tell you which is better (I'd expect 2x queries to be better than 1x query with a nested select). |
This is a great idea..
what i have now is a bit simpler.. Code:
while($row = mysql_fetch_row($result))Maybe this thread should be moved to the SQL forum? |
2x queries would look like so:
PHP Code:
|
Quote:
Here's what i have after connecting, testing and printing the table headers: Code:
$sMinQry = 'SELECT `tag`, `price`, MIN(`price`) AS lowest FROM expedia GROUP BY `tag`'; |
Is your table actually named myproducts, or is it named expedia?
I didn't put an else or a die on the queries, so it won't tell you if it failed (for that I wrapped it in only processing if it was successful). BTW, instead of a foreach within a while loop, you can use vprintf in order to just give it an array to work with (and the format string of course). |
Quote:
if i add: Code:
elseHere's what i have now: Code:
if (!mysql_connect($db_host, $db_user, $db_pwd)) |
OK.. i think im on a good path here..
I'm doing this after connecting and printing the table headers: Code:
$result = mysql_query("SELECT * FROM {$table}");i'm able to get it done with a while within the if.. but i cant get back to the original SQL data.. (if you can't tell i've only been learning SQL for about 2 months.. sorry so noob) |
That will *NEVER* work!
Code:
SELECT `tag`, `price`, MIN(`price`) AS lowest FROM expedia GROUP BY `tag`WHY would you get *BOTH* price AND MIN(price)??? The real problem here is that MySQL is *TOO FLEXIBLE*. That fact that it even ALLOWS you to make that query is, in my mind, a DEFICIENCY. No other DB engine would allow it. Others would all complain that you used the field price but then did not put it in your GROUP BY. And if you then indeed used Code:
SELECT tag, price, MIN(price) AS lowest FROM expedia GROUP BY tag, priceThe *RIGHT* answer, in any DB, is to get rid of price in the query: Code:
SELECT tag, MIN(price) AS lowest FROM expedia GROUP BY tagCode:
SELECT tag, MIN(price) AS lowest FROM expedia GROUP BY tag ORDER BY lowest |
Oh you haven't queried anything. The $sQry is simply the string, but you don't have a mysql_query called upon it.
You definitely don't want to nest your queries if you can avoid it (usually you can). Calling a query within a query is many queries to the database which is much slower than a single large resultset query (at least I'm pretty sure its slower). Not to mention that the DB itself can be locked down to only accept x number of queries per hour, so you want to avoid looping with queries within them. Always goal for only 1 query per script (I've managed to get it down to 3 in a functional environment which has me very happy, but typically I'm between 8 and 15 due to how I build my objects). What I actually meant was this line here: PHP Code:
Edit: Old Pedant weighed in there. Oh yeah, your right! How did I miss that pulling price and min(price) would just result in the record of the price and the price. Lol. Oh not to mention I missed the price in the group by. |
If you wanted to see something like this:
Code:
group product price lowest priceAnd then your PHP code would just pull the data from the records and display it with no futher PHP processing. If that's what you are after, provide some sample data to work with and I'll show you how. |
Old pedant.. I'm sure you know better than me, but both must be displayed.. i guess you query price, and then somehow determine MIN?
Fou-Lu.. I did get this working with your script above... but when i try to add in all of my other fields it's dying out.. and I'm certain that it's my query that's doing so.. (p.s. i was running mysql_query and trying to dump the vars... just pasted too soon and forgot to update it... sorry about that) here are my table details fields: tag url price miles cpm leg1 departuredate leg2 arrivaldate leg3 finaldate localprice currency fxrate date I'm simply (it makes me feel worse actually that i keep saying "simply" lol) trying to dump these values into a table.. and show the minimum price available based on the 'tag', in the <tr title="The Minimum Price is: $MINPRICE on $URL"> of course i know thats not right.. but that's the idea behind it... basically a tag will be "NYC-DEN" and i will load the table with data for NYC-DEN flights.. then if you see "NYC-DEN" you can hover the table row, and it will give you a minimum price for that route.. does that make any sense? here is some quick sample data: Code:
sr_city-test expedia.eu 993.385 8885.36 7.80369 BHM-MAD 2013-03-03 MAD-BHM 2013-03-10 NULL NULL 740.27 EUR 0.779177 2012-11-23 02:32:35 |
Great. Now just what is it you want to display, GIVEN that sample data??
********* Just to demo the concept, here's my sample data: Code:
mysql> select * from prod;Code:
mysql> select p.prodgroup, p.product, p.price, m.lowestCode:
+------------+-----------+-------+--------+Not sure that's exactly what you want, and of course it doesn't relate all that well to the expedia data, but the point is that you can do everything in a single query. |
It *looks* to me like you could do something like this:
Code:
SELECT L.lowest, E.* |
So if you did that, you would dump out all the data for one row into your table, using PHP, and add in something like this:
Code:
<tr onmouseover="showMin(this,<?php echo $row['lowest'];?>)">NOTE: Do you REALLY want this grouped by tag??? Wouldn't it make more sense to have it grouped by (say) leg1? So you would have the lowest rate for all "BHM-MAD" flights? But that's up to you and a trivial change to the SQL query, of course. |
| All times are GMT +1. The time now is 11:16 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.