Forum Moderators: coopster

Message Too Old, No Replies

Building PHP table from SQL database

Want to display SQL data in a table using PHP

         

robbiesqp

9:15 pm on Jul 20, 2010 (gmt 0)

10+ Year Member



Hey everyone,

I'm relatively new to web development and PHP, but I'm beginning to get the hang of it. I searched and found that this topic was covered a couple of times already in a few threads ([webmasterworld.com ] and [webmasterworld.com ]). I've also Googled this and found some helpful resources, however I'm still having some trouble.

My goal is to display data from an SQL database in a table on a website using PHP and HTML to create the table. I have succeeded in creating a table, which I can get to properly display data from a .txt file, for example. I've also properly connected to the SQL database, but the problem is getting the fields I want from the SQL table to populate the PHP table. (If that makes any sense)

I have two different web pages I'm trying to do this on. On the first one, I only want to display the very last row of SQL data (the SQL table I'm pulling the data from has 2359 rows of data so I don't exactly want all that appearing on the web page).


The data in the SQL table is meteorological data from a buoy. The database is called "riverine" and the table is "data".

I have it set up so the rows alternate in color, which looks best on the web page. So here's what I've come up with:


<?php
mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select_db("riverine");
$query="SELECT * FROM data";
$result=mysql_query($query);

