Note that there are some explanatory texts on larger screens.

plurals
  1. POPainting Table with Session Values
    primarykey
    data
    text
    <p>Let's say I have a dynamically-created table that is filled with data from a MySQL db from a <code>SELECT</code> sentence.</p> <p>Now, I need to access a variable that is defined inside of an <code>if</code> clause inside of the table, from the outside. For making that, I'm using some <code>$_SESSION</code> values, but when I try to apply this coloring to the entire row, it colors up all the table instead of a single row in which the session value is stored. I'm guessing that I need to save also the <code>$RowCt</code> value, but dunno how.</p> <p>I've been with this problem for quite a while, and looks like I'm a little bit blinded after looking at this code continuously. Any help is appreciated.</p> <p>Code:</p> <pre><code>&lt;?php include("../includes/header.php")?&gt; &lt;div class="content" align="center"&gt; &lt;?php function SQLResultTable($Query) { $link = mysql_connect("localhost", "...", "...") or die('Could not connect: ' . mysql_error()); mysql_select_db("...") or die('Could not select database'); $Table = ""; $Table.= "&lt;table border='0' align='center'&gt;"; $Result = mysql_query($Query); //Execute the query if(mysql_error()) { $Table.= "&lt;tr&gt;&lt;td&gt;MySQL ERROR: " . mysql_error() . "&lt;/td&gt;&lt;/tr&gt;"; } else { //Header Row with Field Names $NumFields = mysql_num_fields($Result); $Table.= "&lt;tr style=\"background-color: #D00000; float:center; font-size:14px; color: #FFFFFF;\" align='center'&gt;"; for ($i=0; $i &lt; $NumFields; $i++) { if ((mysql_field_name($Result, $i))=="a1") { $Table.= "&lt;th&gt;a1&lt;/th&gt;"; } else if ((mysql_field_name($Result, $i))=="a2") { $Table.= "&lt;th&gt;a2&lt;/th&gt;"; } else if ((mysql_field_name($Result, $i))=="a3") { $Table.= "&lt;th&gt;a3&lt;/th&gt;"; } else if ((mysql_field_name($Result, $i))=="a4") { $Table.= "&lt;th&gt;a4&lt;/th&gt;"; } else if ((mysql_field_name($Result, $i))=="a5") { $Table.= "&lt;th&gt;a5&lt;/th&gt;"; } else if ((mysql_field_name($Result, $i))=="a6") { $Table.= "&lt;th&gt;a6&lt;/th&gt;"; } else if ((mysql_field_name($Result, $i))=="a7") { $Table.= "&lt;th&gt;a7&lt;/th&gt;"; } else if ((mysql_field_name($Result, $i))=="a8") { $Table.= "&lt;th&gt;a8&lt;/th&gt;"; } else if ((mysql_field_name($Result, $i))=="a9") { $Table.= "&lt;th&gt;a9&lt;/th&gt;"; } else if ((mysql_field_name($Result, $i))=="a10") { $Table.= "&lt;th&gt;a10&lt;/th&gt;"; } else if ((mysql_field_name($Result, $i))=="a11") { $Table.= "&lt;th&gt;a11&lt;/th&gt;"; } else if ((mysql_field_name($Result, $i))=="a12") { $Table.= "&lt;th&gt;a12&lt;/th&gt;"; } else { $Table.= "&lt;th&gt;" . mysql_field_name($Result, $i) . "&lt;/th&gt;"; } } $Table.= "&lt;/tr&gt;"; $RowCt = 0; while($Row = mysql_fetch_assoc($Result)) { $timezone = new DateTimeZone( "Europe/London" ); $date = new DateTime(); $date-&gt;setTimezone( $timezone ); $now = $date-&gt;format( 'H:i' ); if (!isset($_SESSION['b2'])) { $Table.="&lt;tr style='background-color:red;'&gt;"; } else if (!isset($_SESSION['b1'])) { $Table.="&lt;tr style='background-color:yellow;'&gt;"; } else { $Table.="&lt;tr style='background-color:white;'&gt;"; } foreach($Row as $field =&gt; $value) { if ($field == 'b1') { if ($value &gt; $now) { $_SESSION['b1'] = $b1; $Table.= "&lt;td style='font-size:14px;' align='center'&gt;$value&lt;/td&gt;"; } else { $Table.= "&lt;td style='font-size:14px;' align='center'&gt;$value&lt;/td&gt;"; } } else if ($field == 'b2') { if ($value &gt; $now) { $_SESSION['b2'] = $b2; $Table.= "&lt;td style='font-size:14px;' align='center'&gt;$value&lt;/td&gt;"; } else { $Table.= "&lt;td style='font-size:14px;' align='center'&gt;$value&lt;/td&gt;"; } } else { $Table.= "&lt;td style='font-size:14px;' align='center'&gt;$value&lt;/td&gt;"; } } $Table.= "&lt;/tr&gt;"; } $Table.= "&lt;tr style=\"background-color: #FFFFFF; font-size:10px; color: #000000;\"&gt;&lt;td colspan='$NumFields'&gt;&lt;br /&gt;Query returned " . mysql_num_rows($Result) . " result(s).&lt;/td&gt;&lt;/tr&gt;"; } $Table.= "&lt;/table&gt;"; return $Table; } ?&gt; &lt;?php echo "&lt;div align='center'&gt;&lt;u&gt;&lt;strong&gt;&lt;a href='index.php'&gt;Query Results&lt;/a&gt;&lt;/strong&gt;&lt;/u&gt;&lt;/div&gt;"; echo SQLResultTable("SELECT a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12 FROM test_db ORDER BY a6"); ?&gt; &lt;/div&gt; </code></pre> <p></p> <p>Now, the <code>$now</code> value returns the actual hour, i.e. 08:30; and <code>$value</code> on fields <code>b1</code> and <code>b2</code> returns a similar value on the same format that is stored from a previous form.</p> <p>The deal is this: When it's late (value on field b1 is greater than now), it colors up the row on yellow. If it's very late (value on field b2 is greater than now), it colors up the row on red. Otherwise, the row is on a white background-color.</p> <p>With the code above, it colors up the entire table. And can't find how to undo this so it colors <em>just</em> the row on which the session value is stored, aka where its late or very late.</p> <p>Any help is appreciated, ask if you need any more data.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload