Jump to content

Trouble displaying column only once and spliting results across columns


phpnewbie1979

Recommended Posts

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

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)

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.

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>

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.