ansipants Posted September 25, 2011 Share Posted September 25, 2011 I Have a page that will display the data that was posted into a database, but it displays it like this: id logoname locationname address city state zip phone website etc... I want to display the data like this: id <--- this will repeat records 4 columns across logoname locationname etc... id <--- this will repeat records 4 columns across logoname locationname etc... and then repeat similar for the rest of the data. Sorry Im a n00b, but trying. Here is the current code: <?php include "config.php"; $con = mysql_connect("$dbhost","$dbusr","$dbpass"); if (!$con) { die('Could not connect:'. mysql_error()); } mysql_select_db("$dbname",$con); $q="select * from venues"; $result=mysql_query($q); if (!$result) { die("Query to show fields from table failed"); } $fields_num = mysql_num_fields($result); echo "<table border='0'><tr>"; for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); echo "<td></td>"; } echo "</tr>\n"; while($row = mysql_fetch_row($result)) { echo "<tr>"; foreach($row as $cell) echo "<td>$cell</td>"; echo "</tr>\n"; } mysql_free_result($result); ?> Help would be greatly appreciated and an explanation so that I don't have to keep asking Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted September 26, 2011 Share Posted September 26, 2011 what does this "id <--- this will repeat records 4 columns across" actualy mean? could you maybe draw an example table in excel/word/whatever - screen cap it and attach it to your post so we can see exactly what layout you are looking for? Quote Link to comment Share on other sites More sharing options...
LiquidFusi0n Posted September 26, 2011 Share Posted September 26, 2011 I 'THINK' I understand what you are wanting. When using SELECT * it will select all and basically show a row at a time.... you are wanting to show a column at a time right? If so use the following SQL. SELECT logonname FROM venues; And then rinse and repeat for the other columns Hope this helps --LiquidFusi0n Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 26, 2011 Share Posted September 26, 2011 I think he is wanting to display the records four across, then start a new row. I.e. Record 1 | Record 2 | Record 3 | Record 4 -------------------------------------------- Record 6 | Record 6 | Record 7 | Record 8 -------------------------------------------- Record 9 | Record 10 | Record 11 | Record 12 Here you go. Made the number of columns configurable using the variable $max_columns. Also cleaned up some of the logic. <?php include "config.php"; $con = mysql_connect($dbhost, $dbusr, $dbpass); if (!$con) { die('Could not connect:'. mysql_error()); } mysql_select_db($dbname, $con); $query = "SELECT * FROM venues"; $result = mysql_query($query); $output = ''; if (!$result) { $output .= "<tr><td>Query to show fields from table failed</td></tr>\n"; } elseif(mysql_num_rows($result)==0) { $output .= "<tr><td>No records returned</td></tr>\n"; } else { $max_columns = 4; $record_count = 0; while($row = mysql_fetch_assoc($result)) { $record_count++; //Start new row if 1st record or $max_columns if($record_count%$max_columns==1) { $output .= "<tr>\n"; } //Display record $output .= "<td>{$row['id']}</td>\n"; $output .= "<td>{$row['logonname']}</td>\n"; $output .= "<td>{$row['locationname']}</td>\n"; //Add additional fields as needed //Close row if last record or $max_columns if($record_count%$max_columns==0) { $output .= "</tr>\n"; } } //Close last row if still open if($record_count%$max_columns!=0) { $output .= "</tr>\n"; } } ?> <table> <?php echo $output; ?> </table> Quote Link to comment Share on other sites More sharing options...
ansipants Posted September 27, 2011 Author Share Posted September 27, 2011 TY mj, your awsome and last time i Checked, i was still a female Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 27, 2011 Share Posted September 27, 2011 ... last time i Checked, i was still a female Well, I'm not going to make any definitive assertions without proof. Not after that awkward dirty dancing incident. *shudder* Quote Link to comment Share on other sites More sharing options...
ansipants Posted September 27, 2011 Author Share Posted September 27, 2011 lol. Btw, the script is displaying the the same way as before, just limiting to 4 per. http://freepoker.zymichost.com/Venues/venue_list.php Quote Link to comment Share on other sites More sharing options...
ansipants Posted September 27, 2011 Author Share Posted September 27, 2011 Ok, so I got it to print downward instead of across for the records, which is fine, but it doesn't start a new column after 4 Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 27, 2011 Share Posted September 27, 2011 Ok, so I got it to print downward instead of across for the records, which is fine, but it doesn't start a new column after 4 Show the code you are currently using. Edit: Bah! I see what I did. Just change this $output .= "<td>{$row['id']}</td>\n"; $output .= "<td>{$row['logonname']}</td>\n"; $output .= "<td>{$row['locationname']}</td>\n"; To this: $output .= "<td>\n"; $output .= "{$row['id']}<br>\n"; $output .= "{$row['logonname']}<br>\n"; $output .= "{$row['locationname']}<br>\n"; $output .= "</td>\n"; Quote Link to comment Share on other sites More sharing options...
ansipants Posted September 27, 2011 Author Share Posted September 27, 2011 ahhhh, ok, now i understand. The </br> creates the break to display the data for the record on the next line. Quote Link to comment 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.