Jump to content

Having output problems with a while{} loop showing the same record twice


prototype18

Recommended Posts

Hey everyone, I've just undertaken my first PHP project and I'm trying to build an HTML table that lists businesses by category.  The problem I am having is that the output of the PHP shows the first record in the first row both columns and this happens with every record after that.

 

The Code:

<?php



  // Set the error(s) reporting level

   error_reporting(E_ALL);

   ini_set("display_errors", 1);

   $category = "Professional Services";

   

   // Open a connection link to your Database Engine

   $link = mysql_connect('localhost', 'liecon63_bizdir', 'pass') or die("Connection Error : " . mysql_error());



   // Select the database to work with

   mysql_select_db('liecon63_bizdir') or die("Database Selection Error : " . mysql_error());



   // Define your query

   $sqlquery = "SELECT * FROM bizdir WHERE bizcategory = '".$category."'"; 



   // Execute the query (returning a result set in this case

   $sqlresult = mysql_query($sqlquery) or die("Query Error : " . $sqlquery . "<br /> Error: " . mysql_error());



   // Validate that the query returned records

   if (mysql_num_rows($sqlresult) > 0) {

     // Start display of the records

     echo "Business Category : " . $category . "<BR />";

     echo "<table>";

     // Loop through your result set to process the results

     while ($row = mysql_fetch_array($sqlresult, MYSQL_ASSOC))  // here you can simply use mysql_fetch_assoc() instead

     {

        // Process your records here... like

?><table border="0" width="100%" id="table3">

  <td width="400"><tr>

        <td><?php echo $row['bizname']; ?>

	<br>

	<?php echo $row['bizaddress']; ?>

	<br>

	<?php echo $row['bizcity']; ?>, <?php echo $row['bizstate']; ?> <?php echo $row['bizzip']; ?>

	<br>

	<?php echo $row['bizphone']; ?></td>

	<td><?php echo $row['bizname']; ?>

	<br>

	<?php echo $row['bizaddress']; ?>

	<br>

	<?php echo $row['bizcity']; ?>, <?php echo $row['bizstate']; ?> <?php echo $row['bizzip']; ?>

	<br>

	<?php echo $row['bizphone']; ?></td>

      </tr>

  </table>

  <?

     }

     echo "</table>";

  } else {

     echo "No records have been found for the category " . $category;

  }

// Close your Db Engine Link

mysql_close($link);

?>

 

The Output:

<BODY onLoad="changeBkg()" id="body" TEXT="#000000" LINK="#FF0000" ALINK="#FF0000" VLINK="#FF0000">
Business Category : Professional Services<BR /><table><table border="0" width="100%" id="table3">
  <td width="400"><tr>
        <td>Blumstein Accounting		<br>
	1476 Blue Spruce Lane		<br>
	Wantagh, New York 11793		<br>
	(516) 221-6161</td>
	<td>Blumstein Accounting		<br>
	1476 Blue Spruce Lane		<br>
	Wantagh, New York 11793		<br>
	(516) 221-6161</td>
      </tr>
  </table>
  <table border="0" width="100%" id="table3">
  <td width="400"><tr>
        <td>Sachem Dental Group		<br>
	470 Patchogue Holbrook Rd		<br>
	Holbrook, New York 11741		<br>
	(631) 589-8485</td>
	<td>Sachem Dental Group		<br>
	470 Patchogue Holbrook Rd		<br>
	Holbrook, New York 11741		<br>
	(631) 589-8485</td>
      </tr>
  </table>
  <table border="0" width="100%" id="table3">
  <td width="400"><tr>
        <td>Care Plus Chiropractic		<br>
	1150 Sunrise Highway		<br>
	Bayshore, New York 11706		<br>
	(631) 665-1150</td>
	<td>Care Plus Chiropractic		<br>
	1150 Sunrise Highway		<br>
	Bayshore, New York 11706		<br>
	(631) 665-1150</td>
      </tr>
  </table>
  </table></html>

 

Please HELP! :confused:

Link to comment
Share on other sites

error is here...

<?php echo $row['bizname']; ?>		<br>		<?php echo $row['bizaddress']; ?>		<br>		<?php echo $row['bizcity']; ?>, <?php echo $row['bizstate']; ?> <?php echo $row['bizzip']; ?>		<br>		<?php echo $row['bizphone']; ?></td>		<td><?php echo $row['bizname']; ?>		<br>		<?php echo $row['bizaddress']; ?>		<br>		<?php echo $row['bizcity']; ?>, <?php echo $row['bizstate']; ?> <?php echo $row['bizzip']; ?>		<br>		<?php echo $row['bizphone']; ?></td>

your have told it TWICE to echo the same data

Link to comment
Share on other sites

Sorry people, but I feel as I have to mention this:-

 

  <?php

    }

    echo "</table>";

 

 

Please make sure that you use the full form tag (<?php?>) opposed to the short form tag (<??>) as this has the potential to kill a script if you mix and match, the example I have given is from the first excerpt of code, and this is why I though it best to mention.

 

And on second look you don't actually need to explicitly close the DB link, as this is a natural thing for mysql to do once the query has been completed: have a read of this to see what I mean

 

Rw

Link to comment
Share on other sites

Try this (untested)...

<table>
<?PHP
$cell = 1;
while ($row = mysql_fetch_array($sqlresult, MYSQL_ASSOC)) {
if($cell==1) {
	echo "<tr>";
}
?>
<td>
<?PHP
echo $row['bizname'] . "<br>" . $row['bizaddress'] . "<br>" . $row['bizcity'] . ", " . $row['bizstate'] . " " . $row['bizzip'] . "<br>" .  $row['bizphone']; 
?>
</td>
<?PHP
$dorow = 0;
if($cell==2) {
	echo "</tr>";
	$cell=0;
	$dorow = 1;
}
$cell ++;
}
if($dorow ==0) {
echo "</tr>";
}
?>
</table>

Link to comment
Share on other sites

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.