stragglerat Posted May 14, 2008 Share Posted May 14, 2008 First of all, I just picked up PHP and SQL out of necessity for a new site. What I've learned so far has amazed me. I'm having a little hiccup, though. I'm using PHP to execute queries to an item database and "echo"ing the html code to set the returns in a table. The method I'm using, though, is only allowing one cell per table row. I've tried to manipulate the code a little, but have yet to find a solution. The example code below is returning a duplicate item in the second cell of each row. Any solutions? UPDATE: Lol, as you can see the <hr> part of my code is actually showing up as a rule below. <?php // set database server access variables: $host = "host"; $user = "user"; $pass = "pass"; $db = "db"; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // create query $query = "SELECT * FROM sampletable"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { // yes // print them one after another echo "<table border=0 cellspacing=0 cellpadding=0 class=box_width_cont product>"; while($row = mysql_fetch_row($result)) { echo "<tr>"; echo "<td colspan=2><hr>"; echo "</td>"; echo "</tr>"; echo "<tr>"; // first cell echo "<td><center><img height=125 width=175 src=".$row[1]."></center>"; echo "<center><font color=#855b63><b>".$row[2]."<br>".$row[3]."<br>".$row[7]." Cut<br>Size: ".$row[4]."</b><br><br></font><font color=#855b63 size=3><b>".$row[10]."</b></center></font><br>"; echo "<center><a href=items/".$row[0].".php><img src='images/button_details.gif'></a></center>"; echo "</td>"; // second cell - exact duplicate of one above echo "<td><center><img height=125 width=175 src=".$row[1]."></center>"; echo "<center><font color=#855b63><b>".$row[2]."<br>".$row[3]."<br>".$row[7]." Cut<br>Size: ".$row[4]."</b><br><br></font><font color=#855b63 size=3><b>".$row[10]."</b></center></font><br>"; echo "<center><a href=items/".$row[0].".php><img src='images/button_details.gif'></a></center></td>"; echo "</td>"; echo "</tr>"; } echo "</table>"; } else { // no // print status message echo "No items found!"; } // free result set memory mysql_free_result($result); // close connection mysql_close($connection); ?> Link to comment https://forums.phpfreaks.com/topic/105527-using-php-to-format-sql-results/ Share on other sites More sharing options...
jvrothjr Posted May 14, 2008 Share Posted May 14, 2008 try this if (mysql_num_rows($result) > 0) { // yes // print them one after another echo "<table border=0 cellspacing=0 cellpadding=0 class=box_width_cont product>"; echo "<tr>"; echo "<td colspan=2>--------------------------------------------------------------------------------"; echo "</td>"; echo "</tr>"; while($row = mysql_fetch_row($result)) { $i++ if ($i == '1') {echo "<tr>";} // first cell echo "<td><center><img height=125 width=175 src=".$row[1]."></center>"; echo "<center><font color=#855b63>".$row[2].$row[3].$row[7]." Cut Size: ".$row[4]."</font><font color=#855b63 size=3>".$row[10]."</center></font>"; echo "<center><a href=items/".$row[0].".php><img src='images/button_details.gif'>[/url]</center>"; echo "</td>"; if ($i == '2') {echo "</tr>";$i='0'} } echo "</table>"; } else { // no // print status message echo "No items found!"; } Link to comment https://forums.phpfreaks.com/topic/105527-using-php-to-format-sql-results/#findComment-540586 Share on other sites More sharing options...
stragglerat Posted May 14, 2008 Author Share Posted May 14, 2008 Thanks for the help. Returned this error though: Parse error: parse error, unexpected T_IF in /home/content/s/t/r/stragglerat/html/3stonerings3.php on line 374 This is referring to this line: if ($i == '1') {echo "<tr>";} Link to comment https://forums.phpfreaks.com/topic/105527-using-php-to-format-sql-results/#findComment-540590 Share on other sites More sharing options...
stragglerat Posted May 14, 2008 Author Share Posted May 14, 2008 How bout this: if (mysql_num_rows($result) > 0) { // yes // print them one after another echo "<table border=0 cellspacing=0 cellpadding=0 class=box_width_cont product>"; echo "<tr>"; while($row = mysql_fetch_row($result)) { echo "<td>"; echo "<center><img height=125 width=175 src=".$row[1]."></center>"; echo "<center><font color=#855b63><b>".$row[2]."<br>".$row[3]."<br>".$row[7]." Cut<br>Size: ".$row[4]."</b><br><br></font><font color=#855b63 size=3><b>".$row[10]."</b></center></font><br>"; echo "<center><a href=items/".$row[0].".php><img src='images/button_details.gif'></a></center>"; echo "</td>"; } echo "</tr>"; echo "</table>"; Is there a way to throw in a </tr><tr> after every two results? Link to comment https://forums.phpfreaks.com/topic/105527-using-php-to-format-sql-results/#findComment-540598 Share on other sites More sharing options...
jvrothjr Posted May 14, 2008 Share Posted May 14, 2008 opps if (mysql_num_rows($result) > 0) { // yes // print them one after another echo "<table border=0 cellspacing=0 cellpadding=0 class=box_width_cont product>"; echo "<tr>"; echo "<td colspan=2>--------------------------------------------------------------------------------"; echo "</td>"; echo "</tr>"; while($row = mysql_fetch_row($result)) { $i++; if ($i == '1') {echo "<tr>";} // first cell echo "<td><center><img height=125 width=175 src=".$row[1]."></center>"; echo "<center><font color=#855b63>".$row[2].$row[3].$row[7]." Cut Size: ".$row[4]."</font><font color=#855b63 size=3>".$row[10]."</center></font>"; echo "<center><a href=items/".$row[0].".php><img src='images/button_details.gif'>[/url]</center>"; echo "</td>"; if ($i == '2') {echo "</tr>";$i='0'} } echo "</table>"; } else { // no // print status message echo "No items found!"; } Link to comment https://forums.phpfreaks.com/topic/105527-using-php-to-format-sql-results/#findComment-540831 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.