I have inherited a system at work that is used to display names from a MySQL database on a plasma screen. Depending on a condition in the database some names are marked in red to highlight them.
Currently the names are arranged in rows of 10 across the screen, which works fine, but recently when we added extra names we discovered the PHP code has been written in such a way that each row of 10 names is a table. Each table then select a range of names from the database and posts them to the screen like thisQuote:
"SELECT * FROM 'post' ORDER BY name ASC LIMIT 10,10";To add more names I have had to create a new table for each row of names, then increment the ASC LIMIT values 11,10 21,10, 31,10 etc.
Is there a quick way to do this that will automatically increase the range when more names are added over time? The code looks really messy and I am sure that there is a better way of doing it but my PHP skills are pretty much non existant.
The SQL that Johnny's posted will give you all the rows, sorted by name.
Can you grab a screenshot of how you want the resulting table to look? (assuming you want a table that is).
If it was me doing it and you wanted a table, the PHP would look something like this:
Assumption is that you have already established the link to the database and are using the standard MySQL calls (rather than an ODBC link for example).
$sql = "SELECT * FROM 'post' ORDER BY name ASC"
$result = mysql_query($sql);
if (!$result) {
die('Could not query:' . mysql_error());
}
echo "";
while ($row = mysql_fetch_assoc($result)) {
echo "<tr>";
for ($i=0; $i < mysql_num_fields($result); $i++) {
echo "";
echo $row[$i];
echo "";
}
echo "</tr>";
}
mysql_free_result($result);
echo "";
This item was edited on Saturday, 14th May 2011, 11:02
The formatting didn't come out too well there...and without my Apache & MySQL server to play on (above done from memory) I may have got the syntax slightly wrong.
The bit I don't like with any SQL is selecting "*" from a database. It's ambiguous.
If someone happens to adjust the schema (i.e. adds / removes fields) then all related code is possibly wrong.
It's better to define the fields explicitly in my opinion, then output them explicitly in the PHP so you know exactly what you're getting.
Heh, any PHP experts and you ask a SQL question. ![]()
By not specifying a LIMIT it would return all possible results for a query, which as Miles shows above can be iterated to give you as many tables as required to show all the results. It's also a much more efficient way of doing it than executing separate queries for each table generated.
Editor
DVD REVIEWER
MYREVIEWER.COM
My Flickr Photostream
I don't know php but
http://www.w3schools.com/
has good tutorials and examples I've used for html, .asp, javascript, jscript and sql.
they have a php section
http://www.w3schools.com/php/default.asp
This item was edited on Saturday, 14th May 2011, 11:06
Well, technically it is a PHP question Rob (just that the source of the rows he wants in the table(s)) is derived from an SQL statement ![]()
Technically it was a SQL one!
Anyway, I'm moving on now before I declare that PHP is the spawn of satan and the worst language every to grace the universe since VisualBasic...
Editor
DVD REVIEWER
MYREVIEWER.COM
My Flickr Photostream
Quote:
Robert John Shepherd says...
Technically it was a SQL one!
Yeah yeah, I guess so.
Quote:
Robert John Shepherd says...
I'm moving on now before I declare that PHP is the spawn of satan and the worst language every to grace the universe since VisualBasic... Awww, don't say that. I love PHP.
I've been using it for years, it's so easy to pick up, and I love the fact you can run it so easily using a LAMP installation for example (unlike IIS which is just a pain in the arse).
I have been doing quite a lot of object oriented PHP recently and have gone back and redesigned a number of my original web applications at work which have had a big performance improvement as a result.