Jump to content

While Loop Problem


quantumdecipher

Recommended Posts

Hi guys, I have this code which fetches data from an existing database and reflects the data into a tabular form. Basically what it does is it parses the field portNumber from my table and gets the last two digits of the variable. Then it will highlight in the table all those returned values for portNumber.

 

Here is the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Location Mapper</title>
<script>
function highlightRow(id) {
	document.getElementById(id).style.backgroundColor = "FFCC00";
}
</script>
<style>
body {
font-family:Tahoma, Geneva, sans-serif;
font-size:10px;
text-align:center;
}
.unitHere {
color:#000;
background-color:#0F0;
styl
}
</style>
</head>

<body>
<table width="800" border="1" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <th colspan="40" scope="col">SCMST 238</th>
  </tr>
  <tr>
	<?php
    	$conn = mysql_connect("localhost", "root", "123456") or die(mysql_error()); 
	mysql_select_db("orderstatus") or die(mysql_error());
	$sql = mysql_query("SELECT * FROM superdome WHERE portNumber LIKE '%238%' AND isHistory='0' ORDER BY portNumber ASC")
		or die(mysql_error());

	if(mysql_num_rows($sql) == 0) {
		echo "NOTHING FOUND";
	} else {

		$i = 1;

		while ($i <= 40) {
			$x = 0;	
			while($result = mysql_fetch_array($sql)) {
				$resultPortArray = str_split($result['portNumber'],4);
				$resultPortArray[1];
				$x++;	
			}
				if ($resultPortArray[1] == $i) {
					echo "<td width=\"20\" height=\"30\" id=\"".$i."\" class=\"unitHere\">".$i."</td>";
					$i++;
				} else {
					echo "<td width=\"20\" height=\"30\" id=\"".$i."\">".$i."</td>";
					$i++;
				}
		}
	}
?>
  </tr>
</table>
</body>
</html>

 

The problem is my output only highlights the one column in the array returned. The rest are not highlighted. I need help badly.

Link to comment
Share on other sites

1. if you only want 40 rows returned, i would use a LIMIT in your query instead of a nested while loop..bad idea.

2. What does a "view source" show about the HTML behavior?

3. where is you "highlightRow()" function even being called here.?

 

Thanks for the reply.

 

1. Only 40 rows returned but not all are in database. Those 40 rows constitute 40 stations. The stored data in the DB are less than 40. So the program actually fetches only those with data.

 

2. The view source is normal, just that the values are not returned properly.

 

3. highlightRow() is being called. And I need it since its the function which changes the color of the station which is flagged.

Link to comment
Share on other sites

It seems your issue is with:

<?php
$resultPortArray[1];
print_r($resultPortArray);
?>

 

$resultPortArray[1]; shouldn't even work.

 

Also, what's the deal with $x? It serves no purpose in this code.

 

Let's get a better understanding on your code first off, I do understand what you want from your explanantion, but we need to help you understand how to get you to understand to get your code there, if that makes any sense.

 

Your query is telling it to grab all rows from the superdome table where the portNumber is like %238%.

 

That's fine, but then your code starts to say, $i is a counter that starts at one, until it reaches 40 don't stop.

 

Inside the loop, you are looping ALL SQL ROWS and building this strange array:

$resultPortArray = str_split($result['portNumber'],4);
				$resultPortArray[1];
				$x++;

And incrementing $x for no reason.

 

What it seems like we need to do is first loop all of the results that are not in a query and then turning that into an array.

 

You will then need to start a loop of the query and have a foreach statement looping all of the other results, doing whatever matching from there to choose which fields need to be highlighted and what don't.

 

For us to help you with making your code do what you want it to, we'll need to have more information about the concept of this script.

Link to comment
Share on other sites

Thanks for the reply. Basically, I looped to 40 to create 40 columns automatically, since there will be many kinds of port, not only 238. What I want for the script to do is dynamically create 40 columns (or any number which I may define later), then connect to the database "orderstatus", open the table "superdome", then fetch all entries with portNumber LIKE 238, parse the portNumber (because the data in the Entry will be in a pattern 238-10, etc), get the digits after the dash, then highlight all the column numbers with returned values and those without values stay in white.

Link to comment
Share on other sites

I understand.

 

<?php 

$query = 'SELECT * FROM superdome WHERE portNumber LIKE '%238%' AND isHistory='0' ORDER BY portNumber ASC';
if( ($result = mysql_query($query)) === FALSE ) {
trigger_error('Could not select data',E_ERROR);
} elseif( mysql_num_rows($result) < 1 ) {
$message = 'Nothing found';
} else {
$data = array(); // create a blank array to hold results
while( $row = mysql_fetch_assoc($result) ) {
	list( $part1, $part2 ) = explode( '-', $row['portNumber'], 2 );
	// $part1 contains 238, $part2 contains 10 in 238-10
	$data[] = $part2; // add $part2 to our array
}
}
// You can print_r( $data ) to see the structure of that array
// Since I don't have your database, I'm going to create my own $data array
$data = array_merge( range(2,12), range(18,23), array(30), range(33,45,2) );

$max_range = 40;

// Create our table
echo '<table><tr>';
// Loop until max range
for( $i = 1; $i <= $max_range; $i++ ) {
echo '<td';
// Check if $i is in the $data array
if( in_array($i, $data) ) echo ' style="background-color:yellow;"';
echo '>' .$i. '</td>';
}
echo '</tr></table>';

?>

 

Let me know if you need further explanation.

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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