Jump to content

PHP View Info from Database


vigiw

Recommended Posts

I am trying to create a page in PHP that will get data from my MySQL database and show it in a table with specific colors, and also specific fonts.

Here's what I have so far:

[code]<?
include("dbaccess.php");
$q = mysql_query("SELECT * from `" . $table_name . "swc_alertanalysis");
while($row = mysql_fetch_assoc($q)) {
echo "<table border=\"1\" width=\"100%\" id=\"table3\">\n";
echo "  <tr>\n";
echo "<td>Storm ID</td>";
echo "<td>Status</td>";
echo "<td>Updated</td>";
echo "<td>Valid On</td>";
echo "<td>Conditions</td>";
echo "<td>Location</td>";
echo "<td>Likelihood</td>";
echo "<td>Risk</td>";
echo "<td>Alert Mode Consideration</td></tr>";
echo "<tr>";
echo "<td>";
echo "1</td>";

}
if ($status == 'active')
{
echo "<td bgcolor=red>ACTIVE</td>";
}
elseif ($status == 'watching')
{
echo "<td bgcolor="#00FFFF">WATCHING</td>";

}
if ($likelihood == 'THREAT')
{
echo "<td bgcolor=aqua><font face="Arial" color="#000000">THREAT</td></font>";
}
elseif ($likelihood == 'POSSIBLE')
{
echo "<td align="center" bgcolor="blue"><font face="Arial" color="#FFFFFF">POSSIBLE</font></td>";
}
elseif ($likelihood == 'LIKELY')
{
echo "<td align="center" bgcolor="orange"><font face="Arial" color="#FFFFFF">LIKELY</font></td>";
}
elseif ($likelihood == 'VERY LIKELY')
{
echo "<td align="center" bgcolor="red"><font face="Arial" color="#FFFFFF">VERY LIKELY</font></td>";
}

if ($risk == 'LOW')
{
echo "<td bgcolor=aqua><font face="Arial" color="#000000">LOW</td></font>";
}
elseif ($risk == 'MODERATE')
{
echo "<td align="center" bgcolor="blue"><font face="Arial" color="#FFFFFF">MODERATE</font></td>";
}
elseif ($risk == 'HIGH')
{
echo "<td align="center" bgcolor="orange"><font face="Arial" color="#FFFFFF">HIGH</font></td>";
}
elseif ($risk == 'EXTREME')
{
echo "<td align="center" bgcolor="red"><font face="Arial" color="#FFFFFF">EXTREME</font></td>";
}

if ($consideration == 'UNLIKELY')
{
echo "<td bgcolor=gray><font face="Arial" color="#FFFFFF">UNLIKELY</td></font>";
}
elseif ($consideration == 'POSSIBLE')
{
echo "<td align="center" bgcolor="aqua"><font face="Arial" color="#000000">POSSIBLE</font></td>";
}
elseif ($consideration == 'IN CONSIDERATION')
{
echo "<td align="center" bgcolor="blue"><font face="Arial" color="#FFFFFF">IN CONSIDERATION</font></td>";
}
elseif ($consideration == 'LIKELY')
{
echo "<td align="center" bgcolor="orange"><font face="Arial" color="#FFFFFF">LIKELY</font></td>";
}
elseif ($consideration == 'VERY LIKELY')
{
echo "<td align="center" bgcolor="red"><font face="Arial" color="#FFFFFF">VERY LIKELY</font></td>";
}
if(mysql_num_rows($q) > 1) { echo "<tr><td align="center" bgcolor="gray"><font face="Arial" color="#FFFFFF">-</font></td><td align="center" bgcolor="gray"><font face="Arial" color="#FFFFFF">INACTIVE</font></td></tr>";
}
?>[/code]

When I view what I have on my website for this page I see:

Parse error: parse error, unexpected T_IF, expecting ',' or ';' in /home/vigilant/public_html/forecast/swc/view.php on line 29

Here's line 29:

[code]}[/code]

And when I delete that bracket, line 30 becomes line 29 and the error becomes--

Parse error: parse error, unexpected T_IF, expecting ',' or ';' in /home/vigilant/public_html/forecast/swc/view.php on line 29

The changed file's line 29, or the complete code above's line 30 is:

[code]if ($likelihood == 'THREAT')
[/code]

This file consists of multiple if statements and other things as you see.

I am clearly not the best with PHP and am quite the beginner with it, but this is really stumping me!

Please help if you could, thanks!!
Link to comment
Share on other sites

Wow, your code is a complete mess.

[code]echo "<td bgcolor="#00FFFF">WATCHING</td>";[/code]

You're basically trying to start a new string on that line.

[code]echo "<td bgcolor='".#00FFFF."'>WATCHING</td>";[/code]

Escape your quotes. You can't have another set of double quotes inside another unless you escape them.

So basically it's:
[code]singlequote(')doublequote(")period(.)TEXTINSIDEHEREperiod(.)doublequote(")singlequote(')[/code]

