Jump to content

What am I doing wrong??


Jamesking56

Recommended Posts

This code outputs the data in a mySQL table into a tablebut... it outputs random letters??Here is my code:

<?php	require_once("db.inc.php");?><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head>	<title>ForumTesting!</title>	<style type="text/css" media="screen, projection, print">		#mainbox {		border: 1px solid #FF0000;		outline: 1px solid #000000;		}				#mainbox img {		border: none;		}				#mainbox td {		border: 1px solid #FF0000;		outline: 1px solid #000000;		}				#mainbox th {		border: 1px solid #0000FF;		outline: 1px solid #000000;		background: #000000;		color: #FFFFFF;			}	</style>	</head><body>	<div align="center" id="mainbox">		<h2>Current Tests</h2>		<?php					// Get List of Current Tests			$query = mysql_query("SELECT * FROM tests WHERE finished=0 AND active=1");						if ( !$query )			{				echo "Error, Could not get Tests!";			}			else			{				// Make Loop for Table				echo "<table>";				echo "<tr>";				echo "<th>Name</th>";				echo "<th>System</th>";				echo "<th>Version</th>";				echo "<th>Test Details</th>";				echo "<th>Status</th>";				echo "</tr>";								while($current = mysql_fetch_array($query))				{				echo "<tr>";				foreach( $current as $test )				{					if ( $test['status']=="open" )					{						echo "<td><a href=\"" . $test['link'] . "\" title=\"\" target=\"_blank\">" . $test['name'] . "<img src=\"sys/icons/link_go.png\" alt=\"\" border=\"0\" /></a></td>";					}					elseif ( $test['status']=="closed" )					{						echo "<td>" . $test['name'] . "<img src=\"sys/icons/lock.png\" alt=\"\" border=\"0\" /></td>";					}										echo "<td>" . $test['system'] . "</td>";					echo "<td>" . $test['version'] . "</td>";					echo "<td>" . $test['details'] . "</td>";										if ( $test['status']=="open" )					{						echo "<td><img src=\"sys/icons/lock_open.png\" alt=\"\" /></td>";					}					elseif ( $test['status']=="closed" )					{						echo "<td><img src=\"sys/icons/lock.png\" alt=\"\" /></td>";					}				}				echo "</tr>";				}								echo "</table>";			}				?>		<br /><br />		<h2>Old Tests</h2>		<?php			// Get List of Old Tests			$query2 = mysql_query("SELECT * FROM tests WHERE finished='0' AND active='1'");			if ( !$query2 )			{				echo "Error, Could not get Results";			}			else			{				// Make Loop for Table				echo "<table>";				echo "<tr>";				echo "<th>Name</th>";				echo "<th>System</th>";				echo "<th>Version</th>";				echo "<th>Test Details</th>";				echo "<th>Result</th>";				echo "</tr>";				while($current2 = mysql_fetch_array($query2))				{				echo "<tr>";				foreach( $current2 as $test )				{					if ( $test['status']=="open" )					{						echo "<td>" . $test['name'] . "<img src=\"sys/icons/stop.png\" alt=\"\" border=\"0\" /></td>";					}					elseif ( $test['status']=="closed" )					{						echo "<td>" . $test['name'] . "<img src=\"sys/icons/lock.png\" alt=\"\" border=\"0\" /></td>";					}					echo "<td>" . $test['system'] . "</td>";					echo "<td>" . $test['version'] . "</td>";					echo "<td>" . $test['details'] . "</td>";					echo "<td>" . $test['result'] . "</td>";									}								echo "</tr>";				}				echo "</table>";			}		?>	</div></body></html>

and here is a screenshot of the output:phpbadscreenyj4.jpgPlease tell me what I am doing wrong!!

Link to comment
Share on other sites

mysql_fetch_array() expects two parameters. The second tells it how to format the result. If you don't pass the second, it defaults to a format that might not be useful to your situation. If you just want an associative array, try mysql_fetch_array($query, MYSQL_ASSOC) or use mysql_fetch_assoc() instead.var_dump($current) might be a good debugging test, also.Mostly, think about this, which I've shortened to show the goodies:

while($current = mysql_fetch_array($query)){   foreach( $current as $test ){	  if ( $test['status']=="open" )

For each pass of the while loop, $current is an array. For each pass of the foreach loop, $test is an element of $current. $test is probably NOT an array, so it won't have a ['status'] element. I think $current has the ['status'] element. If you want to access 'status', 'version', etc, I don't think you need the foreach loop. The values you want should be available in $current['status'], $current['version'], etc.

Link to comment
Share on other sites

				while($current2 = mysql_fetch_array($query2))				{				echo "<tr>";				foreach( $current2 as $test )

When you call the foreach loop inside the while loop, you're screwing up your data. You want to use one or the other.Something like this:
$current2 = mysql_fetch_assoc($query2);foreach($current as $test) {  // do stuff here}

Hopefully that'll help. Like Diedre's Dad said, you had grabbed the array and separated it with the while loop, then separated the array's values with the foreach loop, which is resulting in (at least it looks like) the text in your data being separated into single characters.-Jason

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...