$i=0;
while ($i < 1)
{

$lat=mysql_result($result,$i,"lat");
$long=mysql_result($result,$i,"long");
$speed=mysql_result($result,$i,"speed");
$dir=mysql_result($result,$i,"dir");
$depth=mysql_result($result,$i,"depth");
$wtemp=mysql_result($result,$i,"wtemp";
$hour=mysql_result($result,$i,"hour";
$min=mysql_result($result,$i,"min";
$sec=mysql_result($result,$i,"sec";

print("<em style=\"font-weight: normal;\"><span style=\"font-size: 12pt;\">Data Processed at $hour:$min:$sec</span></em>");
echo "<table border= \"4\" cellpadding=\"2\"cellspacing=\"1\" width=\"60%\">";
echo "<tr bgcolor=#fffff0><td><b>Latitude</b></td><td>$lat</td><td> degrees</td></tr>";
echo "<tr bgcolor=#f0f8fe><td><b>Longitude</b></td><td>$long</td><td> degrees</td></tr>";
echo "<tr bgcolor=#fffff0><td><b>Drifter Speed</b></td><td>$speed</td><td>kts</td></tr>";
echo "<tr bgcolor=#f0f8fe><td><b>Drifter Direction</b></td><td>$dir</td><td>degrees(T)</td></tr>";
echo "<tr bgcolor=#fffff0><td><b>Water depth</b></td><td>$depth</td><td>meters</td></tr>";
echo "<tr bgcolor=#f0f8fe><td><b>Water temp</b></td><td>$wtemp</td><td>&#176;C</td></tr>";
echo "</table>";

$i++;
}
?>


On the "while" loop, I just made it "while ($i < 1)" because I only want the very last row entry (row #2359 to be exact), although I think that may actually give me the first row... I would just put 2359 in there, but this database is going to be updated every 15 minutes or so with new data, so I need to be able to tell it just take the very last row, if there's a way to do that.

Now the above code just displays a blank page in the browser (yes I have PHP installed correctly), however when I remove this fragment


$lat=mysql_result($result,$i,"lat");
$long=mysql_result($result,$i,"long");
$speed=mysql_result($result,$i,"speed");
$dir=mysql_result($result,$i,"dir");
$depth=mysql_result($result,$i,"depth");
$wtemp=mysql_result($result,$i,"wtemp";
$hour=mysql_result($result,$i,"hour";
$min=mysql_result($result,$i,"min";
$sec=mysql_result($result,$i,"sec";


the web page, along with the table, displays perfectly. The problem is, the cells that I want to have my data in are blank. The thing is, by removing the above fragment, I'm not defining those variables, and I understand that is why the cells would be blank.

And if I put a "$num= row_count" type thing in there and set it to "while ($i < $num), it successfully prints out 2359 rows of blank cells on the web page. So I know for a fact this is connecting to the database correctly and I know all my table formatting is fine. And I know the problem is I just need to define those variables in the table ($lat, $long, etc), but I just cannot figure out how. I've tried mysql_result, fetch_array, fetch_assoc, all that stuff, but like I said, I'm a noob with this.

Ok so that's the first web page. The second one is similar, I just want the last 25 rows to display in a table. It's the same exact formatting pretty much (I can do the table formatting), it's just I only want the last 25 rows of the "data" table to be displayed.

I hope I've been clear enough in my questions. If not, I can try to clarify. Any and all help is very much appreciated!

-Robbie

Alcoholico

10:12 pm on Jul 20, 2010 (gmt 0)

10+ Year Member



First, you have some missing closing parentheses:

$wtemp=mysql_result($result,$i,"wtemp";
$hour=mysql_result($result,$i,"hour";
$min=mysql_result($result,$i,"min";
$sec=mysql_result($result,$i,"sec";


Should be:

$wtemp=mysql_result($result,$i,"wtemp");
$hour=mysql_result($result,$i,"hour");
$min=mysql_result($result,$i,"min");
$sec=mysql_result($result,$i,"sec");


You should turn on error reporting so php will inform you about these syntax errors. However, I do not think that solves your problem, mysql_result() is not really an efficient function, does your mysql table have any keys? if so, what are they? what are hour, minute, second? you could simply order the results in MySQL using "ORDER" and "LIMIT" so your script does not retrieve the whole table.

Matthew1980

10:37 pm on Jul 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there robbiesqp,

mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select_db("riverine");
$query="SELECT * FROM data";
$result=mysql_query($query);


Maybe I am wrong, but surely you need to reference the mysql connection reference into the select_db & _query() functions in order for it to know where the connection handle is coming from?

for example:-

$ConnRef = mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select_db("riverine", $ConnRef);

$query = "SELECT * FROM data";
$result=mysql_query($query, $ConnRef) or die(mysql_error());


But note this; subsequent calls to mysql_query will inherit the last known connection reference as a default connection if one isn't defined - I generally define the first then inherit the rest, unless I need to use a separate connection (which is rare :))

And yes, Alcoholico's point is spot on - as too is the point about error_reporting(E_ALL); it really needs turning on for doing development work, then once happy it can be removed as it can pose a security risk it left and and the site goes live ;)

Cheers,
MRb

rocknbil

1:12 am on Jul 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The white page errors are most likely the missing quotes as mentioned. But we're going to make those go away anyway.

I don't exactly want all that appearing on the web page).


Nor do you want to do this.

$query="SELECT * FROM data";

You're still eating up resources stepping through the entire record set when you just need one record. Select what you want:

$query="SELECT * FROM data order by id desc limit 1";

Selecting only one record, you don't need a while loop - we'll get into while later. :-)


<?php
$link = mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select_db("riverine",$link);
$query="SELECT * FROM data order by id desc limit 1";
$result=mysql_query($query);
$content=null;
if ($row = mysql_fetch_array($result)) {
$content = "
<span style=\" font-weight:normal; font-style:italic; font-size: 12px;\">Data Processed at
$row['hour'] . ':' . $row['min'] . ':' . $row['sec'] . "</span>
<table border= \"4\" cellpadding=\"2\"cellspacing=\"1\" width=\"60%\">
<tr style=\"background:#fffff0\"><td><b>Latitude</b></td><td>" .$row['lat'] .
"</td><td> degrees</td></tr>
<tr style=\"background:#f0f8fe\"><td><b>Longitude</b></td><td>" . $row['long'] .
"</td><td> degrees</td></tr>
<tr style=\"background:#fffff0\"><td><b>Drifter Speed</b></td><td>" . $row['speed'] .
"</td><td>kts</td></tr>
<tr style=\"background:#f0f8fe\"><td><b>Drifter Direction</b></td><td>" . $row['dir'] .
"</td><td>degrees(T)</td></tr>
<tr style=\"background:#fffff0\"><td><b>Water depth</b></td><td>" . $row['depth'] .
"</td><td>meters</td></tr>
<tr style=\"background:#f0f8fe\"><td><b>Water temp</b></td><td>" . $row['wtemp'] .
"</td><td>&#176;C</td></tr>
</table>
";
}
if ($content) { echo $content; }
else { echo "<p>No records found.</p>"; }
?>

I just want the last 25 rows to display in a table.


Using the above, you already know the answer. :-) But what I'll never understand is why this has got such a deep root in PHP, I see it everywhere:

$i=0;
while ($i < [some number, usually mysql_num_rows])
{

I know you probably picked it up from someone else, but an enumerator is never needed here. This is where you can use a while on the data. a few minor changes, and you're good to go.

<?php
$link = mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select_db("riverine",$link);
$query="SELECT * FROM data order by id desc limit 25";
$result=mysql_query($query);
$content=null;
while ($row = mysql_fetch_array($result)) {
// Note concatenation, not =
$content .= "
<span style=\" font-weight:normal; font-style:italic; font-size: 12px;\">Data Processed at
$row['hour'] . ':' . $row['min'] . ':' . $row['sec'] . "</span>
<table border= \"4\" cellpadding=\"2\"cellspacing=\"1\" width=\"60%\">
<tr style=\"background:#fffff0\"><td><b>Latitude</b></td><td>" .$row['lat'] .
"</td><td> degrees</td></tr>
<tr style=\"background:#f0f8fe\"><td><b>Longitude</b></td><td>" . $row['long'] .
"</td><td> degrees</td></tr>
<tr style=\"background:#fffff0\"><td><b>Drifter Speed</b></td><td>" . $row['speed'] .
"</td><td>kts</td></tr>
<tr style=\"background:#f0f8fe\"><td><b>Drifter Direction</b></td><td>" . $row['dir'] .
"</td><td>degrees(T)</td></tr>
<tr style=\"background:#fffff0\"><td><b>Water depth</b></td><td>" . $row['depth'] .
"</td><td>meters</td></tr>
<tr style=\"background:#f0f8fe\"><td><b>Water temp</b></td><td>" . $row['wtemp'] .
"</td><td>&#176;C</td></tr>
</table>
";
}
if ($content) { echo $content; }
else { echo "<p>No records found.</p>"; }
?>


The only hitch with that is if you want the last 25 ordered ascending. Then you'd do a count of the records, subtract 25 from that, and do this instead.

// Let's say $start = 2521, or something

$query="SELECT * FROM data order by id asc limit $start, 25";

robbiesqp

4:43 pm on Jul 21, 2010 (gmt 0)

10+ Year Member



Thank you all so much for the help!

With regards to the error reporting, I reinstalled Ubuntu (because 10.04 is so buggy it crashes like every 30 minutes for me) and I forgot to set display_error back to ON. So I went ahead and did that.

Now, I took the code from rocknbil (thanks a lot!) and put it in there in place of everything I had. Then I got the error
unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /status.php on line 148
which is this line from rocknbil's code:

$row['hour'] . ':' . $row['min'] . ':' . $row['sec'] . "</span> 


I figured the problem here was the lack of quotes at the beginning of the line and I was right! I use Kate to edit this stuff, so the colors reflected a correct change and the error went away. However, I now receive the error
unexpected T_VARIABLE in /status.php on line 148
which is still


<span style=\" font-weight:normal; font-style:italic; font-size: 12px;\">Data Processed at
"$row['hour'] . ':' . $row['min'] . ':' . $row['sec'] ."</span>


Line 148 is the line beginning "$row['hour'] so on and so forth.

So I thought the problem might be a lack of quotes in between the different "$row[]" elements, so I went ahead and put

"$row['hour']" . ':' . "$row['min']" . ':' . "$row['sec']" .</span>


But the "unexpected T_VARIABLE on line 148" error remains. I'm thinking it may have something to do with the apostrophes around the colons, but I just don't know.

I tried just removing the whole "if" statement altogether and "echo"ing each line, which allowed me to view the webpage finally, but the table didn't show up and I got some error in its place. But I'd prefer to do it rocknbil's way, because that makes a lot more sense, I just need to find this unexpected t_variable..

Besides that, thank you all so much for your help! I really appreciate it! And with regards to the enumerator I was using, rocknbil you said


I know you probably picked it up from someone else


and you would be right! I was googling this for a while just trying to find out anything that worked. That was from some tutorial I found. Your way makes a lot more sense. I really appreciate the help, everyone.

eta: Did I post this question in the correct forum? If not, would it have been better to post it in the "new to web development" forum or somewhere else? Thanks

rocknbil

5:17 pm on Jul 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Agh, forgot my disclaimer, if they ever allow "signatures": around here this will be mine:


-------------------------------------------------------
** in an effort to assist members with tasks, rocknbil**
** often types code on the fly. By default, it is **
** likely to contain syntax errors and typos. It is **
** not copy and paste code. Learn what ye may, toss **
******* what ye don't code forth and debug. *********
-------------------------------------------------------


Yes this is a PHP issue, I think you're OK here.

I figured the problem here was the lack of quotes at the beginning of the line and I was right!


Yes and no - it is quoting, but not where you thought. By adding the quote, it just moved on you. I don't have time to create a test database to fully debug, which is what it would take, but here's how you sort it.

First, "double quoting" does two things: it allows scalar variables to interpolate:

$foo = 'blah';

echo "Foo is $foo"; // echoes foo is blah

and it supports multilines.

echo "
Foo
is
$foo
";

So your presumption that it was the beginning of the line is incorrect, look:

$content = "
<span style=\".....

There were two problems here. I'd missed a concatenation dot AND a quote, but not at the beginning, at the end. Look between the word "at" and the first instance of "$row" on the next line:


$content = "
<span style=\" font-weight:normal; font-style:italic; font-size: 12px;\">Data Processed at " .
$row['hour'] . ':' . $row['min'] . ':' . $row['sec'] . "</span> ......


OK? Now that same error exists in the second example, fix it there too.

Second, per the quoting, this does NOT work:

"$row['min']"

It will work on scalars but not arrays. You can use curlies for array indices or HEREDOC syntax, but don't want to confuse you - this is why I concatenated at that point, an easy way to avoid single quotes in HTML (a bane from my retentive HTML output side.) The downside is, of course, it's easy to miss quotes, as I've done here.

So take those off, note the two points of error above, there may be more.

One more bit of advice, a simple debugging method. When you get TSPACE or other errors in what's supposed to be "output," look for the most logical block to eliminate and have the program still work. in the example, look at the entire content block. Cut it so that it looks like this:

$content .= "
Bill dorked it here
";

If it runs, you begin re-adding code one chunk at a time until you find the error. This is a good home-grown way to fix stupid typo errors. :-)

robbiesqp

8:00 pm on Jul 21, 2010 (gmt 0)

10+ Year Member



First off - it works! Thank you all so much! I would post a screenshot of the final result, but I guess you aren't allowed to post images on this forum.

I do actually still have one issue to resolve. When I initially said that the second web page was the same as the first, but just with 25 rows of the data, I wasn't exactly clear. While the code provided by the esteemed rocknbil does indeed work (with the minor tweaks as discussed), I actually want the table to look different than the other one.

Here is the code I've got


<?php
$link = mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select_db("riverine",$link);
$query="SELECT * FROM data order by uid desc limit 25";
$result=mysql_query($query,$link) or die(mysql_error());
$content=null;
while ($row = mysql_fetch_array($result))
{
// Note concatenation, not =
$content = "
<table border= \"5\" cellpadding=\"2\"cellspacing=\"2\" width=\"100%\">
<tr><td>Latitude (degrees)</td><td>Longitude (degrees)</td><td><a href=Plotspeed.php>Speed (kts)</a></td><td><a href=Plotcog.php>Dir (TRUE)</a></td><td><a href=Plotdepth.php>Depth (m)</a></td><td>Water Temp (&#176;C)</td></font>
<tr bgcolor=#fffff0><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#f0f8fe><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#fffff0><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#f0f8fe><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#fffff0><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#f0f8fe><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#fffff0><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#f0f8fe><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#fffff0><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#f0f8fe><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#fffff0><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#f0f8fe><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#fffff0><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#f0f8fe><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#fffff0><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#f0f8fe><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#fffff0><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#f0f8fe><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#fffff0><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#f0f8fe><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#fffff0><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#f0f8fe><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#fffff0><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#f0f8fe><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
<tr bgcolor=#fffff0><td>" . $row['lat'] . "</td><td>" . $row['long'] . "</td><td>" . $row['speed'] . "</td><td>" . $row['dir'] . "</td><td>" . $row['depth'] . "</td><td>" . $row['wtemp'] . "</td></font>
</table>";
}
if ($content)
{
echo $content;
}
else
{
echo "<p>No records found.</p>";
}
echo nl2br ("\n");
echo nl2br ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
?>


I know it's kind of tedious, but I just went ahead and copy and pasted 25 rows (with the alternating colors) so it would display just 25 rows, instead of 25 tables. This code is perfectly fine and does display data, however it is showing the same entry over and over (the 25th entry from the bottom).

rocknbil, I kind of see what you're saying about "$start, 25" in the SELECT query, but how do I define the variable to be the 25th from the bottom?

I'm sure there's an easy way to tell it, "just select the last 25 rows" and only have two of the <tr color="whatever"> instead of 25 like I have in that code. Would something like $variable = count($something); do the trick and then do "for ($i=0; $i<$variable; $i++)" ? I know you said those things were bad in PHP, but it's just a thought.

Thanks again everyone! Especially rocknbil! This forum is great!

rocknbil

2:14 am on Jul 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<blushes> You're actually learning this through me, from all the others here who I've learned from. Anyway

however it is showing the same entry over and over (the 25th entry from the bottom).


And it would be . . . because you're starting at the end, and descending, then overwriting with every row. You missed the meaning of this:

// Note concatenation, not =


When you do a while loop, you're stepping through a set of records. If you did
while .....
print $row[0] . '</br>';
}
you'd see 2500,2499,2498,2497 . . . . so what you're doing here is overwriting each value of $content. See?

$content = "

if you did this,

$content .= "

it would display 25 of each row of data. Concatination is like this.

$var = 'hey';
$var .= 'hey';
echo $var; // heyhey

So you want to "add up" the content of the total rows.

Alternating rows is easy, how you'd get the index to start on is also easy. Since I copied this and modified it from your post, there**shouldn't** be any errors, no promises. :-)


<?php
$link = mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select_db("riverine",$link);
//
$query="select count(*)-25 from data"; // count of total records minus 25
$result=mysql_query($query,$link) or die(mysql_error());
$row = mysql_fetch_array($result);
$start_here = $row[0]; // this is your start index.
//
$content=null;
$query="select * from data order by uid asc limit $start_here, 25";
$result=mysql_query($query,$link) or die(mysql_error());
$content=null;
while ($row = mysql_fetch_array($result)) {
// Construct a TOGGLE, if it's gray, make it white, otherwise, make it gray.
$bg = ($bg=='#fff')?'#f0f8fe':'#fff';
//
// Note concatenation, not = (!)
// Use styles, eventually you'll learn to turn them into classes.
$content .= "
<tr style=\"background:$bg;\">
<td>" . $row['lat'] . "</td>
<td>" . $row['long'] . "</td>
<td>" . $row['speed'] . "</td>
<td>" . $row['dir'] . "</td>
<td>" . $row['depth'] . "</td>
<td>" . $row['wtemp'] . "</td>
</tr>
";
}
if ($content) {
// See note on the "quote switch" here
echo '
<table border= "5" cellpadding="2" cellspacing="2" width="100%">
<tr>
<td>Latitude (degrees)</td>
<td>Longitude (degrees)</td>
<td><a href="Plotspeed.php">Speed (kts)</a></td>
<td><a href="Plotcog.php">Dir (TRUE)</a></td>
<td><a href="Plotdepth.php">Depth (m)</a></td>
<td>Water Temp (&#176;C)</td>
</tr>' .
$content . '
</table>
';
}
else { echo "<p>No records found.</p>"; }
echo '<p style="padding-bottom:24px;">&nbsp;</p>';
?>


I had a feeling this would be a better display of the data. But note that you don't print a <table> unless you have data, so I moved the headers down below. A lot easier to read this way too.

I pulled a couple switches on you with single quotes. Sonce there's only **one** variable to print, $content, note how the table and header are single quoted so you can use valid quoting in your links without escaping \". Keep in mind single quotes don't interpolate, do whatever is more efficient for you to code:

$blah = 'something';
echo "Blah is $blah"; // Blah is something
echo 'Blah is $blah'; // Blah is $blah

I know you said those things were bad in PHP, but it's just a thought.


Using a counter to step through database records is just not efficient, I don't see the reasoning. It's like investing in building another house when there's already one there that's bigger and paid for. There are lots of good uses for counters, just not in this context.

robbiesqp

5:38 pm on Jul 22, 2010 (gmt 0)

10+ Year Member



rocknbil comes through in the clutch yet again! I copy and pasted that code and, without any changes, it worked like a charm! Thanks for the help!

However, it does say variable $bg is undefined, but it's just a small error that appears above the table. The table itself displays perfectly and the website is formatted correctly.

Like I said, I'm a big time noob. A month ago, I didn't even know the acronym "PHP" existed and I've really been learning a lot in this internship this summer.

I think I get what you're saying about the concatenation. I decided to add another element to the table to see if I could do it. Here's what I did:

I added this little bit to the $content .= " part

<td>" . $row['month']. "/" . $row['day']. "/" . $row['year'] . " at " . $row['hour'] . ":" . $row['min'] . ":" . $row['sec'] . "</td>


and then gave it a

<td>Updated</td


for its header. And it looks great!

Still trying to wrap my head around all these concepts and such.

Also, this isn't crucial, but I'd like the very last row from the SQL table to be my very first row on the web page table. What do I need to change in the "select" queries to do that?


Using a counter to step through database records is just not efficient, I don't see the reasoning. It's like investing in building another house when there's already one there that's bigger and paid for. There are lots of good uses for counters, just not in this context.


I'll just take your word for it because I'm a noob haha. You're the pro, rocknbil. Many many thanks for all this help.

One quick question regarding these forums... is there an accepted etiquette or anything on these forums that dictates the amount of threads that a user should start in a certain time frame? Since I've been interning this summer and working on this company website, I got the idea to make a personal weather website that's coming together pretty nicely. However, I do have some questions that I'd like to post, but don't want to litter these boards with a bunch of threads. Thanks again

rocknbil

6:57 pm on Jul 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't think there's a set rule on how many threads, but "we don't do homework" and put sufficient effort into solving it yourself, I think . . . mods will have comments there.

variable $bg is undefined


<groans> the very first time, right. Change this

$content=null;

to this

$content=$bg=null;

and it will go away. This first time it loops through, there is no $bg.

I'd like the very last row from the SQL table to be my very first row


Come on . . . you can get this. :-) What did you change from the first incarnation (with the tables) to this last one? I'll give you a hint: it's in the order by clause of the select statement, you changed one thing. Not the limit or where clause . . . order by. What direction is it going in?

Matthew1980

7:41 pm on Jul 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there robbiesqp,

is there an accepted etiquette or anything on these forums that dictates the amount of threads that a user should start in a certain time frame?

Guidelines:[webmasterworld.com ]
Noobie:[webmasterworld.com ]
"The watercooler of WebmasterWorld"[webmasterworld.com ]

That's a good question - I would post that in the WebmasterWorld forum (first link) I don't think there is anyway. I suppose that the key is to research lots first, experiment, experiment a bit more then when your not sure what's going wrong, post a question, there are lots of knowledgeable people here who will help you - but just as Rocknbil points out - were not here to do your homework, lots of people just browse this site on their breaks from work or when they have a few moment, so reading HUGE code dumps will put people off from posting reply's.

I too have found this an excellent resource of the years & been helped out loads of times.

Have fun with your new projects & enjoy the work experience ;)

Cheers,
MRb