Forum Moderators: coopster

Message Too Old, No Replies

checkboxes in a form to determine whether to display in results

checkboxes determining output

         

PRosales

6:07 pm on May 24, 2005 (gmt 0)

10+ Year Member



I am making an effort to put checkboxes in my form that will determine whether to display that column or not. This is my first time doing this with PHP and I am hoping you gurus can assist. I have gotten some extremely great assistance in the past, so I am looking forward to any recommendations.

currently my form is as follows:

--form.php--
<?php

// Make a MySQL Connection

$sql = "SELECT insert_time as day
FROM
test_Nodes_Available_ACRL_Atlantis";
$result1 = mysql_query($sql) or die(mysql_error());
$result2 = mysql_query($sql) or die(mysql_error());

?>
<form action="view.php" method="GET">
<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2">Date Range: <br /></td>
</tr>
<tr>
<td>Start Date: <br /></td>
<td>Stop Date: <br /></td>
</tr>
<tr>
<td><select name='xdate'>
<option value=''>Select Start Time</option>
<?php
while ($row = mysql_fetch_assoc($result1)) {
echo "<option value='{$row['day']}'>{$row['day']}</option>";
}
?>
</select></td>
<td><select name='zdate'>
<option value=''>Select Stop Time</option>
<?php
while ($row = mysql_fetch_assoc($result2)) {
echo "<option value='{$row['day']}'>{$row['day']}</option>";
}
?>
</select></td>
</tr>
</table>
<br />Select Resource: <br />
<select name="fromwhere">
<option value="test_Nodes_Available_ACRL_Atlantis">ACRL Atlantis</option>
<option value="test_Nodes_Available_ACRL_Medusa">ACRL Medusa</option>
<option value="test_Nodes_Available_TLC2_Atlantis">TLC2 Atlantis</option>
<option value="test_Nodes_Available_TLC2_Medusa">TLC2 Medusa</option>
</select>
<br />
<br />
Select Data to Display: <br />
<input name="nodesavailable[]" type="checkbox" value="nodes_available">Nodes Available</input><br><br>
<input name="nodesbusy[]" type="checkbox" value="nodes_busy">Nodes Busy</input><br><br>
<input name="cpuavailable[]" type="checkbox" value="cpu_available">CPU Available</input><br><br>
<input name="cpubusy[]" type="checkbox" value="cpu_busy">CPU Busy</input><br><br>
<input name="jobsrunning[]" type="checkbox" value="jobs_running">Jobs Running</input>
<input type ="submit" value="SUBMIT"/>
</form>

and the results page is:
--view.php--
?php
// Make a MySQL Connection
mysql_connect("localhost", "prosales", "123temp") or die(mysql_error());
mysql_select_db("tlc2_ca_test") or die(mysql_error());

if(isset($_GET['fromwhere'])):
$fromwhere = $_GET['fromwhere'];
$x = DATE($_GET['xdate']);
$z = DATE($_GET['zdate']);

$result = @mysql_query("SELECT id, insert_time, nodes_available, nodes_busy, cpu_available, cpu_busy, jobs_running, FROM_UNIXTIME(insert_time) as date_string FROM $fromwhere WHERE insert_time>='$x' AND insert_time<='$z'");
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
?>
<table border="1" align="center" cellpadding="2" cellspacing="2" bordercolor="#000000">
<tr><th>Day</th><th>Nodes Available</th><th>Nodes Busy</th> <th>CPU Available</th><th>CPU Busy</th><th>Jobs Running</th></tr>

<?php
while ($row = mysql_fetch_array($result)) {
$id=$row['id'];
$dbdate=$row["date_string"];
echo '<tr><td align=center>' . $dbdate . '</td>';
echo '<td align=center>' . $row['nodes_available'] . '</td>';
echo '<td align=center>' . $row['nodes_busy'] . '</td>';
echo '<td align=center>' . $row['cpu_available'] . '</td>';
echo '<td align=center>' . $row['cpu_busy'] . '</td>';
echo '<td align=center>' . $row['jobs_running'] . '</td>';
echo "</tr>";
}?></table>

With the current set all the columns are displayed. How can I approach it to only display the columns I check in the form?

Thanks in advance,
Pete

mcibor

9:37 pm on May 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You aproached the problem from a bit confusing way - there's a much simpler version:


Select Data to Display: <br />
<form action="#" method="POST">
<input name="view[]" type="checkbox" value="nodes_available">Nodes Available</input><br><br>
<input name="view[]" type="checkbox" value="nodes_busy">Nodes Busy</input><br><br>
<input name="view[]" type="checkbox" value="cpu_available">CPU Available</input><br><br>
<input name="view[]" type="checkbox" value="cpu_busy">CPU Busy</input><br><br>
<input name="view[]" type="checkbox" value="jobs_running">Jobs Running</input>
<input type ="submit" value="SUBMIT"/> </form>
...

mcibor

9:38 pm on May 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
$select = implode(", ", $_POST["view"]);
$result = @mysql_query("SELECT id, insert_time, ".$select.", FROM_UNIXTIME(insert_time) as date_string ...");

