Jump to content

[SOLVED] Styling Issue


lukekelly

Recommended Posts

Hi All,

 

I have a small styling issue i need help with. in the script below i would like every other row to be coloured.

 

<style type="text/css">
.ay_client
{
border:2px solid #ccc;
background:#ff;
width:500px;
padding:10px;
text-align:center;
overflow:auto;
table-cell:2px solid #ccc;
}
td {
position: relative;
width: 14%;
margin: 0;
border-width: 1px 1px 1px 1px;
border-style: solid;
border-color: #000;
padding-top: 1em;
vertical-align: center;
}
</style>
<div class="ay_client">
<?php
$query =  mysql_query("SELECT * FROM jos_ay_client_activity");
{
echo $row['month'];
}
echo "<table><tr><td><b>Date Time</b></td> <td><b>Month</b></td> <td><b>Year</b></td> <td><b>Client</b></td> <td><b>Surveys</b></td> <td><b>Air Tests</b></td> <td><b>Project Managements</b></td> <td><b>Bulk Samples</b></td></tr>";

while($row = mysql_fetch_array($query))
{
    echo "<tr><td>";
    echo $row['datetime'];
    echo "</td><td>";
    echo $row['month'];
    echo "</td><td>";
    echo $row['year'];
    echo "</td><td>";
    echo $row['client'];
    echo "</td><td>";
    echo $row['surveys'];
    echo "</td><td>";
    echo $row['airtests'];
    echo "</td><td>";
    echo $row['project'];
    echo "</td><td>";
    echo $row['bulks'];
    echo "</td></tr>";
}

echo "</table>";
?></div>

 

I have found some solutions, but ofc im getting errors because of the php elements... any suggestions? :)

 

Thanks in advance

 

Luke

Link to comment
Share on other sites

Well as i said there were a few solutions (which i have since taken out of the code - because they did not work) the "solutions" i found were to...

 

<style type="text/css">
.highlightoff{
background-color:white;
}
.highlighton{
background-color: yellow;
}
</style>

Then this in every td tag:

class="highlightoff" onmouseover="this.className='highlighton'" onmouseout="this.className='highlightoff'"

 

That would highlight the cells when hovered over - not what i desired but it would suit.

 

This however seems nearer the solution:

 

$row = 25;
$x = 1;

while ($x <= $row) {
if($x%2): $color = "#FFFFFF"; else: $color = "#CCCCCC"; endif;
print "<tr><td style=\"background-color: ".$color."\">row #".$x."</td></tr>\n";
$x++;
}

 

But again cant seem to get it to work.

 

 

Link to comment
Share on other sites

this is what I normaly do for this.

 

tr.odd {
	background-color:#ffffff;
}

tr.even {
	background-color:#dff3f7;
}

 

 


<?php

echo '<table>';

for ($i = 0; $i < 10; $i++) {

$class - $i%2 = 0? 'odd':'even';

echo '<tr class="' . $class . '" ><td>hello</td></tr>';

}

echo '</table>';
?>

Link to comment
Share on other sites

sorry its ment to be

 

$class = $i%2 == 0? 'odd':'even';

 

