/    Sign up×
Community /Pin to ProfileBookmark

Array quandry

I’m dipping my toe into arrays, multidimensional no less. My code is doing nothing, renders nothing in the browser except the empty shell of a doc in source (missing the meta tags for some reason). My book and google aren’t helping in this case.

My objective is to build a row of column headers with sorting links.

This all takes place within <tr></tr> tags, and the page worked fine when I built the rows manually – it just seemed more efficient and maintainable to built the header columns from an array.

[code=php]
$columns = array( // assign associative array for each column (fieldname, label):
array (‘Servername’,’Server’),
array (‘Platform’,’Platform’),
array (‘role’,’Role’),
array (‘primary’,’Primary’),
array (‘secondary’,’Secondary’),
array (‘uporder’,’UPOrder’),
array (‘ip’,’IP Address’),
array (‘rack’,’Rack’),
array (‘rebootlist’,’Reboot’),
array (‘barcode’,’Barcode’),
array (‘manufacturer’,”),
array (‘model’,’Model’),
array (‘hwclass’,’HW’),
array (‘status’,’Status’),
) // end outer ($columns) array

//build the column headers with sort links as appropriate
foreach ($column as $header) {
echo “<td class=’cell1′ style=’border-bottom:navy;’><b>”;
echo “<a style=’text-decoration:none;’ href=’$PHP_SELF?sortby=$header[0]&order=$altorder’>$header[1]</a>”;
echo “</b></td>n”;
} // end foreach
[/code]

Can anyone tell me what I’ve munged up?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@WebnerdMar 10.2006 — You are missing a semi-colon after your array declaration:

) // end outer ($columns) array

needs to be

); // end outer ($columns) array
Copy linkTweet thisAlerts:
@scaiferwauthorMar 10.2006 — Thanks, Craig.

I made that fix (that error would have caught me up sooner or later), but still get the same result.

A little surprised I'm not even getting a php syntax error.

Any one have further ideas?


Full code is:
[code=php]
<?
$pagetitle = "SXSQ | Server List";
include("includes/library.inc.php");

/***
*
*

* rev 04 Add sorting to first field
* rev 05 Build column headers from array with sorting links
*
*/

// connect to database
$dbh=mysql_connect ("localhost", "qadata", "QAKicks05")
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("qalab");

// establish sort field and order from query string
$sortby = "servername"; // initialize with default
($_REQUEST['order']) ? $order = $_REQUEST['order']: $order = "ASC"; // set sort order from query string of provided, or default to ASC
($_REQUEST['order'] == "DESC") ? $altorder= "ASC" : $altorder= "DESC" ;
if ($_REQUEST['sortby'] != "") { $sortby = strtolower($_REQUEST['sortby']); }
$ordericon = "&uarr;";
if ($altorder == "DESC") {$ordericon= "&uarr;";} else {$ordericon= "&darr;";}


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><?= $pagetitle ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<link rel="stylesheet" type="text/css" media="screen, projection" href="../files/vertical.css">
<link rel="stylesheet" type="text/css" media="screen, projection" href="<?=dynRoot()?>files/qanet.css">
</head>
<body background="<?=dynRoot()?>images/bg-greystripe.gif" bgcolor="#f1f1f1">
<p><br>
<table class="frame" bgcolor="maroon" border="1" cellspacing="0" cellpadding="2" width="90%" align="center" style="border: solid; color: navy;"><tr><td>
<table bgcolor="white" border="0" cellspacing="0" cellpadding="10" align="left" width="100%" style="border-collapse: collapse; color: navy;">
<tr><td>

<?//~~~~~~~~~~~~~~~~~~~~~~~~~~ MASTHEAD ~~~~~~~~~~~~~~~~~~~~~~~~~//?>
<table border=0 width='100%'><tr>
<td width="50%"><span style='font-size:170%;font-weight:bold;color:$000080;'>SXSQ Lab Servers</span></td>
<td align="right">
<table bgcolor='#0f4692' cellspacing='0' cellpadding='1' border='0'>
<tr>
<td>
<table cellspacing='0' cellpadding='3' border='0' bgcolor='#f6f6f2' width='100%'>
<tr>
<td align='center'>
<span class='mininavbox'>&nbsp;<a href='../../' CLASS='mininavbox'>Home</a>&nbsp;</span>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr></table>

