Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cabbit

macrumors 68020
Original poster
Jan 30, 2006
2,128
1
Scotland
Code:
<div id="apDiv1">
<div id="apDiv2">
  <h2>Welcome to the Stories Archive</h2>
  <br />
  <p>You will find here and extensive collection of stories.  Even better than that, this story archive is easy to use.</p>
  <p>The Stories are listed in order of newest to oldest so the latest story will always be right at the top of the list for you to read.</p>
  <p>As an added bonus, you can view and add comments about the stories, this is great if you find a problem with a story, or just want to make general comments.</p>
  <p>You too can add stories to the story archive to share with everyone.</p>
  <p>Enjoy searching through the stories ^_^</p>
  <p> </p>
<a href="stories_upload.php">Add a new story</a>
  </div>
<?php
$host = "host";
$username = "name";
$password = "pass";
$database = "dbname";
$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);
$server = mysql_connect($host, $username, $password);
$db = mysql_select_db($database, $server);
$numofrows = @mysql_num_rows($result);
$sql = mysql_query('SELECT * FROM `storys` ORDER BY `id` DESC');
print '<table width="650" border="0" cellpadding="2" cellspacing="1">\n';
print '<tr><th>Story</th><th>Author</th></tr>\n';
while($row = mysql_fetch_array($sql, MYSQL_BOTH)) {
for($i = 0; $i < $numofrows; $i++) 
	{
	$row = @mysql_fetch_array($result);
if($i % 2) 
		{ 
		print '<TR bgcolor="#333333">\n';
		} 
	else 
		{ 
		print '<TR bgcolor="#444444">\n';
		}
printf("<td><a href=\"story_display.php?id=%s&usrname=$usrname\">%s</a></td><td><span style=\"TEXT-ALIGN:center\">%s</span></td>\n", $row["id"], $row["title"], $row["author"], $row["id"]);
print "</TR>\n";
	}
print "</table>\n";
?>
<br/ >
</div>

hi im trying to get my table to print in alternating colours so i followed a tutorial but it dosnt work it just errors out saying there is a unexpected $end. Pls help.

http://www.scriptplayground.com/tutorials/php/Alternating-Row-Color/ tutorial url
 
you have a for loop inside a while loop, and only the for loop is closed. also, thats pretty kludgy markup. have you heard of CSS?

edit: i just looked at what your doing in the two loops. use ONE of the loops. it makes no difference which, (the while loop is a bit cleaner) but you dont need both.

editx2: i just looked at your SQL/Mysql connections. what in gods name are you doing!? have you actually used PHP/MySQL before? like, ever? I'll give you a tip on how to fix it. Start again. I dont know what "tutorial" told you to do all that crap, but find a better one, NOW!
 
Use CSS. It's easier to change colors later, cleaner code, and faster download.

good idea.
here's what I did in a tiny side project of mine:
PHP:
$rowcount = 0;

while ($row = mysql_fetch_assoc($result))	{
	// other code here
	$zebratables = ($rowcount % 2) ? $diveven : $divodd;
	
	// other code here
	print <<<omg
	<div class="quote $zebratables">
	// other code here
	omg;
	
	$rowcount++;
}
where $diveven and $divodd will have whatever class I feel like defining, e.g. $diveven = "even" and $divodd = "odd"
and then have something like
Code:
div.odd { background-color: black; }
div.even { background-color: white; }
in the css, so say I want to change black to orange, it's just one teeny change in the css instead of somewhere else.
 
thanks for the help guys i'll look into that loop. the database connect is due to be replaced by the site wide one and yes it will be put into my css stylesheet once its working i prefer to keep it all seperate till it works.
 
good idea.
here's what I did in a tiny side project of mine:
PHP:
$rowcount = 0;

while ($row = mysql_fetch_assoc($result))	{
	// other code here
	$zebratables = ($rowcount % 2) ? $diveven : $divodd;
	
	// other code here
	print <<<omg
	<div class="quote $zebratables">
	// other code here
	omg;
	
	$rowcount++;
}
where $diveven and $divodd will have whatever class I feel like defining, e.g. $diveven = "even" and $divodd = "odd"
and then have something like
Code:
div.odd { background-color: black; }
div.even { background-color: white; }
in the css, so say I want to change black to orange, it's just one teeny change in the css instead of somewhere else.

How dose this compare to using a table will there be less control with a div or sould i place the table inside the div. If anyone got some free time to rewite it and show me how its done would be apprestiated this is my road block for finishing my new site as most of the site relies on this code.
 
How dose this compare to using a table will there be less control with a div or sould i place the table inside the div. If anyone got some free time to rewite it and show me how its done would be apprestiated this is my road block for finishing my new site as most of the site relies on this code.
Um.

I can't tell you what to do with your code. That was an appropriate solution for my site, where the only reason to use a table was for layout, and I think that's an abuse of tables.

If you need a tutorial, w3schools.com isn't a bad place to start.
 
Code:
<?php
// Database connect //
$server = mysql_connect("host", "usr", "pass");
$connection = mysql_select_db($server);
//Connect to stories table //
$sql = mysql_query('SELECT * FROM `storys` ORDER BY `id` DESC');
$numofrows = @mysql_num_rows($sql);
//Start Loop //
print '<table width="650" border="0" cellpadding="2" cellspacing="1">';
	print '<TR><td width="50%">Name</td><td width="50%">Email</td></TR>';
		while($row = mysql_fetch_array($sql, MYSQL_BOTH)) {
			$row = @mysql_fetch_array($sql);
				if($i % 2) 
					{ 
						print '<TR bgcolor="#333333">';
					} 
						else 
							{ 
								print '<TR bgcolor="#111111">';
							}
	printf("<td><a href=\"story_display.php?id=%s&usrname=$usrname\">%s</a></td><td><span style=\"TEXT-ALIGN:center\">%s</span></td>", $row["id"], $row["title"], $row["author"], $row["id"]);
		print "</TR>";
															}
	print "</table>";
//end //
?>


With the following it prints colours, but its printing the same colour over and over not alternating it as desired.
 
Code:
<?php
// Database connect //
$server = mysql_connect("host", "name", "pass");
$connection = mysql_select_db($server);
//Connect to stories table //
$sql = mysql_query('SELECT * FROM `storys` ORDER BY `id` DESC');
$numofrows = @mysql_num_rows($sql);

//Start Loop //
print '<table width="700" border="0" cellpadding="4" cellspacing="0">';
	print '<TR class="story_top"><td width="60%">Story</td><td width="20%">Author</td><td width="20%">Poster</td></TR>';
		while($row = mysql_fetch_array($sql, MYSQL_BOTH)) {
//start colour changing loop//
			for($i = 0; $i < $numofrows; $i++)	{
				$row = @mysql_fetch_array($sql);
				// the ifelse //
					if($i % 2) 
						{ 
							print '<TR class="pink">';
						} 
				else 
							{ 
								print '<TR class="wight">';
							}
//the output//
	printf("<td><a href=\"story_display.php?id=%s&usrname=$usrname\">%s</a></td><td><span style=\"TEXT-ALIGN:center\">%s</span></td><td>%s</td>", $row["id"], $row["title"], $row["author"], $row["poster"]);
		print "</TR>";
//end colour changing loop//
													}
															}
	print "</table>";
//end //
?>

^_^ it works perfictly with the above code, emm how do i go about really secureing php code?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.