Jump to content

Output/Creating a Table


Bopo

Recommended Posts

Hey

 

Basically I have a query which returns record which are 21 field sizes, the thing is I could echo all the table tags and insert all the data like so

 

<td>'. $row['brand']</td>

 

However as the data is 2 colums and 21 rows, it would take over 80 echo statements to echo all of the table tags, therefore im guessing their is a better method of doing this.

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/131245-outputcreating-a-table/
Share on other sites

Well this is it

 

$result = mysql_query("SELECT * FROM cardata WHERE model LIKE '%$cartype%' LIMIT 1");

if($row = mysql_fetch_array($result))
{

// This is where the table is, note is just html at the moment, but I will have to 
// use echo tags for every line, and insert the fields where the nbsp spaces are.


<table width="200" border="1">
  <tr>
    <td width="109">Brand</td>
    <td width="75"> </td>
  </tr>
  <tr>
    <td>Model</td>
    <td> </td>
  </tr>
  <tr>
    <td>Edition</td>
    <td> </td>
  </tr>
  <tr>
    <td>Price (£)</td>
    <td> </td>
  </tr>
  <tr>
    <td>Ncap Rating</td>
    <td> </td>
  </tr>
  <tr>
    <td>Alarm</td>
    <td> </td>
  </tr>
  <tr>
    <td>Driver Airbag</td>
    <td> </td>
  </tr>
  <tr>
    <td>Immobiliser</td>
    <td> </td>
  </tr>
  <tr>
    <td>Traction Control</td>
    <td> </td>
  </tr>
  <tr>
    <td>Remote Locking</td>
    <td> </td>
  </tr>
  <tr>
    <td>Cruise Control</td>
    <td> </td>
  </tr>
  <tr>
    <td>Body Type</td>
    <td> </td>
  </tr>
  <tr>
    <td>Fuel Type</td>
    <td> </td>
  </tr>
  <tr>
    <td>Max Power BHP/RPM</td>
    <td> </td>
  </tr>
  <tr>
    <td>Max Tourque lb ft/RPM</td>
    <td> </td>
  </tr>
  <tr>
    <td>0 - 60 (mph)</td>
    <td> </td>
  </tr>
  <tr>
    <td>Max Speed</td>
    <td> </td>
  </tr>
  <tr>
    <td>Combined Mpg</td>
    <td> </td>
  </tr>
  <tr>
    <td>Urban Mpg</td>
    <td> </td>
  </tr>
  <tr>
    <td>Extra Urban Mpg</td>
    <td> </td>
  </tr>
  <tr>
    <td>Insurance Group</td>
    <td> </td>
  </tr>
</table>

} else {
echo 'record not found';
}

// use echo tags for every line, and insert the fields where the nbsp paces are.

echo statements can span multiple lines, I would recommends a heredoc statement,

 

 echo <<<MY_HTML;
place all your HTML here
MY_HTML;

 

There are numerous ways for making your code better.

Hi, thanks for your help

 

However I keep getting an error on this line:

 

 echo <<<MY_HTML;

 

Parse error: syntax error, unexpected T_SL, expecting ',' or ';'

 

I have tried removing the semi colon etc, but the same error appears, anyway here is my code again

 

$result = mysql_query("SELECT * FROM cardata WHERE model LIKE '%$cartype%' LIMIT 1");

if($row = mysql_fetch_array($result))
  {
echo <<<MY_HTML;

<table width="200" border="1">
  <tr>
    <td width="109">Brand</td>
    <td width="75">$row['brand']</td>
  </tr>
  <tr>
    <td>Model</td>
    <td> </td>
  </tr>
  <tr>
    <td>Edition</td>
    <td> </td>
  </tr>
  <tr>
    <td>Price (£)</td>
    <td> </td>
  </tr>
  <tr>
    <td>Ncap Rating</td>
    <td> </td>
  </tr>
  <tr>
    <td>Alarm</td>
    <td> </td>
  </tr>
  <tr>
    <td>Driver Airbag</td>
    <td> </td>
  </tr>
  <tr>
    <td>Immobiliser</td>
    <td> </td>
  </tr>
  <tr>
    <td>Traction Control</td>
    <td> </td>
  </tr>
  <tr>
    <td>Remote Locking</td>
    <td> </td>
  </tr>
  <tr>
    <td>Cruise Control</td>
    <td> </td>
  </tr>
  <tr>
    <td>Body Type</td>
    <td> </td>
  </tr>
  <tr>
    <td>Fuel Type</td>
    <td> </td>
  </tr>
  <tr>
    <td>Max Power BHP/RPM</td>
    <td> </td>
  </tr>
  <tr>
    <td>Max Tourque lb ft/RPM</td>
    <td> </td>
  </tr>
  <tr>
    <td>0 - 60 (mph)</td>
    <td> </td>
  </tr>
  <tr>
    <td>Max Speed</td>
    <td> </td>
  </tr>
  <tr>
    <td>Combined Mpg</td>
    <td> </td>
  </tr>
  <tr>
    <td>Urban Mpg</td>
    <td> </td>
  </tr>
  <tr>
    <td>Extra Urban Mpg</td>
    <td> </td>
  </tr>
  <tr>
    <td>Insurance Group</td>
    <td> </td>
  </tr>
</table>
MY_HTML;
}

Yeah that was a noob error, basically uploading the wrong script  :-X

 

Anyway getting a new error on this:

 

  <td width="75">$row['brand']</td>

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

 

Something similar to this worked earlier, however its probably just syntax/format related.

Archived

This topic is now archived and is closed to further replies.

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