prototype18 Posted September 17, 2010 Share Posted September 17, 2010 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! Quote Link to comment Share on other sites More sharing options...
litebearer Posted September 17, 2010 Share Posted September 17, 2010 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 Quote Link to comment Share on other sites More sharing options...
prototype18 Posted September 17, 2010 Author Share Posted September 17, 2010 Now it only shows one column repeating, I wanted two records to show in the same row. Quote Link to comment Share on other sites More sharing options...
Dubya008 Posted September 17, 2010 Share Posted September 17, 2010 Now it only shows one column repeating, I wanted two records to show in the same row. So do you want 2 records per row? Biz 1 info Biz2 biz 3 biz 4 Quote Link to comment Share on other sites More sharing options...
prototype18 Posted September 17, 2010 Author Share Posted September 17, 2010 yes, exactly. Quote Link to comment Share on other sites More sharing options...
rwwd Posted September 17, 2010 Share Posted September 17, 2010 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 Quote Link to comment Share on other sites More sharing options...
prototype18 Posted September 17, 2010 Author Share Posted September 17, 2010 Thanks, I will change the PHP tags. But I still need help on getting two different records in the same row... Quote Link to comment Share on other sites More sharing options...
litebearer Posted September 17, 2010 Share Posted September 17, 2010 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> Quote Link to comment Share on other sites More sharing options...
prototype18 Posted September 17, 2010 Author Share Posted September 17, 2010 Thanks litebearer, you got it to show seperate records in the same row, but now its only showing two records and not looping through the rest of the table Quote Link to comment Share on other sites More sharing options...
prototype18 Posted September 17, 2010 Author Share Posted September 17, 2010 Nevermind! I fixed it, it was because I had left the previous while{} statement there. THANK YOU VERY MUCH FOR YOU HELP! YOU SAVED MY BRAIN! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.