Bopo Posted November 3, 2008 Share Posted November 3, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/131245-outputcreating-a-table/ Share on other sites More sharing options...
wildteen88 Posted November 3, 2008 Share Posted November 3, 2008 If the $row variable is an array use a foreach loop foreach($row as $cell) { echo '<td>'.$cell.'</td>'; } Quote Link to comment https://forums.phpfreaks.com/topic/131245-outputcreating-a-table/#findComment-681392 Share on other sites More sharing options...
Bopo Posted November 3, 2008 Author Share Posted November 3, 2008 Hi Thats a really good idea, as it does work, however I kinda forgot I am manually selecting 21 cells from the query to display in the web, as it actually alot more data that isnt needed, therefore I guess manual is the only way. Thanks anyway Quote Link to comment https://forums.phpfreaks.com/topic/131245-outputcreating-a-table/#findComment-681505 Share on other sites More sharing options...
kenrbnsn Posted November 3, 2008 Share Posted November 3, 2008 Please post the code. If there is a better way, someone here will see it. Ken Quote Link to comment https://forums.phpfreaks.com/topic/131245-outputcreating-a-table/#findComment-681508 Share on other sites More sharing options...
Bopo Posted November 3, 2008 Author Share Posted November 3, 2008 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/131245-outputcreating-a-table/#findComment-681592 Share on other sites More sharing options...
wildteen88 Posted November 3, 2008 Share Posted November 3, 2008 // 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. Quote Link to comment https://forums.phpfreaks.com/topic/131245-outputcreating-a-table/#findComment-681596 Share on other sites More sharing options...
Bopo Posted November 3, 2008 Author Share Posted November 3, 2008 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; } Quote Link to comment https://forums.phpfreaks.com/topic/131245-outputcreating-a-table/#findComment-681632 Share on other sites More sharing options...
DarkWater Posted November 3, 2008 Share Posted November 3, 2008 Removing the semi-colon on the echo line removes the error when I test it... Quote Link to comment https://forums.phpfreaks.com/topic/131245-outputcreating-a-table/#findComment-681635 Share on other sites More sharing options...
Bopo Posted November 4, 2008 Author Share Posted November 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/131245-outputcreating-a-table/#findComment-681652 Share on other sites More sharing options...
wildteen88 Posted November 4, 2008 Share Posted November 4, 2008 Wrap your variable within curly braces {}, eg <td width="75">{$row['brand']}</td> You will have to do this with all your $row variables and/or variables which are arrays within your string. Quote Link to comment https://forums.phpfreaks.com/topic/131245-outputcreating-a-table/#findComment-682239 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.