Page 1 of 1

Returning results from a query and displaying each field

Posted: Wed Jun 18, 2003 2:14 am
by Moonspell
Hello all. Here is my HTML code which will format the data I retrieve:

Code: Select all

<table width="647" border="1" cellpadding="0" cellspacing="3" bordercolor="#000000">
  <tr> 
    <td colspan="2" rowspan="10" bgcolor="#990000"><p><font color="#FFFFFF">$FName 
        $LName<br>
        $StreetNum $StreetName<br>
        $Zip</font></p>
      <p><font color="#FFFFFF">$Telephone<br>
        $Email </font></p></td>
    <td width="171" bgcolor="#FF9933"><strong>Guessed Number</strong></td>
    <td width="268" bgcolor="#CCCCCC">&nbsp;</td>
  </tr>
  <tr> 
    <td height="20" bgcolor="#FF9933"><strong>Date</strong></td>
    <td bgcolor="#CCCCCC">&nbsp;</td>
  </tr>
  <tr> 
    <td bgcolor="#FF9933"><strong>Vehicle Year</strong></td>
    <td bgcolor="#CCCCCC">&nbsp;</td>
  </tr>
  <tr> 
    <td bgcolor="#FF9933"><strong>Make</strong></td>
    <td bgcolor="#CCCCCC">&nbsp;</td>
  </tr>
  <tr> 
    <td bgcolor="#FF9933"><strong>Model</strong></td>
    <td bgcolor="#CCCCCC">&nbsp;</td>
  </tr>
  <tr> 
    <td bgcolor="#FF9933"><strong>Last Purchase</strong></td>
    <td bgcolor="#CCCCCC">&nbsp;</td>
  </tr>
  <tr> 
    <td bgcolor="#FF9933"><strong>Type of Vehicle</strong></td>
    <td bgcolor="#CCCCCC">&nbsp;</td>
  </tr>
  <tr> 
    <td bgcolor="#FF9933"><strong>Current Payment</strong></td>
    <td bgcolor="#CCCCCC">&nbsp;</td>
  </tr>
  <tr> 
    <td bgcolor="#FF9933"><strong>Most Important <br>
      When Buying</strong></td>
    <td bgcolor="#CCCCCC">&nbsp;</td>
  </tr>
  <tr>
    <td bgcolor="#FF9933"><strong>Marketing</strong></td>
    <td bgcolor="#CCCCCC">&nbsp;</td>
  </tr>
</table>
Since this is how the HTML output should look, the variable looking text ($FName and such) should be replaced by the variable.

I want to grab data from a field, such as:

Code: Select all

<?php
$result = mysql_query("SELECT * FROM CustomerEntry WHERE ClientNameID = '$id'");
?>
Then I want to take each row and display it's data in its own table. The $FName variables I'm displaying will be the column for that row. How can I go through each row?

Basically I'm just taking the row and outputting it. But I want it to be displayed in my own order and in my own fashion, rather then if I just looped through every field in an array.

Posted: Wed Jun 18, 2003 11:08 am
by volka
there are some examples at
http://www.php.net/mysql_fetch_array
http://www.php.net/mysql_fetch_assoc

basically you fetch the rows one by one in a loop. What you do with the values within the body of the loop is up to you

Code: Select all

<?php
...
while($row = mysql_fetch_assoc($result))
{
?>
	<tr>
		<td><?php echo $row['nameOfField1']; ?></td>
		<td><?php echo $row['nameOfField2']; ?></td>
		<td><?php echo $row['nameOfField3']; ?></td>
	</tr>
<?php
}
...
?>

Posted: Wed Jun 18, 2003 11:52 am
by slick
Not sure exactly what you mean...

If you want your own ordering sequence, specify an ORDER BY |col_name| in your SQL query. For example,

Code: Select all

<?php 
$result = mysql_query("SELECT * FROM CustomerEntry WHERE ClientNameID = '$id' ORDER BY vehicle_year DESC");
will order your customers in order of the newest vehicle registration. You can make more complex ordering systems. See the mysql docs for more info

Posted: Wed Jun 18, 2003 11:03 pm
by Moonspell
Thanks volka. I didn't know how to retrieve in an assoc array.