Jump to content

extra row in html table


Bazzaah

Recommended Posts

Hi there

 

I followed this post

 

http://www.phpfreaks.com/forums/index.php?topic=95426.0

 

to get a multi-column layout of search results.

 

All's well but I seem to have an extra blank row at the bottom of the table.

 

This is my code for the table

 

echo '<table width="800px" class="center" border="1">';
echo '<td>';  

if($result && mysql_num_rows($result) > 0)
{
    $i = 0;
    $max_columns = 3;
    while ($list = mysql_fetch_array($result))       
   {
       // make the variables easy to deal with
       extract($list);

       // open row if counter is zero
       if($i == 0)
          echo "<tr>";
          echo '<td><a href="word.php?w=' . $list['word'] . '">' . $list['word'] . '</a></td>';
    
       // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
   } // end while
} // end if results

// clean up table - makes your code valid!
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "<td> </td>";
}
echo '</tr>';
echo '</table>';

 

Any ideas on how not to have it there?

 

Thanks in advance.

 

Link to comment
Share on other sites

Bazzaah,

 

    Good morning... First off, when you run your script and view source, you get:

 

<table width="800px" class="center" border="1"><td><tr><td><a href="word.php?w="></a></td><td><a href="word.php?w="></a></td><td><a href="word.php?w="></a></td></tr><tr><td><a href="word.php?w="></a></td><td><a href="word.php?w="></a></td><td><a href="word.php?w="></a></td></tr><tr><td><a href="word.php?w="></a></td><td><a href="word.php?w="></a></td><td><a href="word.php?w="></a></td></tr><tr><td><a href="word.php?w="></a></td><td> </td><td> </td></tr></table>

 

    Kind of hard to debug...

 

    Adding some Source control new line characters , we get:

 

<table width="800px" class="center" border="1">
<td><tr>
<td><a href="word.php?w="></a></td>
<td><a href="word.php?w="></a></td> 
<td><a href="word.php?w="></a></td> 
</tr> 
<tr>
<td><a href="word.php?w="></a></td> 
<td><a href="word.php?w="></a></td> 
<td><a href="word.php?w="></a></td>
</tr> 
<tr> 
<td><a href="word.php?w="></a></td> 
<td><a href="word.php?w="></a></td> 
<td><a href="word.php?w="></a></td>
</tr> 
<tr> 
<td><a href="word.php?w="></a></td>
<td> </td> 
<td> </td>
</tr>
</table>

 

Right away, we can now see that we have a leading <td> which should not be there.

 

Here is the result of my altered script:

 

<table width="800px" class="center" border="1">
<tr>
<td><a href="word.php?w=1">1</a></td>
<td><a href="word.php?w=2">2</a></td>
<td><a href="word.php?w=3">3</a></td>
</tr>
<tr>
<td><a href="word.php?w=4">4</a></td>
<td><a href="word.php?w=5">5</a></td>
<td><a href="word.php?w=6">6</a></td>
</tr>
<tr>
<td><a href="word.php?w=7">7</a></td>
<td><a href="word.php?w=8">8</a></td> 
<td><a href="word.php?w=9">9</a></td>
</tr>
<tr> 
<td><a href="word.php?w=10">10</a></td>
<td> </td>
<td> </td> 
</tr>
</table>

 

 

A note about the new line character ( and tab character ), they don't work hidden behind '', only if imbedded in : "".

You code has a mix of single quotes and double quotes for holding your string content... for any html you want to parrot out, always use double quotes and the new line character...

 

Here is my altered code:

 

<?php

//	Program Name:		test.php
//	Program Title:		 
//	Created by:			 
//	Template family:	 
//	Template name:		 
//	Purpose:        
//	Program Modifications:

// Set no caching

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

set_time_limit(0);

session_start();

echo '<table width="800px" class="center" border="1">'  . "\n"; 
  
	$i = 0;    
	$max_columns = 3;    

	$myArray = array(1,2,3,4,5,6,7,8,9,10);

	// while ($list = mysql_fetch_array($result)) {  

	while (!empty($myArray)) {

		// make the variables easy to deal with      

		$list['word'] = array_shift($myArray);

		// open row if counter is zero       

		if($i == 0) 
		         
			echo "<tr> \n"; 

			/**
			 * 
			 * 
			 *  Bad coding technique ( Works, just hard do follow... ( your indent is wrong. ) )
			 * 
			 *     Better to always use: 
			 * 
			 * 				if ($someVar = $someVal) {
			 * 					doSomething();
			 * 				}
			 * 
			 * 		even for one-line directive for true "if"
			 *     
			 * 
			 */
		         
		echo '<td><a href="word.php?w=' .

		$list['word'] . '">' . 

		$list['word'] . "</a></td> \n ";  
		         

		// increment counter - if counter = max columns, reset counter and close row       

		if(++$i == $max_columns) { 
			          
			echo "</tr> \n"; 
			          
			$i=0;  
			     
		}  // END if(++$i == $max_columns) { 

	} // END while (!empty($myArray)) {


// } END while ($list = mysql_fetch_array($result)) { 


	// clean up table - makes your code valid!

	if($i < $max_columns) {    

		for($j=$i; $j<$max_columns;$j++) 
			echo "<td> </td> \n";


			/**
			 * 
			 * 
			 *  Bad coding technique ( Works, just hard do follow... )
			 * 
			 *     Better to always use: 
			 * 
			 * 				for($j=$i; $j<$max_columns;$j++)  {
			 * 					doSomething();
			 * 				}
			 * 
			 * 		even for one-line directive for true "for"
			 *     
			 * 
			 */

	} // END if($i < $max_columns) {     

	echo '</tr>' . "\n"; 

	echo '</table>' . "\n"; 



?>

Link to comment
Share on other sites

ScotDiddle

 

Thanks so much for that, it's really appreciated.

 

One further question if I may about using an array; the table is built from a search which I've set up like this;

 

$sql = "SELECT * FROM words WHERE word LIKE '%{$searchTermDB}%' ORDER BY word LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

 

(I should update to mysqli functions.)

 

If the results are expressed through the variable $result, how do I use an array?

 

Link to comment
Share on other sites

Bazzaah,

 

    I don't use MySQL, I'm a DB2 guy, but take a look a this link: http://www.tizag.com/mysqlTutorial/mysqlfetcharray.php

 

    (I only used an array because I did not have access to your table, nor did I have the time or inclination to build one in DB2) :)

 

Scot L. Diddle, Richmond VA

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.