Jump to content

Alternating Color Table with URL links


Scruffy

Recommended Posts

Hi,

 

I'm having trouble with my table. I want to make the first row in my table be able to have links such as this

 

<a href="Test.php?id=<?php echo $row['title']; ?>"><?php echo $row_['title']; ?> </a>

 

Though I don't know where to put it in this code:

 

<?php
$sql  = $query_ps3_list;
$qry = mysql_query($sql) or die(mysql_error());

// Cell colors for odd rows
$c1  = array('#4c4e4e', '#4c4e4e', '#4c4e4e', '#4c4e4e', '#4c4e4e', '#4c4e4e', '#4c4e4e', '#4c4e4e', '#4c4e4e');

// Cell Colors for even rows
$c2  = array('#5c5d5d', '#5c5d5d', '#5c5d5d', '#5c5d5d', '#5c5d5d', '#5c5d5d', '#5c5d5d', '#5c5d5d', '#5c5d5d');

// Row Counter
$cnt  = 0;

$ret = '<table>';
while($row = mysql_fetch_assoc($qry))
{
  // Cell counter
  $cnt2 = 0;
  $ret  .= '<tr>';
  foreach($row as $key=>$val)
   {
    $ret .= '<td bgcolor="';

    if($cnt % 2)
      {
        $ret .= $c1[$cnt2];
      }
    else
      {
        $ret .= $c2[$cnt2];
      }

    $ret .= '">'.$val.'</td>';
    ++$cnt2;
   }
  ++$cnt;
  $ret .= '</tr>'."\n";
}
$ret .= '</table>';

echo $ret;
?> 

 

If you could help me out that would be awesome! Thanks!

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/184444-alternating-color-table-with-url-links/
Share on other sites

<?php
$sql  = $query_ps3_list;
$qry = mysql_query($sql) or die(mysql_error());

// Cell colors for odd rows
$c1  = array('#4c4e4e', '#4c4e4e', '#4c4e4e', '#4c4e4e', '#4c4e4e', '#4c4e4e', '#4c4e4e', '#4c4e4e', '#4c4e4e');

// Cell Colors for even rows
$c2  = array('#5c5d5d', '#5c5d5d', '#5c5d5d', '#5c5d5d', '#5c5d5d', '#5c5d5d', '#5c5d5d', '#5c5d5d', '#5c5d5d');

// Row Counter
$cnt  = 0;

$ret = '<table>';
$i=0;
while($row = mysql_fetch_assoc($qry))
{
    if ($i == 0) {
       $ret .= '<a href="Test.php?id=' . $row['title'] . '">' . $row_['title'] . '</a>';
       $i++;
   }
  // Cell counter
  $cnt2 = 0;
  $ret  .= '<tr>';
  foreach($row as $key=>$val)
   {
    $ret .= '<td bgcolor="';

    if($cnt % 2)
      {
        $ret .= $c1[$cnt2];
      }
    else
      {
        $ret .= $c2[$cnt2];
      }

    $ret .= '">'.$val.'</td>';
    ++$cnt2;
   }
  ++$cnt;
  $ret .= '</tr>'."\n";
}
$ret .= '</table>';

echo $ret;
?> 

 

EDIT:

Added an if inside the loop with $i to increment, if $i is 0 then it adds that code to the list, else it skips it.

I'm trying to use:

 

<a href="Test.php?id=<?php $row['title'] ?>"></a>

 

And make the title that shows up in the first column be part of the URL for the next page that loads. Though When I type in the link its like its not being read because its in PHP even though I have will put "'" and ";" around the HTML.

 

 

Archived

This topic is now archived and is closed to further replies.

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