Page 1 of 1

Arrays, Arrays and more arrays

Posted: Tue Dec 21, 2004 12:25 am
by sumomike
Please help if you can. I new to php so please be kind, this is my first page. I am having trouble with an array keeping its value and then passing the array to another page/table. The script has 2 dropdown menus populated from MySQL and then a check box that creates an array. I want this array to stay while more choices are selected and added to it. however if i try to make another selection from the dropdown box, the previous array disappears.

When the user is done, the array is passed to the next page. You can see what I have so far here: http://s92923142.onlinehome.us (hope I didnt break any rules by posting a link.)

I know it looks sloppy but I am more worred about functionality at this point then making it pretty.

any sugestion on how to do this a better way are welcome.

thanks

Code: Select all

<?PHP
// Get Connection Settings
include("connect.php");

// Check to see if category has been sent
$mnucategory = isset($_POST['mnucategory'])? $_POST['mnucategory']: 0;

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Select an Application</title>

</head>

<body>
<h1 align="Left">Application Time Chart</h1>

<table width="584" border="0" cellpadding="0" cellspacing="0">
<!--DefaultTable-->
<tr>
<td colspan="3" valign="top">

<?php
echo '<form name="target" method="post" action="'.$_SERVER['PHP_SELF'].'">';


// create the SQL statement
$sql = "SELECT id, AppName FROM tbltarget order by AppName";

// execute the SQL statement
$result = mysql_query($sql, $conn) or die(mysql_error());

//get the number of rows in the resultset
$number_of_rows=mysql_num_rows($result);
//echo "the number of rows is $number_of_rows";

echo '
<p>1) Select an App:<br>
<select name="mnucategory" id="mnucategory" onchange="this.form.submit();">
<option value="0"'.($mnucategory == 0? ' SELECTED': '').'>Choose an App</option>';

while($row = mysql_fetch_array($result)){
echo '
<option value="'.$row[0].'"'.(($mnucategory == $row[0])? ' SELECTED': '').'>'.$row[1].'</option>';
}
echo '
</select>
</p>';

// create the SQL statement
$sql2 = "SELECT testid, TestName, id, testhours FROM tbltest";
if($mnucategory != 0){ // Filter 
$sql2 .= " WHERE id='".$mnucategory."'";

}
// execute the SQL statement
$result2 = mysql_query($sql2, $conn) or die(mysql_error());
echo '
<p>2) Select a Test Case:<br>
<select name="mnuproduct" id="mnuproduct" onchange="this.form.submit();">
<option value="0" '.($mnuproduct == 0? ' SELECTED': '').'>Choose a Test</option>
';
while($row2 = mysql_fetch_array($result2)){
echo '
<option value="', $row2[0].'"'.(($mnuproduct == $row2[0])? ' SELECTED': '').'>'. $row2[1], '</option>';
}
echo '
</select>
</p>';

?>
 
</form>
</table>
	
<?php 
	  
$result3 = mysql_query("SELECT * FROM tbltest
WHERE testid='$mnuproduct'") or die(mysql_error());

while($row = mysql_fetch_array( $result3 )) {

$num1 = $row['TestHours'];
$name1 = $row['TestName'];
}
    
/* 
// later create an array from mysql to populate the dropdown.
$result4 = mysql_query("SELECT * FROM tblcase
WHERE caseid='$mnuproduct'") or die(mysql_error());
  	//Added to view the results in the DB, remove later
	// Print out the contents of each row into a table
echo "<table border="0" cellpadding="5" cellspacing="0">\n";
//echo "<tr> <th>Test Name</th> <th>Test Hours</th> </tr>";

while($row = mysql_fetch_array($result4, MYSQL_ASSOC)) {

echo "<tr> <td>";
echo $row['TestName'];
echo "</td><td>";
echo $row['TestHours'];
echo "</td> </tr>";
 }*/


?>

<?php

if ($name1 == "Real")
{
   	$Test_Array = array(
		"Real Streaming"=>"8",
		"Real Offline"=>"9",
		"Real IOT"=>"10",
		"Real Full Regression"=>"11",
		"Real Sanity"=>"12"
		);
	    ksort($Test_Array);

echo "<form action="{$_SERVER['PHP_SELF']}" method="post">\n";
//echo "<form action="output.php" method="post">\n";

foreach($Test_Array as $media => $hours)
{
    echo "  <input type="checkbox" value="{$media}" name="Final_Array[]">{$media}</input><br />\n";
}

echo "3)  <input type="submit" value="Add to Array" name="submit"/>\n";
echo "</form>\n<br><br><br>";

}
?>
 
<?php
if ($name1 == "Media Player")
{
	$Test_Array = array(
		"QTV Streaming"=>"8",
		"QTV Offline"=>"9",
		"QTV IOT"=>"10",
		"QTV Full Regression"=>"11",
		"QTV Sanity"=>"12"
		);
        ksort($Test_Array);

//echo "<form action="output.php" method="post">\n";
echo "<form action="{$_SERVER['PHP_SELF']}" method="post">\n";

foreach($Test_Array as $media => $hours)
{
    echo "  <input type="checkbox" value="{$media} {$hours}" name="Final_Array[]">{$media}</input><br />\n";
}
echo "3) <input type="submit" value="Add to Array" name="submit"/>\n";
echo "</form>\n<br><br><br><br>";

}
?>

<?php
if ($Final_Array != "")
{

foreach ($Final_Array as $media => $hours){
  print "$name $hours<br>\n";
}

echo "<form action="output.php" method="post">\n";
echo "4) <input type="submit" value="Output.php" name="submit"/>\n<br>";
}
?>
</body>
</html>

Posted: Tue Dec 21, 2004 1:08 am
by rehfeld
please read this

viewtopic.php?t=21171