Contact Us Forum Rules Search Archive
WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-09-2004, 07:15 PM
WebProWorld Member
 

Join Date: Jan 2004
Location: NorCal
Posts: 64
MasterT RepRank 0
Default Alternating rows mySQL Query

hello i'm trying to code this query so that it will change every row bgcolor for every other row....but it gives me duplicates
im new to php and mySQL please help

here's my test page http://www.mxtracks.us/listtest.php

here's my query
Code:
<?php 
$db = mysql_connect("localhost", "%%%%%%%%%", "%%%%%%"); 
mysql_select_db("mxtracks_catracks",$db); 
$result = mysql_query("SELECT * FROM catracks WHERE `north` = '1'",$db); 
while($myrow = mysql_fetch_array($result)) 
{ 
echo "<tr class=TableText onClick=document.location='test.php?id=$myrow[id]' onMouseOver=this.style.backgroundColor=#552200 onMouseOut=this.style.backgroundColor=#331100 bgcolor=#331100><td>", "<a href=\"test.php?id=".$myrow[id]."\">".$myrow["name"]."</a>", "</td>", "<td>" .$myrow["city"]. "</td>", "<td>".$myrow["updated"], "</td></tr>"; 
echo "<tr class=TableText onClick=document.location='test.php?id=$myrow[id]' onMouseOver=this.style.backgroundColor=#552200 onMouseOut=this.style.backgroundColor=#442200 bgcolor=#442200><td>", "<a href=\"test.php?id=".$myrow[id]."\">".$myrow["name"]."</a>", "</td>", "<td>" .$myrow["city"]. "</td>", "<td>".$myrow["updated"], "</td></tr>"; 
} 
?>
Thanks for reading
__________________
http://mxtracks.us/images/MXTracksLogo.gif
California MX Tracks, Games, Photos, whatever you want its here
Reply With Quote
  #2 (permalink)  
Old 06-09-2004, 07:30 PM
WebProWorld Pro
 

Join Date: May 2004
Location: United Kingdom
Posts: 176
php~pro RepRank 0
Default

Hi
The code you have there is set to alter row colours on a mouse over event, is thet what you are actually trying to do or do you just want static alternating row colurs. If you want mouseover effects you are first best to select initial alternate colours with php and then integrate css for the mouseover. If you can be more specific about what you are actually trying to achieve Im happy to answer your query and re-write your code. It may be advantagous to post your email address here or private message me with it, along with details. I can then use this to post you some code. I dont do open source.
Reply With Quote
  #3 (permalink)  
Old 06-09-2004, 08:17 PM
WebProWorld Member
 

Join Date: Jan 2004
Location: NorCal
Posts: 64
MasterT RepRank 0
Default

here's the static version of what im looking for
http://www.mxtracks.us/mx/California/

im upgradeing from .html to .php and mySQL
__________________
http://mxtracks.us/images/MXTracksLogo.gif
California MX Tracks, Games, Photos, whatever you want its here
Reply With Quote
  #4 (permalink)  
Old 06-09-2004, 08:26 PM
WebProWorld Member
 

Join Date: Jan 2004
Location: NorCal
Posts: 64
MasterT RepRank 0
Default

and check your pms
__________________
http://mxtracks.us/images/MXTracksLogo.gif
California MX Tracks, Games, Photos, whatever you want its here
Reply With Quote
  #5 (permalink)  
Old 06-09-2004, 09:42 PM
WebProWorld Pro
 

Join Date: Jul 2003
Location: New Jersey, U.S.
Posts: 293
jdiben RepRank 0
Default

You could use the mod operator "%" to alternate colors in every other row. The if statement in the code below will alternate the color assigned to the $bgcolor variable. The color would then be printed to alternating rows. The same idea will work for your while loop. I didnt realize that you were using a while loop until after I posted the code below. If you need help using it in your while loop just let me know and I would be happy to help.

Code:
echo "<table>";
for($x=0; $x<$rowCount; $x++){
    if($x%2==0){
        $bgcolor="#FFFFFF";
    }else{
        $bgcolor="#C0C0C0";
    }
    echo "<TR bgcolor=".$bgcolor."><TD> content </TD></TR>";
}
echo "</table>";
Luckily most of us do open source so that everyone with the same question can benefit from the answer. Thats what this forum is about.