Ok great now getting results on the page, however its not styling them :( as i test i just used your code:

 

<style type="text/css">
   tr.odd {
      background-color:#ffffff;
   }
   
   tr.even {
      background-color:#CCCCCC;
   }
</style>
<?php
echo '<table>';
for ($i = 0; $i < 10; $i++) {
$class - $i%2 == 0? 'odd':'even';
echo '<tr class="' . $class . '" ><td>hello</td></tr>';
}
echo '</table>';
?>

 

I get hello 10 times w/o any formatting/styling

 

somthing with

for ($i = 0; $i < 10; $i++) {

? seems if i change the variable < 10 to say 5 it will display 5 Hello's!

Link to comment
Share on other sites

Ok just a problem putting it together with my existing code :)

 

Here is how i've implemented it so far:

 

<style type="text/css">
.ay_client
{
border:2px solid #ccc;
background:#ff;
width:500px;
padding:10px;
text-align:center;
overflow:auto;
table-cell:2px solid #ccc;
}
td {
position: relative;
width: 14%;
margin: 0;
border-width: 1px 1px 1px 1px;
border-style: solid;
border-color: #000;
padding-top: 1em;
vertical-align: center;
}
tr.odd {
      background-color:#ffffff;
}
tr.even {
      background-color:#CCCCCC;
}
</style>
<div class="ay_client">
<?php
$query =  mysql_query("SELECT * FROM jos_ay_client_activity");
{
echo $row['month'];
}
echo '<table><tr><td><b>Date Time</b></td> <td><b>Month</b></td> <td><b>Year</b></td> <td><b>Client</b></td> <td><b>Surveys</b></td> <td><b>Air Tests</b></td> <td><b>Project Managements</b></td> <td><b>Bulk Samples</b></td></tr></table>';

while($row = mysql_fetch_array($query))
echo '<table>;
for ($i = 0; $i < 10; $i++)
{
$class = $i%2 == 0? 'odd':'even';
    echo "<tr class="' . $class . '" ><td>";
    echo $row['datetime'];
    echo "</td><td>";
    echo $row['month'];
    echo "</td><td>";
    echo $row['year'];
    echo "</td><td>";
    echo $row['client'];
    echo "</td><td>";
    echo $row['surveys'];
    echo "</td><td>";
    echo $row['airtests'];
    echo "</td><td>";
    echo $row['project'];
    echo "</td><td>";
    echo $row['bulks'];
    echo "</td></tr>";
}

echo "</table>'";
?></div>

 

Im getting -

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'  eval()'d code on line 41

 

41 is

$class = $i%2 == 0? 'odd':'even';

 

I cant make out what it is wanting, i added another ; on the end; which ofc wasn't right... anyone see anything that clearly stands out as being obvious?

 

Thanks in advance.

Link to comment
Share on other sites

Hey thanks for your reply, unfortunately that was not the problem solved, a step further this time but im getting an error on the row below now

 

echo "<tr class="' . $class . '" ><td>";

 

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' eval()'d code on line 42.

 

::)

 

 

Link to comment
Share on other sites

Ok so i am no getting results with

 

<style type="text/css">
.ay_client
{
border:2px solid #ccc;
background:#fff;
width:500px;
padding:10px;
text-align:center;
overflow:auto;
table-cell:2px solid #ccc;
}
td {
position: relative;
width: 14%;
margin: 0;
border-width: 1px 1px 1px 1px;
border-style: solid;
border-color: #000;
padding-top: 1em;
vertical-align: center;
}
tr.odd {
      background-color:#ffffff;
}
tr.even {
      background-color:#cccccc;
}
</style>
<div class="ay_client">
<?php
$query =  mysql_query("SELECT * FROM jos_ay_client_activity");
{
echo $row['month'];
}
?><table><tr><td><b>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=datetime&dir=ASC">
<img src="images/M_images/sort0.png"></a>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=datetime&dir=DESC">
<img src="images/M_images/sort1.png"></a>
<br>Entered</b></td> 
<td><b>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=month&dir=ASC">
<img src="images/M_images/sort0.png"></a>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=month&dir=DESC">
<img src="images/M_images/sort1.png"></a>
<br>Month</b></td> 
<td><b>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=year&dir=ASC">
<img src="images/M_images/sort0.png"></a>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=year&dir=DESC">
<img src="images/M_images/sort1.png"></a>
<br>Year</b></td> 
<td><b>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=client&dir=ASC">
<img src="images/M_images/sort0.png"></a>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=client&dir=DESC">
<img src="images/M_images/sort1.png"></a>
<br>Client</b></td> 
<td><b>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=surveys&dir=ASC">
<img src="images/M_images/sort0.png"></a>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=surveys&dir=DESC">
<img src="images/M_images/sort1.png"></a>
<br>Surveys</b></td> 
<td><b>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=airtests&dir=ASC">
<img src="images/M_images/sort0.png"></a>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=airtests&dir=DESC">
<img src="images/M_images/sort1.png"></a>
<br>Air Tests</b></td> 
<td><b>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=project&dir=ASC">
<img src="images/M_images/sort0.png"></a>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=project&dir=DESC">
<img src="images/M_images/sort1.png"></a>
<br>Projects</b></td> 
<td><b>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=bulks&dir=ASC">
<img src="images/M_images/sort0.png"></a>
<a href="index.php?option=com_jumi&fileid=4&Itemid=50?orderby=bulks&dir=DESC">
<img src="images/M_images/sort1.png"></a>
<br>Bulks</b></td></tr></table>
<?php
while($row = mysql_fetch_array($query)) {
echo '<table>';
for ($i = 0; $i < 10; $i++)
{
$class = $i%2 == 0? 'odd':'even';
    echo "<tr class='" . $class . "' ><td>";
    echo $row['datetime'];
    echo "</td><td>";
    echo $row['month'];
    echo "</td><td>";
    echo $row['year'];
    echo "</td><td>";
    echo $row['client'];
    echo "</td><td>";
    echo $row['surveys'];
    echo "</td><td>";
    echo $row['airtests'];
    echo "</td><td>";
    echo $row['project'];
    echo "</td><td>";
    echo $row['bulks'];
    echo "</td></tr>";
}

echo "</table>"; }
?></div>

 

