Jump to content

Using PHP to format SQL results


stragglerat

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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