<hr color="#000080">
<?//~~~~~~~~~~~~~~~~~~~~~~~~~~ /masthead ~~~~~~~~~~~~~~~~~~~~~~~~//?>

<table align="center" border="0" cellspacing="1" cellpadding="5" style="border-collapse: collapse; border-color: maroon;">
<tr><td>

<?
/***
* ~~~ Display Data ~~~
*
*/


$query = "SELECT * FROM servers ORDER BY $sortby $order";
$result = mysql_query($query);
// Error occurred, return given name by default //
$num_rows = mysql_numrows($result);
if(!$result || ($num_rows < 0)){
echo "Error displaying info";
return;
}
if($num_rows == 0){
echo "Database table empty";
return;
}

// echo "<p>$query"; // debug line

// Display table contents //

// build header row with sort links
echo "nn<table align='left' border='1' cellspacing='0' cellpadding="1" width='100%' style='border-style:thin;border-color:#000080;border-collapse:collapse;'>n";
echo "<tr>";
$columns = array( // assign associative array for each column:
array ('Servername','Server'),
array ('Platform','Platform'),
array ('role','Role'),
array ('primary','Primary'),
array ('secondary','Secondary'),
array ('uporder','UPOrder'),
array ('ip','IP Address'),
array ('rack','Rack'),
array ('rebootlist','Reboot'),
array ('barcode','Barcode'),
array ('manufacturer',''),
array ('model','Model'),
array ('hwclass','HW'),
array ('status','Status'),
); // end outer ($columns) array

//build the column headers with sort links as appropriate
foreach ($column as $header) {
foreach ($header as $key=>&val) {
}
echo "<td class='cell1' style='border-bottom:navy;'><b>";
echo "<a style='text-decoration:none;' href='$PHP_SELF?sortby=$header[0]&order=$altorder'>$header[1]</a>";
echo "</b></td>n";
} // end foreach


for($i=0; $i<$num_rows; $i++){
// Set row background for alternate rows - blue if row # is divisible by two (uses ternary operator '?')
($i % 2) ? $rowbgcolour = "#D5EAFF" : $rowbgcolour = "#FFFFFF";

$servername = mysql_result($result,$i,"Servername");
$platform = mysql_result($result,$i,"Platform");
$role = mysql_result($result,$i,"Role");
$primary = mysql_result($result,$i,"primary");
$secondary = mysql_result($result,$i,"secondary");
$uporder = mysql_result($result,$i,"uporder");
$ip = mysql_result($result,$i,"ip");
$rack = mysql_result($result,$i,"rack");
$rebootlist = mysql_result($result,$i,"rebootlist");
$barcode = mysql_result($result,$i,"barcode");
$manufacturer = mysql_result($result,$i,"manufacturer");
$model = mysql_result($result,$i,"model");
$hwclass = mysql_result($result,$i,"hwclass");
$status = mysql_result($result,$i,"status");

echo "n<tr bgcolor='$rowbgcolour'>n".
"<td class='cell1'>$servername</td>n".
"<td class='cell1'>$platform</td>n".
"<td class='cell1'>$role</td>n".
"<td class='cell1'>$primary</td>n".
"<td class='cell1'>$secondary</td>n".
"<td class='cell1'>$uporder</td>n".
"<td class='cell1'>$ip</td>n".
"<td class='cell1'>$rack</td>n".
"<td class='cell1'>$rebootlist</td>n".
"<td class='cell1'>$barcode</td>n".
"<td class='cell1'>$manufacturer</td>n".
"<td class='cell1'>$model</td>n".
"<td class='cell1'>$hwclass</td>n".
"<td class='cell1'>$status</td>n".
"</tr>n";
}

echo "</table>n<br>n";


?>

</td></tr>
</table>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@NogDogMar 10.2006 — You also need to get rid of the comma after the last array element.
×

Success!

Help @scaiferw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...