Only problem is i'm getting each result returned 10 times! If

for ($i = 0; $i < 10; $i++)

 

Is changed to

for ($i = 0; $i < 1; $i++) 

 

I get correct results but no formatting...

Link to comment
Share on other sites

Take out the for loop and add a counter:

 

<?php
$count = 0;
while($row = mysql_fetch_array($query)) {
    echo '<table>';
    $class = $count%2 == 0? 'odd':'even';
    echo "<tr class='" . $class . "' ><td>";
    echo $row['datetime'];
    echo "</td><td>";
    echo $row['month'];
    echo "</td><td>";
    echo $row['year'];
    echo "</td><td>";
    echo $row['client'];
    echo "</td><td>";
    echo $row['surveys'];
    echo "</td><td>";
    echo $row['airtests'];
    echo "</td><td>";
    echo $row['project'];
    echo "</td><td>";
    echo $row['bulks'];
    echo "</td></tr>";
    echo "</table>";
    $count++;
}

Link to comment
Share on other sites

One little tweak to j.Daniels code:

 

$count = 0;
echo '</pre>
<table>';
while($row = mysql_fetch_array($query)) {    
    $class = $count%2 == 0 ? 'odd' : 'even';
    echo "";
    echo $row['datetime'];
    echo "";
    echo $row['month'];
    echo "";
    echo $row['year'];
    echo "";
    echo $row['client'];
    echo "";
    echo $row['surveys'];
    echo "";
    echo $row['airtests'];
    echo "";
    echo $row['project'];
    echo "";
    echo $row['bulks'];
    echo "";  
    $count++;
}
echo "</table>

 

Link to comment
Share on other sites

Thanks a lot for your replys guys - thats done the job nicely - i will now compare the two codes to see what you have changed so i can learn from this and for everyone's benefit:

 

Added in the counter:

 

$count = 0;

 

    $count++;

 

Changed:

 

$class = $i%2 == 0? 'odd':'even';

 

to

 

$class = $count%2 == 0 ? 'odd' : 'even';

 

Now that its sorted i can logically see the problem was the line with $i affecting the result outcome. After that the code was tidied up.

 

However I'm still not 100% sure when you use a loop so i will read up on that :)

 

But once again thanks for your help, has been insightful and prompt.

 

Link to comment
Share on other sites

Just to clarify, you already had a loop -- which was the mysql_fetch_array() loop.  When you do a a query you get a result SET.  That set could be anything from no rows, to the size of your tables.    Each time that line returns true, a row gets fetched, and the variable gets incremented. 

 

So the other thing I changes was moving the table definition outside the loop, because initially it was making a table every time a row was fetched, and I inferred that what you really wanted was one table, and to make a new table row for each row fetched from the result set. 

 

The simple modulus 2 function of course gives you the functionality to alternate the style name. 

 

So to clarify, you have a loop and a variable being incremented every time the loop runs.  Your initial mistake was that even though you arleady had the while() loop, which runs until the result set is finished, you were unclear I guess that you already had what you needed, so you stuck in a for() loop, which was not what you wanted, as you already had the loop you needed.

 

HTH.

 

 

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.