while ($row = mysql_fetch_array($result)) {
$id=$row['id'];
$dbdate=$row["date_string"];
echo '<tr><td align=center>' . $dbdate . '</td>';
foreach($_GET["view"] as $name=>$value)
{echo '<td align=center>' . $row[$value] . '</td>';
}}


This should work, however I didn't test it
Michal Cibor

PS. Sorry for splitting this message. I seem to have problem with longer codes on the computer over the bridge

mcibor

7:01 am on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There should be
foreach($_POST["view"] as $name=>$value)
{echo '<td align=center>' . $row[$value] . '</td>';
}

Sorry for my mistake
Michal Cibor

PRosales

3:46 pm on May 25, 2005 (gmt 0)

10+ Year Member



Michal,

Thanks for your assistance. I used your recommendation but couldn't get it to work. I massaged it a bit to what made sense to me and I am able to get the desired results. I am not sure if the code is streamlined properly but it is functioning. I certainly wouldn't have been able to do it without your help. Thanks.

Here is what I am using. If someone can recommend a better streamlined approach, I am definitely interested in comparing and learning.

--form.php--
<html>
<head>
<title>Cluster Accounting</title>
</head>

<body>
<?php

// Make a MySQL Connection

$sql = "SELECT insert_time as day
FROM
test_Nodes_Available_ACRL_Atlantis";
$result1 = mysql_query($sql) or die(mysql_error());
$result2 = mysql_query($sql) or die(mysql_error());

?>
<form action="view.php" method="GET">
<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2">Date Range: <br /></td>
</tr>
<tr>
<td>Start Date: <br /></td>
<td>Stop Date: <br /></td>
</tr>
<tr>
<td><select name='xdate'>
<option value=''>Select Start Time</option>
<?php
while ($row = mysql_fetch_assoc($result1)) {
echo "<option value='{$row['day']}'>{$row['day']}</option>";
}
?>
</select></td>
<td><select name='zdate'>
<option value=''>Select Stop Time</option>
<?php
while ($row = mysql_fetch_assoc($result2)) {
echo "<option value='{$row['day']}'>{$row['day']}</option>";
}
?>
</select></td>
</tr>
</table>
<br />
Select Resource: <br />
<select name="fromwhere">
<option value="test_Nodes_Available_ACRL_Atlantis">ACRL Atlantis</option>
<option value="test_Nodes_Available_ACRL_Medusa">ACRL Medusa</option>
<option value="test_Nodes_Available_TLC2_Atlantis">TLC2 Atlantis</option>
<option value="test_Nodes_Available_TLC2_Medusa">TLC2 Medusa</option>
</select>
<br />
<br />
Select Data: <br />
<input name="view[]" type="checkbox" value="nodes_available">Nodes Available</input><br><br>
<input name="view[]" type="checkbox" value="nodes_busy">Nodes Busy</input><br><br>
<input name="view[]" type="checkbox" value="cpu_available">CPU Available</input><br><br>
<input name="view[]" type="checkbox" value="cpu_busy">CPU Busy</input><br><br>
<input name="view[]" type="checkbox" value="jobs_running">Jobs Running</input>
<br />
<br />
<input type ="submit" value="SUBMIT"/>
</form>

</body>
</html>

--view.php--
<?php
// Make a MySQL Connection

if(isset($_GET['fromwhere'])):
$fromwhere = $_GET['fromwhere'];
$x = DATE($_GET['xdate']);
$z = DATE($_GET['zdate']);
$select = implode(", ", $_GET["view"]);

$result = @mysql_query("SELECT ".$select.", FROM_UNIXTIME(insert_time) as date_string FROM $fromwhere WHERE insert_time>='$x' AND insert_time<='$z'");
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
?>
<table border="1" align="center" cellpadding="2" cellspacing="2" bordercolor="#000000">
<tr><th>Day</th><th>Nodes Available</th><th>Nodes Busy</th> <th>CPU Available</th><th>CPU Busy</th><th>Jobs Running</th></tr>

<?php
while ($row = mysql_fetch_array($result)) {
$id=$row['id'];
$dbdate=$row["date_string"];
echo '<tr><td align=center>' . $dbdate . '</td>';
echo '<td align=center>' . $row['nodes_available'] . '</td>';
echo '<td align=center>' . $row['nodes_busy'] . '</td>';
echo '<td align=center>' . $row['cpu_available'] . '</td>';
echo '<td align=center>' . $row['cpu_busy'] . '</td>';
echo '<td align=center>' . $row['jobs_running'] . '</td>';
echo "</tr>";
}

?>
</table>

<?php endif;?>

Thanks to this community for all the help.
Pete

mcibor

8:18 pm on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if you are using $_GET on your form, then I really recommend you use POST:

<form action="view.php" method="POST">

and then get the values by $_POST["value"] - it is better method, because it's user friendly (they don't get really big url) and it's more difficult to tamper with.

I wrote the last part because otherwise you still print the data out (you don't chose what to show). If you change the my $_POST into your $_GET (or change the form method to post), then it should work immediately

Best regards
Michal Cibor