Alternating Tables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ahms
Forum Newbie
Posts: 7
Joined: Mon Dec 01, 2003 10:21 am

Alternating Tables

Post by Ahms »

Hi, new guy here :)

New to PHP and trying to accomplish a task that I think is simple but I'm not sure on how to exactly code it. Here is the page in question:

http://www.ancient-sun.com/newsite/structure.php

The info in the tables is taken from a database. Basically, on the left hand side I want people listed with a certain identifying variable, and on the other side people liisted with another identifying variable. Basically I want the script to be intelligent and put certain people in one category and the others in the other one

I sort of did it, but I'm getting the blank boxes. I'm not really sure how to correct it, here's my code:

Code: Select all

<?php
  
      	mysql_connect('localhost', '*****', '*****');
      	mysql_select_db('*****');

      	$query = "SELECT personal.username,
      	mainchar.mainname, mainchar.username, mainchar.title, mainchar.rank, mainchar.division
      	FROM personal, mainchar WHERE personal.username = mainchar.username ORDER BY personal.username";
      	$result = mysql_query($query);
      	echo "
      	<TR>
      	<TD ALIGN = left CLASS = 'tabletop'>
      	<DIV CLASS = 'tabletitle'>
      	<B>The Ancient Hand</B>
      	</TD>
      	<TD ALIGN = left CLASS = 'tabletop'>
      	<DIV CLASS = 'tabletitle'>
      	<B>The Ancient Shield</B>
      	</TD>
      	</TR>";

      	while ($query_data = mysql_fetch_row($result)) {
      	if($i % 2) {
        echo"<TR BGCOLOR = '#31200A'>";
      	}
        else {
        	echo"<TR BGCOLOR = '#160D01'>";
             }	

      	echo "
      	<TD>";
      	if($query_data[5] == "Hand") {
        echo "
        <FONT COLOR = '#FFFFFF' FACE = 'tahoma' SIZE = '2'>
        <B>$query_data[1]</B> the $query_data[3]<BR>
        $query_data[4]<BR>
        ($query_data[0])
        ";
      	}
      	echo "</TD><TD>";

      	if ($query_data[5] == "Shield") {
        echo "
        <FONT COLOR = '#FFFFFF' FACE = 'tahoma' SIZE = '2'>
        <B>$query_data[1]</B> the $query_data[3]<BR>
        $query_data[4]<BR>
        ($query_data[0])
        ";
        }
      	echo "
      	</TD></TR>";
      	++$i;
                }
	?>
Any help is appreciated. Thanks! :D


feyd | switched

Code: Select all

to

Code: Select all

tag[/color]
jakobdoppler
Forum Commoner
Posts: 46
Joined: Wed May 21, 2003 6:16 pm

Post by jakobdoppler »

hi

i would suggest to create two separate tables WITHIN this table for each of your 2 groups querying one with shield and the other one with !shield (whatever that name was ;-)

means in pseudo code

Code: Select all

<table>
<tr>
<td>

//first table
<table>
while (!Shield)
<tr><td>name</td></tr>
</table>

</td>
<td>

//2nd table
<table>
while (Shield)
<tr><td>name</td></tr>
</table>

</td>
</tr>
</table>
*hth* _yak
Ahms
Forum Newbie
Posts: 7
Joined: Mon Dec 01, 2003 10:21 am

Post by Ahms »

I'm getting pretty much the same problem still- blank boxes, all be them alot skinnier now. What I guess happens is it looks through my database starting from the top, and if something doesn't have the variable "Hand" it just puts a blank box and continues on
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yeah.. you're still echoing a <td> even if division isn't 'Hand' or 'Shield'. You could alter your query to only select those with a division of 'Hand' or 'Shield'
Ahms
Forum Newbie
Posts: 7
Joined: Mon Dec 01, 2003 10:21 am

Post by Ahms »

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


[quote="feyd"]yeah.. you're still echoing a <td> even if division isn't 'Hand' or 'Shield'. You could alter your query to only select those with a division of 'Hand' or 'Shield'[/quote]

Tried it, getting closer now!   

New code:

Code: Select all

<?php 
  
        mysql_connect('*****', '*****', '*****'); 
        mysql_select_db('*****'); 

        $query_hand = "SELECT personal.username, 
        mainchar.mainname, mainchar.username, mainchar.title, mainchar.rank, mainchar.division 
        FROM personal, mainchar WHERE personal.username = mainchar.username AND mainchar.division = 'Hand' ORDER BY mainchar.mainname"; 
        $resulthand = mysql_query($query_hand); 
  	
	$query_shield = "SELECT personal.username, 
        mainchar.mainname, mainchar.username, mainchar.title, mainchar.rank, mainchar.division 
        FROM personal, mainchar WHERE personal.username = mainchar.username AND mainchar.division = 'Shield' ORDER BY mainchar.mainname"; 
        $resultshield = mysql_query($query_shield);

	echo "<TR>
	<TD><TABLE>";

	while ($query_data = mysql_fetch_row($resulthand)) { 
		echo " 
		<TR><TD>
		<FONT COLOR = '#FFFFFF' FACE = 'tahoma' SIZE = '2'>
		$query_data[1]</TD></TR>";
	}
	++$i;
	echo "</TABLE></TD><TD><TABLE>";
	
	while ($query_data = mysql_fetch_row($resultshield)) { 
		echo " 
		<TR><TD>
		<FONT COLOR = '#FFFFFF' FACE = 'tahoma' SIZE = '2'>
		$query_data[1]</TD></TR>";
	}
	echo "</TABLE></TD></TR>";
	++$i;
http://www.ancient-sun.com/newsite/structure.php

Still have a few problems. The table rows are not lining up correctly, and what I would like to do is get alternating table colors for each new row. Before I had the "if ($i %2)" to set my table row colors but it does not work now


feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try this query instead:

Code: Select all

SELECT personal.username,
        mainchar.mainname, mainchar.username, mainchar.title, mainchar.rank, mainchar.division
        FROM personal, mainchar WHERE personal.username = mainchar.username AND mainchar.division IN( 'Hand', 'Shield' ) ORDER BY mainchar.mainname
obviously lose the double query and whatnot..
Post Reply