Page 1 of 1

multicolumn output from a database with PHP

Posted: Tue Jul 26, 2005 9:19 pm
by liez_1606
i'm a newbie in PHP and mySQL web development. i have to develop a web based network management using php and mysql.
i have tried many tips and scripts and now i came out with this result:
this is table port:
CREATE TABLE port (
port_idx int(4) NOT NULL,
switchid int(4) NOT NULL,
slot varchar(2),
port int(4),
closet char(1),
location char(10),
jackid int(4),
mediaid char(2),
PRIMARY KEY(port)
);

i have 38 ports in this table and i want to display only ports as the output in a horizontal display which has 8 columns in a row.i also used recordset so i can display ports according to which switch that i chose.i've got the result but it start to display with the 2nd port, and the first port disappeared.

This is the full source code:

<?php
?>
<?php require_once('Connections/localhost.php'); ?>
<?php
$colname_Recordset2 = "1";
if (isset($HTTP_GET_VARS['switch'])) {
$colname_Recordset2 = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['switch'] : addslashes($HTTP_GET_VARS['switch']);
}
mysql_select_db($database_localhost, $localhost);
$query_Recordset2 = sprintf("SELECT port FROM port WHERE switchid = '%s'", $colname_Recordset2);
$Recordset2 = mysql_query($query_Recordset2, $localhost) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);

$colname_rstSwitch = "1";
if (isset($HTTP_GET_VARS['switch'])) {
$colname_rstSwitch = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['switch'] : addslashes($HTTP_GET_VARS['switch']);
}
mysql_select_db($database_localhost, $localhost);
$query_rstSwitch = sprintf("SELECT name FROM switch WHERE switch_idx = '%s'", $colname_rstSwitch);
$rstSwitch = mysql_query($query_rstSwitch, $localhost) or die(mysql_error());
$row_rstSwitch = mysql_fetch_assoc($rstSwitch);
$totalRows_rstSwitch = mysql_num_rows($rstSwitch);

$colname_rstswitch = "1";
if (isset($HTTP_GET_VARS['name'])) {
$colname_rstswitch = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['name'] : addslashes($HTTP_GET_VARS['name']);
}
mysql_select_db($database_localhost, $localhost);
$query_rstswitch = sprintf("SELECT * FROM switch WHERE name = '%s'", $colname_rstswitch);
$rstswitch = mysql_query($query_rstswitch, $localhost) or die(mysql_error());
$row_rstswitch = mysql_fetch_assoc($rstswitch);
$totalRows_rstswitch = mysql_num_rows($rstswitch);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>List of ports from switch</title>
</head>

<body bgcolor="#999999">
<h2 align="center"><font color="#0000FF">List of ports in switch <?php echo $row_rstSwitch['name']; ?>&nbsp;.</font></h2>
<p> <?php echo $totalRows_Recordset2 ?> port/ports in this switch <?php echo $row_rstSwitch['name']; ?>.</p>
<p> </p>
<?php
$columns = 8;
echo "<table border =\"0\" align =\"center\">\n";
for($i = 0; $i < $totalRows_Recordset2; $i++) {
$row_Recordset2 = mysql_fetch_array($Recordset2);
if($i % $columns == 0) {
echo "<tr bgcolor=\"yellow\">\n";
}
echo "<td align=\"center\">" .$row_Recordset2['port']. "</td>\n";
if(($i % $columns) == ($columns - 1) || ($i + 1) == $totalRows_Recordset2) {
echo "</tr>\n";
}
}
echo "</table>\n";
?>
<p align="center"><a href="switchbackup.php">Back
view page</a></p>
</body>
</html>
<?php
mysql_free_result($Recordset2);

mysql_free_result($rstSwitch);

mysql_free_result($rstswitch);
?>


anyone know how to solve this.. please help me.. :(

[/img]