Jump to content

zebra stripe dynamic table for ie8


mdemetri2

Recommended Posts

Thanks for the reply Jessica. This is the latest bit of code I have been playing with that was supposed to work in IE8...:


<?php require_once('Connections/Connection1.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_Connection1, $Connection1);
$query_recordset1 = "select * from users";
$recordset1 = mysql_query($query_recordset1, $Connection1) or die(mysql_error());
$row_recordset1 = mysql_fetch_assoc($recordset1);
$totalRows_recordset1 = mysql_num_rows($recordset1);
?>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script><script type="text/javascript">

$(function() {    
    //
    $('.tableClass tr:even').addClass('even');
    $('.tableClass tr:odd').addClass('odd');
});
</script>

<style type="text/css">

.tableClass{
}
.odd { background-color: blue; }
.even { background-color: white; }

</style>



<html>
    <head>
    <title>How To Create Zebra Striped Table</title>
</head>
<body>

<table width="600" border="0">
  <tr>
    <td> </td>
  </tr>
  <?php do { ?>
    <tr>
      <td><span class="tableClass"> <?php echo $row_recordset1['userid']; ?></span></td>
    </tr>
    <?php } while ($row_recordset1 = mysql_fetch_assoc($recordset1)); ?>
</table>
<p class="tableClass"> </p>
</body>
</html>
<?php
mysql_free_result($recordset1);
?>

any ideas where it is wrong? this isnt actually working in chrome either.... :-\

Link to comment
Share on other sites

It shouldn't work in any browser, there's nothing to actually put the stripes on the rows.

 

A. That should be a while loop, not a do-while.

B. You should have a variable to hold the number of rows, then alternate the class based on that. Here's a simple example:

<?php
$num = 0;
while ($row_recordset1 = mysql_fetch_assoc($recordset1)){
    $num++;
    $class = $num%2 ? 'even' : 'odd';
    echo '<tr class="'.$class.'">';
    echo '<td>Row Data Here</td>';
    echo '</tr>';
}
?>

Edit: I see the JavaScript now that was supposed to do it. I strongly recommend using PHP instead, unless you dynamically alter the table with JS later. Your JS isn't working because you're putting the tableClass class on a span, not the table row. So .tableClass tr never exists.

Edited by Jessica
Link to comment
Share on other sites

Ok, it seems I chose a poor example from my google search results, apologies. So, can I use your example instead, remove my use of .tableClass and keep the .odd and .even ones? Also, do I replace Row Data Here with echo $row_recordset1['userid'];

Link to comment
Share on other sites

Ok, I have gone back to a simple table, that is populated by a query. Jessica - please would you be able to provide some pointers on where I need to adapt your example to make the following alternate the colour on the rows:


<?php require_once('Connections/Connection1.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_Connection1, $Connection1);
$query_recordset1 = "select * from users";
$recordset1 = mysql_query($query_recordset1, $Connection1) or die(mysql_error());
$row_recordset1 = mysql_fetch_assoc($recordset1);
$totalRows_recordset1 = mysql_num_rows($recordset1);
?>
<style type="text/css">
.odd { background-color: blue; }
.even { background-color: white; }
</style>



<html>
    <head>
    <title>How To Create Zebra Striped Table</title>
</head>
<body>
<table width="600" border="0">
  <tr>
    <td>User</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><?php echo $row_recordset1['userid']; ?></td>
    </tr>
    <?php } while ($row_recordset1 = mysql_fetch_assoc($recordset1)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($recordset1);
?>

Link to comment
Share on other sites

put an initializer here

<?php $count = 0; do { ?>

Then on every row, check if it is odd or even

<td class="<?php echo (($count % 2) == 0) ? 'even' : 'odd'; ?>"><?php echo $row_recordset1['userid']; ?></td>
Edited by Eiseth
Link to comment
Share on other sites

many thanks for the reply and help Eiseth, I now have the following code:

<?php require_once('Connections/Connection1.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_Connection1, $Connection1);
$query_recordset1 = "select * from users";
$recordset1 = mysql_query($query_recordset1, $Connection1) or die(mysql_error());
$row_recordset1 = mysql_fetch_assoc($recordset1);
$totalRows_recordset1 = mysql_num_rows($recordset1);
?>
<style type="text/css">
.odd { background-color: blue; }
.even { background-color: white; }
</style>
<html>
    <head>
    <title>How To Create Zebra Striped Table</title>
</head>
<body>
<table width="600" border="0">
  <tr>
    <td>User</td>
  </tr>
  <?php $count = 0; do { ?>
    <tr>
      <td class="<?php echo (($count % 2) == 0) ? 'even' : 'odd'; ?>"><?php echo $row_recordset1['userid']; ?></td>
    </tr>
    <?php } while ($row_recordset1 = mysql_fetch_assoc($recordset1)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($recordset1);
?>

and it still isn't working. As you can tell I'm poking around in the dark here being new to this somewhat!

Link to comment
Share on other sites

Try putting your style below the title

<html>
<head>
    <title>How To Create Zebra Striped Table</title>
    <style type="text/css">
        .odd { background-color: blue; }
        .even { background-color: white; }
    </style>
</head>
Edited by Eiseth
Link to comment
Share on other sites

Try putting your style below the title

<html>
<head>
    <title>How To Create Zebra Striped Table</title>
    <style type="text/css">
        .odd { background-color: blue; }
        .even { background-color: white; }
    </style>
</head>

 

What? No.

Link to comment
Share on other sites

Thanks to both of you. Ok, so I moved the style section to after the title section.

 

Jessica, you said I need to increment the counter but I am really not sure what to change and therefore how to do this.....I assume something like $count + 1 somewhere? I really do need to find out how to understand your code and break it down into what each part is doing....

 

<table width="600" border="0">
  <tr>
    <td>User</td>
  </tr>
  <?php $count = 0; do { ?>
    <tr>
      <td class="<?php echo (($count % 2) == 0) ? 'even' : 'odd'; ?>"><?php echo $row_recordset1['userid']; ?></td>
    </tr>
    <?php } while ($row_recordset1 = mysql_fetch_assoc($recordset1)); ?>
</table>

Link to comment
Share on other sites

erm, $num++;   ??

And by comparing what I have and what you sent I also realised that I am not using what you provided.....probably because I failed miserably to implement it the first time round, and then got changing it using other reponses or things I found! It's been one of those days....I will try yours again.

Link to comment
Share on other sites

holy crap lol. I just amended what I had to this:

 

<table width="600" border="0">
  <tr>
    <td>User</td>
  </tr>
  <?php $count = 0; do { ?>
    <tr>
    <?php $count++;?>
      <td class="<?php echo (($count % 2) == 0) ? 'even' : 'odd'; ?>"><?php echo $row_recordset1['userid']; ?></td>
    </tr>
    <?php } while ($row_recordset1 = mysql_fetch_assoc($recordset1)); ?>
</table>

 

And it bloody works!

 

However is it a messy way of doing it? And...I dont really understand what is goind on specifically! BUT Jessica you have been excellent help....can you recommend any decent books for this stuff?

Link to comment
Share on other sites

I don't really learn by reading books so I can't. However there is a thread somewhere here with a list of good ones.

The PHP manual is very useful. Follow their examples, read the comments, read the manual pages about all the basics.

 

It would look less messy if you don't start/stop PHP all the time.

Link to comment
Share on other sites

So far wonderful help, thank you.

 

To extend on this a little, is it possible to alter the now working code, so that it says 'None' if there are no records returned by the query? So I imagine a check that says if the recordset count = 0 then echo 'None'....?

Link to comment
Share on other sites

sorry wrong terminology, I meant the code that works for striping the rows:

<?php $count = 0; do { ?>
          <tr>
          <?php $count++;?>
            <td colspan = "5" class="<?php echo (($count % 2) == 0) ? 'even' : 'odd'; ?>"><?php echo $row_RecordSet1['userid']; ?></td>
          </tr>
          <?php } while ($row_RecordSet1 = mysql_fetch_assoc($RecordSet1)); ?>
Link to comment
Share on other sites

Okay so that is your code, what is the problem?

 

You really need to switch to using a while loop, not a do-while, and remove your extra fetch() call, like I said to do in my very first second post.

Edited by Jessica
Link to comment
Share on other sites

I was asking whether it was possible to amend it to show 'No Records' when there were no results from the query......rather than the table just displaying no row / content at all.

 

I will take a look at a while loop, and find out you mean by the other fetch - it could be to do with the repeat region - and obviously dreamweaver will also be adding code that might not be necessary.....but I wouldn't know what to remove - noob, remember....using dreamweaver might not be the best way to learn (or not as the case may be) but that is what I am using.

 

Thank you for your continued help, it is appreciated - even though it might be simple stuff for you!

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.