Joe
Reply With Quote
  #6 (permalink)  
Old 06-10-2004, 01:19 AM
WebProWorld Member
 

Join Date: Jan 2004
Location: NorCal
Posts: 64
MasterT RepRank 0
Default

ya i need a while statement
im not sure how to make it

this is what i got
Code:
		while($myrow = mysql_fetch_array($result)) 
		{for($x=0; $x<$rowCount; $x++){ 
    if($x%2==0){ 
        $ter="<tr class=TableText onClick=document.location='test.php?id=$myrow[id]' onMouseOver=this.style.backgroundColor=#552200 onMouseOut=this.style.backgroundColor=#442200 bgcolor=#442200>"; 
    }else{ 
        $ter="<tr class=TableText onClick=document.location='test.php?id=$myrow[id]' onMouseOver=this.style.backgroundColor=#552200 onMouseOut=this.style.backgroundColor=#331100 bgcolor=#331100>"; 
    } 
    echo "$ter <td>", "<a href=\"test.php?id=".$myrow[id]."\">".$myrow["name"]."</a>", "</td>", "<td>" .$myrow["city"]. "</td>", "<td>".$myrow["updated"], "</td></tr>"; 
}
__________________
http://mxtracks.us/images/MXTracksLogo.gif
California MX Tracks, Games, Photos, whatever you want its here
Reply With Quote
  #7 (permalink)  
Old 06-10-2004, 07:15 AM
WebProWorld Pro
 

Join Date: May 2004
Location: United Kingdom
Posts: 176
php~pro RepRank 0
Default

Below is an example or a far simpler method of alternateing row colours. Good server side code should be unfussy, easy to understand and quick to execute.

// Define your colors for the alternating rows

$color1 = "#CCFFCC";
$color2 = "#BFD8BC";
$row_count = 0;

// Perform an statndard SQL query:

$sql_events = mysql_query("SELECT date_format(example_date, '%M %d, %Y') as example_date,
example_title FROM my_example ORDER BY example _date ASC") or die (mysql_error());

// Use the "$row" method for this query. This is just my preference.

while ($row = mysql_fetch_array($sql_examples)) {
$event_date = $row["example_date"];
$event_title = $row["example_title"];

/* Tell
php to alternate the colors between the two colors defined above. */

$row_color = ($row_count % 2) ? $color1 : $color2;

// Echo your table row and table data that you want to be looped over and over here.

echo "<tr>
<td width="110" bgcolor="$row_color" nowrap>
$example_date</td>
<td bgcolor="$row_color">
$example_title</td>
</tr>";

// Add 1 to the row count

$row_count++;
}

// Close out your table.

echo "</table>";
Reply With Quote
  #8 (permalink)  
Old 06-14-2004, 03:39 PM
WebProWorld Member
 

Join Date: Jan 2004
Location: NorCal
Posts: 64
MasterT RepRank 0
Default I did it

i read this tutorial http://codewalkers.com/tutorials/6/2.html and came up with this


Code:
<?php
for($i = 0; $i < $numofrows; $i++) {
    $row = mysql_fetch_array($result); //get a row from our result set
    if($i % 2) { //this means if there is a remainder
        echo "<tr class=\"TableText\" onClick=\"document.location='template.php?id=$row[id]'\" onMouseOver=\"this.style.backgroundColor='#552200'\" onMouseOut=\"this.style.backgroundColor=''\" bgcolor=\"#442200\">\n";
    } else { //if there isn't a remainder we will do the else
        echo "<tr class=\"TableText\" onClick=\"document.location='template.php?id=$row[id]'\" onMouseOver=\"this.style.backgroundColor='#552200'\" onMouseOut=\"this.style.backgroundColor=''\" bgcolor=\"#331100\">\n";
    }
    echo "<td>", "<a href=\"template.php?id=$row[id]\">".$row["name"]."</a>", "</td>", "<td>" .$row["city"]. "</td>", "<td>".$row["updated"], "</td>\n";
    echo "</TR>\n";
}
?>
works great
here's the page i put it on http://www.mxtracks.us/Temp/rowtest.php
thank you all for the help
__________________
http://mxtracks.us/images/MXTracksLogo.gif
California MX Tracks, Games, Photos, whatever you want its here
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Tags: , , ,



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Search Engine Optimization by vBSEO 3.2.0