phpnewbie1979 Posted February 10, 2010 Share Posted February 10, 2010 Hi everyone I have a table that holds neighborhood information. I'm trying to display the names of the neighborhood in a list across three columns sorted by city with each name serving as a link to the rest of the data. What I managed to do is display the list of neighborhood names and have them link to the rest of the data. I can't figure out how to make the list of names show in several columns and how to sort the data by City and have the City name show only once. My code and sample of what I'm looking for is below. Any help is greatly appreciated. <?php require_once "connect_to_mysql.php"; // if no id is specified, list the available articles if(!isset($_GET['id'])) { $self = $_SERVER['PHP_SELF']; $query = "SELECT id, name, content FROM table_name ORDER BY id"; $result = mysql_query($query) or die('Error : ' . mysql_error()); // create the article list $content = '<ol>'; while($row = mysql_fetch_array($result, MYSQL_NUM)) { list($id, $name) = $row; $content .= "<li><a href=\"$self?id=$id\">$name</a></li>\r\n"; } $content .= '</ol>'; $name = 'list'; } else { // get the article info from database $query = "SELECT * FROM table_name WHERE id=".$_GET['id']; $result = mysql_query($query) or die('Error : ' . mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC); $name = $row['name']; $content = $row['content']; $f1 = $row['f1']; $f2 = $row['f2']; $f3 = $row['f3']; $f4 = $row['f4']; $f5 = $row['f5']; } ?> <html> <head> <title> <?php echo $name; ?> </title> </head> <body> <table width="600" border="0" align="center" cellpadding="10" cellspacing="1" bgcolor="#336699"> <tr> <td bgcolor="#FFFFFF"> <h1 align="center"><?php echo $name; ?></h1> <?php echo $content; echo $f1; echo $f2; echo $f3; echo $f4; echo $f5; // when displaying an article show a link // to see the article list if(isset($_GET['id'])) { ?> <p> </p> <p align="center"><a href="<?php echo $_SERVER['PHP_SELF']; ?>">]List</a></p> <?php } ?> </td> </tr> </table> </body> </html> I need the page to look like: City_one name1 name2 name 3 name 4 name5 name6 City_two name 7 name 8 name 9 Link to comment https://forums.phpfreaks.com/topic/191570-trouble-displaying-column-only-once-and-spliting-results-across-columns/ Share on other sites More sharing options...
Catfish Posted February 10, 2010 Share Posted February 10, 2010 the problem you are having is to do with the output formatting when 'id' is not set? so it is a problem with this portion of code?: <?php require_once "connect_to_mysql.php"; // if no id is specified, list the available articles if(!isset($_GET['id'])) { $self = $_SERVER['PHP_SELF']; $query = "SELECT id, name, content FROM table_name ORDER BY id"; $result = mysql_query($query) or die('Error : ' . mysql_error()); // create the article list $content = '<ol>'; while($row = mysql_fetch_array($result, MYSQL_NUM)) { list($id, $name) = $row; $content .= "<li><a href=\"$self?id=$id\">$name</a></li>\r\n"; } $content .= '</ol>'; $name = 'list'; } (just checking so as to not waste time looking at the rest of the code) Link to comment https://forums.phpfreaks.com/topic/191570-trouble-displaying-column-only-once-and-spliting-results-across-columns/#findComment-1009885 Share on other sites More sharing options...
phpnewbie1979 Posted February 10, 2010 Author Share Posted February 10, 2010 Thank you for your response, But I'm really new to php. Can you please elaborate on what I'm doing wrong or maybe point me in the right direction to obtaining a better at doing this. Link to comment https://forums.phpfreaks.com/topic/191570-trouble-displaying-column-only-once-and-spliting-results-across-columns/#findComment-1010202 Share on other sites More sharing options...
Catfish Posted February 10, 2010 Share Posted February 10, 2010 your script is following two lines of flow from what i can see. one when $_GET['id'] is not set and one when it is set. this means your problem is in the section of code i pasted above. i would do more investigating now but must go to work for the day. maybe someone else can help for now. Link to comment https://forums.phpfreaks.com/topic/191570-trouble-displaying-column-only-once-and-spliting-results-across-columns/#findComment-1010207 Share on other sites More sharing options...
coder7 Posted February 10, 2010 Share Posted February 10, 2010 When you perform your select statement try using "order by name asc" or "order by name desc" at the end of the query. That will will sort the city names in alphabetical order. As for the spanning row issue, try using tables to seperate rows as below, making one row span three columns (obviously with php outputting the names and cities in the appropriate TD field). <table> <tr> <td colspan=3>City</td> </tr> <tr> <td> Name1</td> <td> Name2</td> <td> Name3</td> </tr> </table> Link to comment https://forums.phpfreaks.com/topic/191570-trouble-displaying-column-only-once-and-spliting-results-across-columns/#findComment-1010220 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.