11
« on: January 19, 2008, 09:22:11 pm »
I'm trying to develop a php page that will allow you to batch set multiple media files with the same picture and attributes. This could be useful for setting the media attributes for episodes for shows and tracks within CDs. I'm new to php and far from a web developer. I put together a page that kind of works but I'm having a problem with variables.
The problem is that after I submit a query to a DB I fill an array with some values. This works fine, but after I do another submit with a different button, the values in the array seem to disappear. Any help will be appreciated. I figured it might be some php gurus in the community.
Here is the code, bear in mind that this is a rough, kind of proof of concept. After I get it to work, I will try to incorporate it into plutoadmin:
<?php
/***************** Connect to database ******************/
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("pluto_media") or die(mysql_error());
/****************** Set Title ****************************/
Echo "<html>";
Echo "<title>HTML with PHP</title>";
/***************** Variables ****************************/
$i = 0;
$querStr = $_POST['querStr'];
$mediaSubTypeEnum = array (1 => "TV Shows",2 => "Movies",3 => "Home Videos", 4 => "Sports Events", 5 => "Music Videos", 6 => "Alternative", 7 => "Popular Music", 8 => "Classical Music");
$fileFormatEnum = array (1 => "Low Res",2 => "DVD",3 => "Standard Def", 4 => "HD 720", 5 => "HD 1080", 6 => "Low Quality", 7 => "MP3", 8 => "CD Quality", 9 => "High-def audio");
/******************* Create main body of html page **********************************************/
Echo "
<body bgcolor='Green'>
<form action=".$_SERVER['PHP_SELF']." method='post'>
Search For:<input name='querStr' type='text' size='10'/>
<input type='submit' name='submit' value='submit'>";
/**************** perform action after button is clicked ***************************************/
if(isset($_POST['submit'])) { //If the initial button displayed is clicked
$result = mysql_query("SELECT * FROM File Where Filename LIKE '%".$querStr."%'");
Echo " <input type='submit' name='submit2' value='submit2'> // output results of query in html table - this works fine
<table border='1'>
<tr>
<th>Primary File</th>
<th>Update File</th>
<th>Filename</th>
<th>Path</th>
<th>MediaSubType</th>
<th>FileFormat</th>
<th>PK_File</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td><input type='radio' name='primary[]' value=$i/></td>";
echo "<td><input type='checkbox' name='CopyFile[]' value='$i'/></td>";
echo "<td>".$row['Filename']."</td>";
echo "<td>".$row['Path']."</td>";
echo "<td>".$mediaSubTypeEnum[$row['FK_MediaSubType']]."</td>";
echo "<td>".$fileFormatEnum[$row['FK_FileFormat']]."</td>";
$myArray[$i++] = $row['PK_File']; // this is the variable in question - it does get fill here, as expected
echo "<td>".$myArray[$i-1]." -".$i."</td>";
echo "</tr>";
}
echo "</table>";
}
/**************** perform action after second button is clicked ***************************************/
if(isset($_POST['submit2'])) {
foreach($_POST['primary'] as $value) { // determine which radio button is selected
$value = $value + 1;
echo "the primary is".$value."\n";
}
for ($i = 0; $i < 10; $i++) {
/* simple test to see if anything is in myArray. the array appears to be empty. This is the problem area. Before the second button was clicked
the array had verified values in it.*/
echo $myArray[$i]; // the array seems to have lost all previous values
echo " in loop".$i;
}
$result = mysql_query("SELECT * FROM Picture_File Where FK_File = '".$myArray[$value]."'"); // so the rest of the code won't work
$row = mysql_fetch_array($result);
$FK_Picture = $row['FK_Picture'];
$priVal = $value;
echo "prival and fk_pic = ".$priVal. " - ".$FK_Picture."\n";
foreach($_POST['CopyFile'] as $value) {
$value = $value + 1;
mysql_query("INSERT INTO `Picture_File` VALUES ($FK_Picture,$myArray[$value],NULL,NULL,NULL,0,NULL,NULL)");
mysql_query("UPDATE File SET mediaSubTypeEnum='.mediaSubTypeEnum[$priVal].' fileFormatEnum='.$fileFormatEnum[$priVal].' WHERE FK_File='.$myArray[$value].'");
}
}
Echo " </form>
</body>
</html>";
?>