Jump to content

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.

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.