As long as TEXTINSIDEHERE doesn't include any single or double quotes or any periods it will work fine. You need to clean up your code too, put some indents in there so you can recognize individual if and elseif statements.
Link to comment
Share on other sites

Thank you for your help!

The Quote-Eliminating part worked, I didn't indent yet, so far I can tell them apart.

But here's the new error, may have something to do with my code being either MySQL 4 or MySQL 5 compatible.  My server is running MySQL 5.0, and I am getting these errors:


Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/vigilant/public_html/forecast/swc/view.php on line 4

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/vigilant/public_html/forecast/swc/view.php on line 69

Here's 4:

while($row = mysql_fetch_assoc($q)) {

Here's 69:

if(mysql_num_rows($q) > 1) { echo "<tr><td align=center bgcolor=gray><font face=Arial color=#FFFFFF>-</font></td><td align=center bgcolor=gray><font face=Arial color=#FFFFFF>INACTIVE</font></td></tr>";

Anything noticeable?

Thanks again!
Link to comment
Share on other sites

Well now my only error is line 69, which I altered a bit to seperate just in case there was too much in one "quote section" ("x" . ".....";)

Here's the error message:

Parse error: parse error, unexpected '<' in /home/vigilant/public_html/forecast/swc/view.php on line 69

Here's the line:


if(mysql_num_rows($q) > 1) { echo "<tr><td align=center bgcolor=gray><font face=Arial color=#FFFFFF>-</font></td>" . "<td align=center bgcolor=gray><font face=Arial color=#FFFFFF">INACTIVE</font></td></tr>"; }

Hmm.. which '<' is it referring to??

Thanks for your help everyone, it is really coming along! :)
Link to comment
Share on other sites

it's referring to the second line of that.

[code]
if(mysql_num_rows($q) > 1) {
echo "<tr><td align=center bgcolor=gray><font face=Arial color=#FFFFFF>-</font></td>";
echo "<td align=center bgcolor=gray><font face=Arial color=#FFFFFF">INACTIVE</font></td></tr>";
}
[/code]
Link to comment
Share on other sites

You need to look at your code more carefully. Look for the stray quotes.

hint: it's right before INACTIVE.

You should use a single quote to start and end your string since HTML attributes should have double quotes surrounding them:
[quote]$str = '<td><p style="className">Text</p></td>';[/quote]
You also don't need to concat your two strings, just make one string.

And use CSS!!!
Link to comment
Share on other sites

Ah, of course.. thanks...

Back to that MySQL error on 4 and 69.

How do I make it so 69 will be called up if the table is empty, and 4 if it is not.

This is the beginning of call 1 for the big display for full data-- here's lines 3 through 4.

[code]$q = mysql_query("SELECT * from `" . $table_name . "swc_alertanalysis");
while($row = mysql_fetch_assoc($q)) {
[/code]

And of course, 69, changed since last:

[code]if(mysql_num_rows($q) > 1) { echo "<tr><td align=center bgcolor=gray><font face=Arial color=#FFFFFF>-</font></td>" . "<td align=center bgcolor=gray><font face=Arial color=#FFFFFF>INACTIVE</font></td></tr>"; }
[/code]

Thanks!
Link to comment
Share on other sites

[quote author=jesirose link=topic=119797.msg490943#msg490943 date=1166930969]

[quote]$str = '<td><p style="className">Text</p></td>';[/quote]

[/quote]

Well, it wouldn't matter either way. But it would matter if he were to echo a variable. You could always escape the double quotes.

[code]
$str = "<td><p style='".classname."'>$var1 is a person</p></td>";
[/code]
Link to comment
Share on other sites

mgall, what you did to my $str doesn't make sense.
I've never heard of using a single quote to escape a quote. It's a \...are you thinking of sql? Please show me in the docs where it says you can escape like that. It would be
$str = "<p className=\"$name\"></p>";


viwig - you know how to create if else statements ;)
Link to comment
Share on other sites

I was pointing out that if you were to echo out YOUR $str line and it contained a variable ($) inside of it, that it would echo off the variable name instead of the content of the variable.

[code=php:0]
<?php
$varname = hello;
$str = '<td><p class="classname">$varname</p></td>';

echo $str;
?>
[/code]

In the table cell it would echo off $varname instead of hello, unless you went my way

[code=php:0]
<?php
$varname = hello;
$str = "<td><p class='".classname."'>$varname</p></td>";
echo $str
?>
[/code]

In which case you would get the return of hello instead of $varname.
Link to comment
Share on other sites

What you are writing creates this.
<td><p class='classname'>hello</p></td>

Which is still using single quotes. My point was that AFAIK, HTML attributes should be double quoted. So if you really wanted to surround your string in double quotes, you'd need to escape them using \". What you're doing is just concatenating a bunch of strings.

If you're going to bother concatenating strings, use
$str = '<p class="className">'.$var.'</p>';

But since the OP is trying to alter the style based on code, the classname should be a variable.
$str = '<p class="'.$className.'">'.$var.'</p>';
OR
$str = "<p class=\"$className\">$var</p>";

It's a matter of preference, but I'm going to go check on the HTML part. I think they have to be double quotes.
Link to comment
Share on other sites

Hello,

Sorry, I might not have been to clear there..

I meant, how do I make it so there are "valid supplied arguments" based on the same code.

Should I convert it to something like this:

[code]<?
include("dbaccess.php");
$q = mysql_query("SELECT * FROM `swc_alertanalysis`") or die (mysql_error());
echo "<table border=\"1\" width=\"100%\" id=\"table3\">\n" . "<tr>\n" . "<td>Storm ID</td>" . "<td>Status</td>" . "<td>Updated</td>" . "<td>Valid On</td>" . "<td>Conditions</td>" . "<td>Location</td>" . "<td>Likelihood</td>" . "<td>Risk</td>" . "<td>Alert Mode Consideration</td></tr>" . "<tr><td>1</td>";
if (mysql_num_rows($q) == 0)
{
echo "<tr><td align=center bgcolor=gray><font face=Arial color=#FFFFFF>-</font></td>" . "<td align=center bgcolor=gray><font face=Arial color=#FFFFFF>INACTIVE</font></td></tr>"; }

}
elseif (mysql_num_rows($q) > 0)
echo "if ($status == 'active')
{
echo "<td bgcolor=red>ACTIVE</td>";
}
elseif ($status == 'watching')
{
echo "<td bgcolor=#00FFFF>WATCHING</td>";
}
if ($likelihood == 'THREAT')
{
echo "<td bgcolor=aqua><font face=Arial color=#000000>THREAT</td></font>";
}
elseif ($likelihood == 'POSSIBLE')
{
echo "<td align=center bgcolor=blue><font face=Arial color=#FFFFFF>POSSIBLE</font></td>";
}
elseif ($likelihood == 'LIKELY')
{
echo "<td align=center bgcolor=orange><font face=Arial color=#FFFFFF>LIKELY</font></td>";
}
elseif ($likelihood == 'VERY LIKELY')
{
echo "<td align=center bgcolor=red><font face=Arial color=#FFFFFF>VERY LIKELY</font></td>";
}

if ($risk == 'LOW')
{
echo "<td bgcolor=aqua><font face=Arial color=#000000>LOW</td></font>";
}
elseif ($risk == 'MODERATE')
{
echo "<td align=center bgcolor=blue><font face=Arial color=#FFFFFF>MODERATE</font></td>";
}
elseif ($risk == 'HIGH')
{
echo "<td align=center bgcolor=orange><font face=Arial color=#FFFFFF>HIGH</font></td>";
}
elseif ($risk == 'EXTREME')
{
echo "<td align=center bgcolor=red><font face=Arial color=#FFFFFF>EXTREME</font></td>";
}

if ($consideration == 'UNLIKELY')
{
echo "<td bgcolor=gray><font face=Arial color=#FFFFFF>UNLIKELY</td></font>";
}
elseif ($consideration == 'POSSIBLE')
{
echo "<td align=center bgcolor=aqua><font face=Arial color=#000000>POSSIBLE</font></td>";
}
elseif ($consideration == 'IN CONSIDERATION')
{
echo "<td align=center bgcolor=blue><font face=Arial color=#FFFFFF>IN CONSIDERATION</font></td>";
}
elseif ($consideration == 'LIKELY')
{
echo "<td align=center bgcolor=orange><font face=Arial color=#FFFFFF>LIKELY</font></td>";
}
elseif ($consideration == 'VERY LIKELY')
{
echo "<td align=center bgcolor=red><font face=Arial color=#FFFFFF>VERY LIKELY</font></td>";
}
if(mysql_num_rows($q) > 1) { echo "<tr><td align=center bgcolor=gray><font face=Arial color=#FFFFFF>-</font></td>" . "<td align=center bgcolor=gray><font face=Arial color=#FFFFFF>INACTIVE</font></td></tr>"; }
?>[/code]
Link to comment
Share on other sites

XHTML specs say you must quote attributes. Gotta keep up with the times.

There most certainly is a point. What about when you want to do this:
<p class="sub class">.
Can you do <p class=sub class>? I think this will not work.
I use multiple classes often.

Just because it used to be okay to leave them unquoted doesn't mean it will always be that way ;)
Link to comment
Share on other sites

Going back to my last reply..

I get this error for the most recent code I suggested--

Parse error: parse error, unexpected '}' in /home/vigilant/public_html/forecast/swc/view.php on line 9

here's 7 through 10:

[code]echo "<tr><td align=center bgcolor=gray><font face=Arial color=#FFFFFF>-</font></td>" . "<td align=center bgcolor=gray><font face=Arial color=#FFFFFF>INACTIVE</font></td></tr>"; }

}
elseif (mysql_num_rows($q) > 0)
[/code]
Link to comment
Share on other sites

Hiya Mike

[quote]So...invalid HTML is okay if everything is invalid? :/[/quote]

I would say so. It doesn't matter how high the quality his code is, I purposely think he would want the code to work before going to fix it to an XHTML standard.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.