vigiw Posted December 24, 2006 Share Posted December 24, 2006 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 29Here'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 29The 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!! Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/ Share on other sites More sharing options...
marcus Posted December 24, 2006 Share Posted December 24, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147148 Share on other sites More sharing options...
marcus Posted December 24, 2006 Share Posted December 24, 2006 As far as indentation something like:[code]if($var1 = $var){echo "hello";}else {echo "wrong";} if....{ ... } ... } if...{ ... } ... } if...{ ... } ... }if...{...}...}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147151 Share on other sites More sharing options...
vigiw Posted December 24, 2006 Author Share Posted December 24, 2006 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 4Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/vigilant/public_html/forecast/swc/view.php on line 69Here'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! Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147152 Share on other sites More sharing options...
marcus Posted December 24, 2006 Share Posted December 24, 2006 You have a sql syntax error.Just do something like[code]$q = mysql_query("SELECT * FROM `tablename`") or die (mysql_error());[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147153 Share on other sites More sharing options...
marcus Posted December 24, 2006 Share Posted December 24, 2006 If you start something you have to end it. On your query line ($q) it shows you have a never ended `. And I would go about just writing it in, instead of using $table_name Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147155 Share on other sites More sharing options...
Jessica Posted December 24, 2006 Share Posted December 24, 2006 That's probably why that line has an error, you're not setting a table. What is that variable supposed to be?You should really set your styles using classes in CSS too. Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147160 Share on other sites More sharing options...
vigiw Posted December 24, 2006 Author Share Posted December 24, 2006 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 69Here'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! :) Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147161 Share on other sites More sharing options...
marcus Posted December 24, 2006 Share Posted December 24, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147162 Share on other sites More sharing options...
Jessica Posted December 24, 2006 Share Posted December 24, 2006 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147163 Share on other sites More sharing options...
vigiw Posted December 24, 2006 Author Share Posted December 24, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147164 Share on other sites More sharing options...
marcus Posted December 24, 2006 Share Posted December 24, 2006 [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] Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147165 Share on other sites More sharing options...
marcus Posted December 24, 2006 Share Posted December 24, 2006 [code]if(mysql_num_rows($q) == 0){echo "no info in the table";}else {echo "there is information in the table}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147166 Share on other sites More sharing options...
Jessica Posted December 24, 2006 Share Posted December 24, 2006 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 ;) Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147167 Share on other sites More sharing options...
marcus Posted December 24, 2006 Share Posted December 24, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147168 Share on other sites More sharing options...
ataria Posted December 24, 2006 Share Posted December 24, 2006 yoyo. Marucs. Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147169 Share on other sites More sharing options...
Jessica Posted December 24, 2006 Share Posted December 24, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147170 Share on other sites More sharing options...
vigiw Posted December 24, 2006 Author Share Posted December 24, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147172 Share on other sites More sharing options...
ataria Posted December 24, 2006 Share Posted December 24, 2006 There is no point for the quotes... <p class=classname>end of story. Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147173 Share on other sites More sharing options...
Jessica Posted December 24, 2006 Share Posted December 24, 2006 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 ;) Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147174 Share on other sites More sharing options...
marcus Posted December 24, 2006 Share Posted December 24, 2006 But for all we know, he might not even be using a doctype. Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147175 Share on other sites More sharing options...
Jessica Posted December 24, 2006 Share Posted December 24, 2006 [quote author=mgallforever link=topic=119797.msg490955#msg490955 date=1166932303]But for all we know, he might not even be using a doctype.[/quote]So...invalid HTML is okay if everything is invalid? :/ Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147176 Share on other sites More sharing options...
vigiw Posted December 24, 2006 Author Share Posted December 24, 2006 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 9here'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] Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147177 Share on other sites More sharing options...
marcus Posted December 24, 2006 Share Posted December 24, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147178 Share on other sites More sharing options...
Jessica Posted December 24, 2006 Share Posted December 24, 2006 Dude, you gotta read your code. These are simple errors that you need to look for, not post every typo.Good luck. Ignore my trying to enforce good standards upon your html, just thought I'd try to start you on the right foot. Quote Link to comment https://forums.phpfreaks.com/topic/31742-php-view-info-from-database/#findComment-147179 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.